PHP - Post Media / Image to twitter

99封情书 提交于 2020-01-14 22:40:17

问题


I got code changing twitter status, unfortunately it does not posting image.

Please review my code and help me posting image with text.

 $consumerKey    = '';
 $consumerSecret = '';
 $oAuthToken     = '';
 $oAuthSecret    = '';

include "OAuth.php";
 include "twitteroauth.php";
 require_once('twitteroauth.php');
 $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken,      $oAuthSecret);
 $tweet->post('statuses/update', array('status' => 'PHP --> example.co.uk <-- ',  'media[]' => "http://www.balanced-hr.com/wp-     content/uploads/2014/08/majster.jpg"));
 echo "your message has been sent to twitter API";

回答1:


Seems that you should upload your media first (after creating TwitterOAuth object)

$media = $tweet->upload('media/upload', array('media' => 'path_to_your_file'));

Then set the parameters of your tweet (like status and media)

$parameters = array(
'status' => 'Your Tweet status',
'media_ids' => $media->media_id_string);

You can view the docs to upload up to 4 media resources in the same tweet

And then send the tweet with the parameters.

$send_tweet = $connection->post('statuses/update', $parameters);

You can see an example using media here: https://twitteroauth.com/
And the statuses/update reference here: https://dev.twitter.com/rest/reference/post/statuses/update




回答2:


$connection = new TwitterOAuth($consumerkey, $consumersecret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");

check the connection first

$result = $connection->upload('media/upload', array('media' => 'Relative Path to your media'));

$mediaID = $result->media_id;
$parameters = array('status' => 'First tweet','media_ids' => $mediaID);
$response = $connection->post('statuses/update', $parameters);

The above code worked in my instance



来源:https://stackoverflow.com/questions/29897694/php-post-media-image-to-twitter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!