加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – update_with_media使用亚伯拉罕的twitteroauth

发布时间:2020-12-13 13:32:40 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用Abraham的twitteroauth库(TwitterOAuth v0.2.0-beta2)从ajax实现upload_with_media请求.我对基本帖子没有任何问题,但是当我尝试包含媒体时,我会将此作为回复: "{"request":"/1/statuses/update_with_media.json","error":"Error creating
我正在尝试使用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的例子吗?我似乎无法弄清楚我做错了什么.

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

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

没有使用UPDATE_WITH_MEDIA

>搜索和搜索后,我找到了相同的库,但有一个修复程序.你可以在这里找到它:
https://github.com/tomi-heiskanen/twitteroauth/tree/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth

基本的不同之处在于原版具有“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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读