update_with_media使用亚伯拉罕的twitteroauth

时间:2012-05-10 08:42:29

标签: php twitter twitter-oauth

我正在尝试使用Abraham的twitteroauth库(TwitterOAuth v0.2.0-beta2)从ajax实现upload_with_media请求。我对基本帖子没有任何问题,但是当我尝试包含媒体时,我会将此作为回复:

"{"request":"\/1\/statuses\/update_with_media.json","error":"Error creating status."}"

我发布媒体的代码如下:

   $image = $_FILES["media"]["tmp_name"];

    $parameters = array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
        'status'   => $status
      );

    if(isset($reply_id)) {
        $parameters['in_reply_to_status_id'] = $reply_id;
    }
    $post = $twitteroauth->post('https://upload.twitter.com/1/statuses/update_with_media.json', $parameters);
    echo json_encode($post);

我已经验证所有数据都正确地发送到此脚本,甚至设法使用上面的相同数据和tmhOAuth库获取update_with_media帖子,但由于我的其余部分使用了twitteroauth,我更愿意保留事情统一。我也试过它,有没有.json贴在结尾,没有看到任何区别。有人能告诉我一个使用twitteroauth成功实现update_with_media的例子吗?我似乎无法弄清楚我做错了什么。

5 个答案:

答案 0 :(得分:5)

在使用twitteraouth库解决UPDATE_WITH_MEDIA解决方案的几个小时后,我发现以下解决方案正常工作:

  • 首先:从Twitter Dev here链接的PHP原始库不起作用。

没有使用UPDATE_WITH_MEDIA

基本不同之处在于原版具有“post”功能而没有“$ multipart”参数。此参数允许在文档中发送Twiiter要求的内容:多部分enctype帖子。所以最后基本代码如下:

$image_path="folder/image.jpg";

$handle = fopen($image_path,'rb');
$image  = fread($handle,filesize($image_path));
fclose($handle);

$params = array(
  'media[]' => "{$image};type=image/jpeg;filename={$image_path}",
  'status'  => "Put your message here, must be less than 117 characters!"
);
$post = $connection->post('statuses/update_with_media', $params, true);

重要!如果您使用原始库尝试此代码,您将发现错误。您必须从上面的链接下载并替换项目中的两个文件(OAuth.php和twitteroauth.php)。

答案 1 :(得分:4)

尝试使用codebird-php https://github.com/mynetx/codebird-php

事实证明,尽管Twitter在Twitter建议的php库列表中排在最后,但它仍然可以解决问题。只需从git repo中获取codebird.php和cacert.pem。

    include_once('codebird.php');
    \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken($token, $token_secret);
    $status = 'Gamo, I just tweeted with an image!';
    $filename = '/home/asdf/test.png';
    $cb->statuses_updateWithMedia(array('status' => $status, 'media[]' => $filename));

答案 2 :(得分:2)

原始图书馆不包含上传媒体功能尚未。 您可以查看https://github.com/natefanaro/twitteroauth

答案 3 :(得分:0)

我建议您使用Fiddler2或类似的工具来检查和比较与twitteroauth以及tmhOAuth一起发出的消息。你会看到差异。

根据我的经验,这是使用Twitter的Twitter发布的样子 update_with_media {XML,JSON}。我相信你使用的后缀只影响respponse。 (您的应用必须以特定的方式设置授权标头 你的应用。)

您希望twitteroauth发布类似以下内容的内容

POST https://upload.twitter.com/1/statuses/update_with_media.xml HTTP/1.1
Authorization: OAuth oauth_callback="oob", oauth_consumer_key="xxxxxxxxxxxx", oauth_nonce="7774328k", oauth_signature="pUYjRnccmrBYiO1j9cliETsw%2B5s%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318300521", oauth_token="59152613-vrlZ2edX56PudQtBmpAWd3SPDt9cPyAhibO7ysl6W", oauth_version="1.0"
Content-Type: multipart/form-data; boundary=======c49479438c600bf59345e======
Host: upload.twitter.com
Content-Length: 7320
Connection: Keep-Alive

--======c49479438c600bf59345e======
Content-Disposition: form-data; name="status"

working on a Tweet tool that uses the OAuth Manager library.
--======c49479438c600bf59345e======
Content-Disposition: file; name="media[]"; filename="ThisIsAPicture.png"
Content-Type: image/png

  ...binary png data here...

--======c49479438c600bf59345e======--

答案 4 :(得分:0)

我想发送带状态参数的网址链接

喜欢:把信息放在这里