update_with_media 使用 abraham 的 twitteroauth [英] update_with_media using abraham's twitteroauth

查看:21
本文介绍了update_with_media 使用 abraham 的 twitteroauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Abraham 的 twitteroauth 库 (TwitterOAuth v0.2.0-beta2) 实现来自 ajax 的 upload_with_media 请求.我对基本帖子没有任何问题,但是当我尝试包含媒体时,我得到了以下回复:

I'm trying to implement an upload_with_media request from ajax using Abraham's twitteroauth library (TwitterOAuth v0.2.0-beta2). I've had no problems with basic posts but when I try to include media I get this as a response:

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

我发布媒体的代码如下所示:

My code for posting media looks like this:

   $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 和不附上 .json 并没有看到任何区别.谁能给我看一个使用 twitteroauth 成功实现 update_with_media 的例子?我似乎无法弄清楚我做错了什么.

I have verified that all data is being sent to this script correctly and even managed to get an update_with_media post working using the same data above and the tmhOAuth library but as the rest of my widget uses twitteroauth, I'd prefer to keep things uniform. I've also tried it with and without the .json affixed to the ending and saw no difference. Can anyone show me an example of a successful implementation of update_with_media using twitteroauth? I can't seem to figure out what I'm doing wrong.

推荐答案

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

After dealing during hours for a solution to UPDATE_WITH_MEDIA with twitteraouth library, I found the following solution that works fine:

  • 首先:从 Twitter Dev 此处链接的 PHP 原始库不起作用.
  • First: the PHP original library linked from Twitter Dev here does not work.

不适用于 UPDATE_WITH_MEDIA

IS NOT WORKING WITH UPDATE_WITH_MEDIA

基本的区别是原来有函数post",没有$multipart"参数.此参数允许发送 Twiiter 在文档中要求的内容:多部分 enctype 帖子.所以最后的基本代码如下:

The basic diference is that the original has the function "post" without "$multipart" parameter. This parameter is what allows to send what Twiiter ask for in the documentation: a multipart enctype post. So at the end the basic code is as follows:

$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).

IMPORTANT! If you try this code with the original library, you will find out an error. You have to download from the link above and replace both files (OAuth.php and twitteroauth.php) in your project.

这篇关于update_with_media 使用 abraham 的 twitteroauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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