【问题标题】:PHP - Post Media / Image to twitterPHP - 将媒体/图像发布到 twitter
【发布时间】:2015-07-06 00:30:47
【问题描述】:

我得到了改变推特状态的代码,不幸的是它没有发布图片。

请检查我的代码并帮助我发布带有文字的图片。

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

【问题讨论】:

    标签: php twitter


    【解决方案1】:

    看来你应该先上传你的媒体(在创建 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返回的日志?
    • 不工作 gettin error 语法错误,意外的 ''media_ids'' (T_CONSTANT_ENCAPSED_STRING),期待 ']'
    • $media = $connection->upload('media/upload', array('media' => 'gettyimages.in/gi-resources/images/Embed/new/embed2.jpg')); $parameters = [ 'status' = >“tweet” 'media_ids' = > $media->media_id_string ];
    • 也许是数组中的逗号? ['status' => "tweet" , 'media_ids' => $media->
    • 不要删除,标记一定有,我回家试试你的代码:)
    【解决方案2】:
    $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);
    

    上面的代码在我的实例中工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 2019-12-12
      • 2014-09-28
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      相关资源
      最近更新 更多