Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Meenaachy's avatar

Post text and image to twitter using laravel and twitter api version 2

$tweetText = $request->input('tweet_text');

    // $image = [];

$media = $connection->upload('media/upload', ['media' => 'images/botton-image.jpg'], true); // array_push($image, $media->media_id_string); // check result (returns media object) // var_dump($media); //Create a Tweet $tweetParams = [ 'text' => $tweetText, 'media'=> ['media_ids' => [$media->media_id_string], ]

];

$connection->setApiVersion('2'); $status = $connection->post('tweets', $tweetParams, true);

// version 1.1 api 'statuses/update'

if ($connection->getLastHttpCode() == 200) { echo "Successfully posted a test tweet and database.\n"; $tweetId = $status->data->id; $tweetText = $status->data->text;

return view('tweet', compact('tweetText'))->with('report_created', 'Report has been created successfully');

} else { echo "Successfully posted a test tweet and database..\n"; $httpCode = $connection->getLastHttpCode(); $apiResponse = $status; return view('tweet', compact('httpCode', 'apiResponse')); }

In this its can post text with image... But I need select image from blade file nad post the image to tiwtter.... Please help me how to modify the code. Thank you

0 likes
1 reply
hupp's avatar

@meenaachy Are you using package thujohn/twitter ? for API Calling ? Please re-post pretty format code for check.

Please or to participate in this conversation.