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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里