使用 php 通过 Twitter API 发布图像 + 状态 [英] Posting image + status with the Twitter API using php

查看:55
本文介绍了使用 php 通过 Twitter API 发布图像 + 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最终使用了 codebird 而不是 TwitterAPIExchange.php.请看我的回答.

I ended up using codebird and not TwitterAPIExchange.php. Please see my answer.

TwitterAPIExchange.php

我绞尽脑汁想弄清楚为什么我的代码不起作用.我可以很好地向 Twitter 发布状态更新,但是当我尝试添加图像时,它似乎永远不会与状态一起发布.

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.

一件事是,这些帖子中的许多帖子都将 API 调用 url 称为 https://api.twitter.com/1.1/statuses/update_with_media.json,根据 这篇文章 已贬值.

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.

我认为"的新 URL 只是 https://api.twitter.com/1.1/statuses/update.json

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

推荐答案

我最终无法使用此方法并找到了更当前的解决方案.我学到的关于使用 php 发送带有消息的推文图像的一件事是,您必须首先将图像加载到 twitter,其中 API 将返回一个 media_id 给您.media_id 与图像相关联.获得 media_id 后,您就可以将该 ID 与您的消息相关联,并发送带有 media_id 的消息.一旦我了解了这一点,代码就会变得更有意义.

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.

我使用 codebird 来实现用 php 发推文.

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

}

当您下载 API 时,请务必确保 cacert.pem 与下载的 codebird.php 位于同一目录中,这一点很重要.不要只是下载 codebird.php

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

另外牢记 Twitter 的有关图像和视频的指南尺寸和参数.

Also keep in mind Twitter's guidelines for images and videos relating to sizes and parameters.

确保您的服务器上至少有 php 版本 5.3 和 curl 启用.如果你不确定你有什么,你可以创建任何 .php 文件并添加 phpinfo(); ,这会告诉你你的 php 配置所拥有的一切.

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.

一旦你准备好所有这些,你需要做的就是用 codebird 发送一条推文

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

这篇关于使用 php 通过 Twitter API 发布图像 + 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆