Main menu
WalkswithMeSocial NetworkingTweets with media file using Oauth API in php

Tweets with media file using Oauth API in php

Tweets with media file using Oauth API in php is simple and cute, It simply allow us to upload an media file to twitter and make a status with image is far better than old text only status message. Yes now I have a situation to post an image to twitter with status update so here I’m sharing what code is works fine for me. You can check my old post related to post to twitter using Oauth api for text only status updates
First of all using Oauth API with twitter is very simple and nice, Recently twitter changed their APP creation now you have to give your mobile number to create an APP with write permission. If you have already an app with write permission then that one’s credentials can be used for Tweets with media file.

For creating twitter Oauth API Apps check this link .

Post to twitter using PHP Oauth API

Tweets with media file using Oauth API in php

After creating your Apps on twitter account just create a PHP file or your custom function for post to twitter using Oauth API, this is using version 1.1.


<?PHP
require_once 'twitteroauth.php';
define("CONSUMER_KEY", "****consumer key*******");
define("CONSUMER_SECRET", "*******Consumer secret********");
define("OAUTH_TOKEN", "**********Oauth token*******");
define("OAUTH_SECRET", "*****Oauth Secret********");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
$image = 'walkswithme.png';
$status_message = 'Attaching an image to a tweet';
$status = $connection->upload('statuses/update_with_media', array('status' => $status_message, 'media[]' => file_get_contents($image)));
echo json_encode($status);
?>

Make sure few thing then your first attempt will work successfully.

  • Your APP have proper read/write permission.
  • Your APP Keys are correct in configuration file without white spaces.
  • You didn’t touch any code on the API core files.
  • The media attaching with the status message is valid format and it exists in the given path.
  • Do not use status message and media file directly inside upload() It may not work.

Make sure your twitter apps have the proper access to read/write data from/to your twitter account.

Post to twitter using PHP Oauth API

Tweets with media file using Oauth API in php

This simple code will post an image with status message to my own twitter account like below.

Tweets with media file using php Oauth API

Tweets with media file using php Oauth API

You can download the working example from below link.

Download1777 downloads

31 thoughts on “Tweets with media file using Oauth API in php

  1. i am using same code bur following error displays “Fatal error: Call to a member function upload() on a non-object in “

  2. I am not able to upload media with given function. Becaust upload function is not there in twitteroauth.php.. So can anyone can share link for twitteroauth.php having upload function.
    Thanks in advance.

  3. Hi All. The script with upload the image stop working, why? The Twitter no approwed the upload the image? I can only change the status script, no work upload image. Cuple days ago all work fine. Whots happend? Please for help.
    Regards Tomek

  4. Hi all. What a reason . Which code to use to send the image to stop working. I can only change status.

    $connection->post('statuses/update', array('status' => 'This tweet is from PHP page :)'));

    the upload image not work:

    $status = $connection->upload('statuses/update_with_media', array('status' => $status_message, 'media[]' => file_get_contents($image)));


    Is this Twitter forbid adding images??!!. Please help me.
    Regards Tomek

  5. Thank you for sharing and already got it running! I just have a question, some pictures dont get uploaded and i think its because of the file size, am i correct? Im not sure. The code runs on some pictures and doesnt on others.

  6. I have used your code and keep getting this error: Fatal error: Call to undefined function curl_init() in /base/data/home/apps/s~api-project-73840364026/1.383277508189485910/FOCapp/twitteroauth.php on line 219.

    What am I doing wrong?

  7. Hello WWM Support,
    i use this code and receive error:

    Fatal error: Call to undefined method TwitterOAuth::upload() in /home/public_html/test/twitter.php on line 15

    this is the line:

    $status = $connection->upload(‘statuses/update_with_media’, array(‘status’ => $status_message, ‘media[]’ => file_get_contents($image)));

    Please help to resolve this problem.
    Regards
    Tomek

  8. i used your code but it’s not working .
    $statues= $twitteroauth->post(‘statuses/update’, array(‘status’ => $message));
    i used above one for update status.
    $image = $_FILES [‘file’] [‘tmp_name’];
    $status = $twitteroauth->upload(‘statuses/update’, array(‘status’ => $status_message, ‘media[]’ => $image));
    for image upload with text on timeline i used this code line but it’s not showing on timeline

  9. can you please any code of php like any form or something in which i can integrate this pakage of twitter api it will be help i am having no buddy to help around me please help?

    if documentation is also there it will more then help… thank’s in advance

    1. The complete code can be downloaded from the Download link provided at the bottom of the article.
      Just download it follow my articles step by set you will be able integrate it.
      Hope it make sense..

  10. The above code is not working at my end. I’m getting the following error

    {“request”:”\/1.1\/statuses\/update_with_media.json”,”error”:”Read-only application cannot POST.”}

    I’ve added my api keys. I don’t see any option to add my username & password as hardcore to check whether the code is working or not.

    Any help is appreciated.
    Thanks.

    1. Your application created with read only access, Please make sure it have read-write access.
      Twitter updated their APP authentication , so you should add a profile phone number and need to verify it for creating write permission APP.
      then try once again it should work!.

  11. Hi

    Thanks a lot..am spending more then two for this concept ..and u given download folder is so useful l..because sometime ..people struggle while inserting a plugin ..

    keep-it up

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

FacebookTwitterGoogle+RSS