使用twitteroauth上传图片的格式是什么?

问题描述 投票:1回答:2

资料来源:https://github.com/abraham/twitteroauth/pull/137

在上述链接中,Github用户Robhaswell对亚伯拉罕的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代码。

twitter oauth twitter-oauth image-uploading
2个回答
4
投票

由于这个问题仍然存在,但答案现已过时,我将继续使用更新的答案更新此问题。

statuses/update_with_media已被推特弃用。 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);

我只是觉得我会在这里发帖,因为我在寻找答案的时候偶然发现了这个问题。


3
投票

由于我一直在努力解决这个问题,我认为回答这个问题可能对其他人有所帮助。这使用了亚伯拉罕的twitteroauth添加的图像上传功能(code can be found here

您应该检查图像路径是否正确。它必须指向服务器上的文件(相对)。

另外,确保twitteroauth文件中的主机正确(请参阅upload函数,它会短暂更改主机URL,这是不再需要的)。你现在应该使用

https://api.twitter.com/1.1/

代替

https://upload.twitter.com/1/

在代码之后记住这两件事应该有效(对我而言);

$this->api = new TwitterOAuth($consumerKey, $consumerSecret,$token, $token_secret);    
$attachment = "./images/img.jpg";
$image = "@{$attachment};type=image/jpeg";
$status = "Text";
$result = $this->api->upload('statuses/update_with_media',array('status'=>$message,'media[]'=>$image));
© www.soinside.com 2019 - 2024. All rights reserved.