skip to Main Content

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 AbrahamTwitterOAuthTwitterOAuth;

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

$media = $connection->upload('media/upload', ['media' => 'C:xampphtdocstwitter_oauthS3169002871756923800.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:xampphtdocstwitter_oauthindex.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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Change by:

    $connection->setApiVersion(‘1.1’);

    $media = $connection->upload(‘media/upload’, [‘media’ => ‘C:xampphtdocstwitter_oauthS3169002871756923800.jpg’]);

    array_push($image, $media->media_id_string);

    $connection->setApiVersion(‘2’);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search