I ended up using codebird and not TwitterAPIExchange.php. Please see my answer.
TwitterAPIExchange.php
I am racking my brain trying to figure out why my code is not working. I am able to post a status update fine to twitter but when I try and add an image it seems to never post it with the status.
With the many posts about this I have read I have tried them all applying the media examples and none seem to work.
One thing is that many of these posts refer to the API call url being https://api.twitter.com/1.1/statuses/update_with_media.json
which according to this article is depreciated.
The new URL "I think" is just https://api.twitter.com/1.1/statuses/update.json
At this point the status uploads fine, the image never does. Can anyone help me with my code please.
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "***",
'oauth_access_token_secret' => "***",
'consumer_key' => "***",
'consumer_secret' => "***"
);
$url = "https://api.twitter.com/1.1/statuses/update.json";
$requestMethod = 'POST';
$twimage = '60001276.jpg';
$postfields = array(
'media[]' => "@{$twimage}",
'status' => 'Testing Twitter app'
);
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
print_r($response);
I ended up not being able to use this method and found a more current solution. The one thing I learned about using php to tweet images with a message is that you have to load the image first up to twitter in which the API will return a media_id
back to you. That media_id
is associated with the image. Once you have the media_id
back then you associate that ID with your message and send the message with the media_id
's. That made the code make more sense once I learned that.
I used codebird instead to achieve tweeting with php.
All you have to do is create a function like so
function tweet($message,$image) {
// add the codebird library
require_once('codebird/src/codebird.php');
// note: consumerKey, consumerSecret, accessToken, and accessTokenSecret all come from your twitter app at https://apps.twitter.com/
\Codebird\Codebird::setConsumerKey("Consumer-Key", "Consumer-Secret");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("Access-Token", "Access-Token-Secret");
//build an array of images to send to twitter
$reply = $cb->media_upload(array(
'media' => $image
));
//upload the file to your twitter account
$mediaID = $reply->media_id_string;
//build the data needed to send to twitter, including the tweet and the image id
$params = array(
'status' => $message,
'media_ids' => $mediaID
);
//post the tweet with codebird
$reply = $cb->statuses_update($params);
}
It's important when you download the API you make sure that cacert.pem
is located in the same directory as codebird.php
which comes with the download. Don't just download codebird.php
Also keep in mind Twitter's guidelines for images and videos relating to sizes and parameters.
Make sure you have at least php version 5.3 and curl enabled on your server. If you are not sure what you have you can create any .php
file and add phpinfo();
and that will tell you everything that your php config has.
Once you have that all in place then all you have to do to send a tweet with codebird is
tweet('This is my sample tweet message','http://www.example.com/image.jpg');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With