0

I'm using Twitter library its called Twitteroauth, and follow their instruction about image posting. So when I try to do image posting its throwing error because media are not uploading. Normal text posting is working fine as expected and everything I did check many times, but its not working. So can anyone fix this?

Image uploading is not working:

require "vendor/autoload.php";

use Abraham\TwitterOAuth\TwitterOAuth;

$connection = new TwitterOAuth('p8kebRtLTYZo4Lyc8p17lcUGZ', 'KUmxOxfgfISQPxM64kqA9YTEibC28s2tNjcqBNu5YpY1wTuJNU', '1236013086688989184-c5gHPKryY5zJTJp03ILU1N31atayJp', 'yvunJthcPlBRXnUaBwfbIpyv6q5i7jJX3TzxjLK6Mq5On');
$connection->setApiVersion('2');
$image = [];

$media = $connection->upload('media/upload', ['media' => 'C:\xampp\htdocs\twitter_oauth\S3169002871756923800.jpg']);
array_push($image, $media->media_id_string);

$data =  [
    'text' => 'Hello world',
    'media'=> ['media_ids' => $image]
];


$content = $connection->post("tweets", $data, true);

echo "<pre>";
print_r($content);
exit;

Getting error because of image uploading is not working. What can be the issue? Please help.

Notice: Trying to get property 'media_id_string' of non-object in C:\xampp\htdocs\twitter_oauth\index.php on line 28
stdClass Object
(
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [parameters] => stdClass Object
                        (
                            [$.media.media_ids[0]] => Array
                                (
                                    [0] => null
                                )

                        )

                    [message] => $.media.media_ids[0]: null found, string expected
                )

        )

    [title] => Invalid Request
    [detail] => One or more parameters to your request was invalid.
    [type] => https://api.twitter.com/2/problems/invalid-request
)

Expecting that it will work as per their documentation but its not working.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mir Shaon
  • 1
  • 4

1 Answers1

0

I know this is an old question but better late than never, right?

At the beginning of your code, just after creating the connection, instead of using this:

$connection->setApiVersion('2');

...try this...

$connection->setApiVersion('1.1');.

Leave the rest as is.

I don't know why this works but it does. I guess bugs.

JoshGeake
  • 141
  • 3
  • 13