来源: https://github.com/abraham/twitteroauth/pull/137
在上述链接中,Github 用户 Robhaswell 对 Abraham 的 TwitterOAuth 代码进行了调整,增加了用于上传图片的 upload
功能。这是对框架的一个很好的补充,但是,新代码没有适当的文档或示例,所以我在使用该功能时遇到了一些麻烦:
$image = 'weather.jpg';
$response = $tweet->upload('statuses/update_with_media', array(
'status' => 'This is a test',
'media[]' => "@{$image};type=image/jpeg;filename={$image}")
);
而 weather.jpg 与包含上述代码的文件位于同一文件夹中。
旁注:代码将通过 cron 执行,并将始终存在于服务器上的图像上传到 Twitter。只是为了澄清,用户不必先上传他们的图片,然后通过此脚本将它们提交到 Twitter。
谁能指出我正确的方向?
编辑:我知道这个功能不是原始构建的一部分,我已经根据 Robhaswell 的调整更新了 twitteroauth 和 OAuth 代码。
最佳答案
由于这个问题仍然相关,但答案现在已经过时,我将继续使用更新的答案更新这个问题。
statuses/update_with_media
已被 Twitter 弃用。 https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update_with_media.html
这是我的工作代码,用于上传和推文带有图片的状态。
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$content = $twitter->get("account/verify_credentials");
$tweet = "My tweet";
$imageMedia = $twitter->upload('media/upload', array('media' => '/path/to/image/image_name.png'));
$parameters = array(
"status" => $tweet,
"media_ids" => $imageMedia->media_id_string);
$statuses = $twitter->post("statuses/update", $parameters);
我只是想把这个贴在这里,因为我在寻找答案时偶然发现了这个问题。
关于twitter - 使用twitteroauth上传图片的格式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13585065/