williamsdb / php2Bluesky

Helper library to post to Bluesky Social
GNU General Public License v3.0
5 stars 2 forks source link

Is there a way to submit 4 images? I was able to submit one image but not more than two. #3

Closed e1blue closed 9 months ago

e1blue commented 9 months ago

Is there a way to submit 4 images? I was able to submit one image but not more than two.

$connection = bluesky_connect($handle, $password);
$image1 = upload_media_to_bluesky($connection, $filename1);
$image2 = upload_media_to_bluesky($connection, $filename2);
$image3 = upload_media_to_bluesky($connection, $filename3);
$image4 = upload_media_to_bluesky($connection, $filename4);
$mediaArray = array($image1, $image2, $image3, $image4);
$response = post_to_bluesky($connection, $text, $mediaArray);
williamsdb commented 9 months ago

I have only tested it uploading one image but it should be possible to upload more in one go. I'll have a look and see if I can work out what I need to do to the code to allow more than one image upload. I'll then push the code back here.

williamsdb commented 9 months ago

I have updated the code to handle up to four images which (I think) is the maximum that Bluesky allows. If you pull the latest code you can do exactly as you tried before, i.e. upload four images, create an array from the output and then post. Posting a single image works as it did before.

The code below shows that in action posting both a single image and four images:

    $handle = '<your handle>';
    $password = '<your password>';
    $filename1 = 'https://www.spokenlikeageek.com/wp-content/uploads/2023/10/IMG_5334.jpeg';
    $filename2 = '/Users/neilthompson/Downloads/2024-01-02 15-22-08.png';
    $filename3 = 'https://www.neilthompson.co.uk/wp-content/uploads/2023/12/2023-12-07-10.43.53.jpg';
    $filename4 = '/Users/neilthompson/Downloads/2024-01-02 15-21-15.png';
    $text = 'Hey look! four images!';

    // connect to Bluesky API
    $connection = bluesky_connect($handle, $password);

    // send an image with text
    $image1 = upload_media_to_bluesky($connection, $filename1);
    $response = post_to_bluesky($connection, $text, $image1);
    print_r($response);

    // send multiple images with text
    $image1 = upload_media_to_bluesky($connection, $filename1);
    $image2 = upload_media_to_bluesky($connection, $filename2);
    $image3 = upload_media_to_bluesky($connection, $filename3);
    $image4 = upload_media_to_bluesky($connection, $filename4);
    $imageArray= array($image1, $image2, $image3, $image4);
    $response = post_to_bluesky($connection, $text, $imageArray);
    print_r($response);

Let me know how you get on.

e1blue commented 9 months ago

Thank you for your response. ,, I just tried it and got a ( ! ) Fatal error: Uncaught JsonException: Syntax error in /vendor/cjrasmussen/bluesky-api/src/BlueskyApi.php on line 134. Probably because the version of BlueskyApi is now v2.0.0. ......

williamsdb commented 9 months ago

I've had a look at where I am using my code and it is still working fine for me so I am not sure that it can be due to the API upgrade. However, the error is not in my code so there's not much I can do about it. I would suggest that you raise a ticket here and see if the author can sort. Would be interested to hear how you get on.

e1blue commented 9 months ago

Thanks for the reply. I will try to think about it a little more myself. I'm a bit confused about the meaning of you raise a ticket here, but I'll look this up myself later as well.

williamsdb commented 9 months ago

The error you sent was in /vendor/cjrasmussen/bluesky-api which is the repository that actually sends and receives from BlueSky. I did not write that but you can see it here https://github.com/cjrasmussen/BlueskyApi. My suggestion was to create a ticket there so that Clark can see it and fix it.

e1blue commented 9 months ago

Thank you again for your reply. I understand now. I will try to figure it out one more time by myself and if I still don't understand, I will contact /vendor/cjrasmussen/bluesky-api.

e1blue commented 9 months ago

I don't know why, but the following works fine in my environment. Thank you very much.

▼functions.php

function bluesky_connect($handle, $password)
{
  //$connection = new BlueskyApi($handle, $password);
  $connection = new BlueskyApi();
  try {
    $connection->auth($handle, $password);
    return $connection;
  } catch (Exception $e) {
    die('Authentication Error: ' . $e->getMessage()); }
  }
}