PHP - 将媒体/图片发布到Twitter

4

我写了可以改变 Twitter 状态的代码,但是很遗憾这个代码无法发布图片。

请检查我的代码并帮助我在发布文字的同时发布图片。

 $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";
2个回答

6
似乎您应该先上传媒体(在创建TwitterOAuth对象之后)才能进行操作。
$media = $tweet->upload('media/upload', array('media' => 'path_to_your_file'));

然后设置您的推文参数(例如状态和媒体)
$parameters = array(
'status' => 'Your Tweet status',
'media_ids' => $media->media_id_string);

您可以查看文档,了解如何在同一条推文中上传最多4个媒体资源。

然后使用参数发送推文。

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

您可以在这里查看使用媒体的示例:https://twitteroauth.com/
并查看状态/更新参考文献:https://dev.twitter.com/rest/reference/post/statuses/update

你能发布API返回的日志吗? - iJos
不起作用,出现语法错误,意外的''media_ids''(T_CONSTANT_ENCAPSED_STRING),期望']'。 - rupesh
$media = $connection->upload('media/upload', array('media' => 'http://www.gettyimages.in/gi-resources/images/Embed/new/embed2.jpg'));$parameters = [ 'status' => "推文", 'media_ids' => $media->media_id_string ]; - rupesh
也许是数组中的逗号引起的问题? ['status' => "tweet" , 'media_ids' => $media-> - iJos
不要移除它,标记必须在那里,我回家后会尝试你的代码 :) - iJos

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

首先检查连接

$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);

上述代码在我的实例中运行良好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接