douhuifen9942 2016-02-11 21:54
浏览 118
已采纳

使用php使用Twitter API发布图像+状态

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);
  • 写回答

1条回答 默认 最新

  • dsgdfg30210 2016-03-11 07:44
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制