php – update_with_media使用亚伯拉罕的twitteroauth

前端之家收集整理的这篇文章主要介绍了php – update_with_media使用亚伯拉罕的twitteroauth前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Abraham的twitteroauth库(TwitterOAuth v0.2.0-beta2)从ajax实现upload_with_media请求.我对基本帖子没有任何问题,但是当我尝试包含媒体时,我会将此作为回复
  1. "{"request":"\/1\/statuses\/update_with_media.json","error":"Error creating status."}"

我发布媒体的代码如下:

  1. $image = $_FILES["media"]["tmp_name"];
  2.  
  3. $parameters = array(
  4. 'media[]' => "@{$image};type=image/jpeg;filename={$image}",'status' => $status
  5. );
  6.  
  7. if(isset($reply_id)) {
  8. $parameters['in_reply_to_status_id'] = $reply_id;
  9. }
  10. $post = $twitteroauth->post('https://upload.twitter.com/1/statuses/update_with_media.json',$parameters);
  11. 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帖子.所以最后基本代码如下:

  1. $image_path="folder/image.jpg";
  2.  
  3. $handle = fopen($image_path,'rb');
  4. $image = fread($handle,filesize($image_path));
  5. fclose($handle);
  6.  
  7. $params = array(
  8. 'media[]' => "{$image};type=image/jpeg;filename={$image_path}",'status' => "Put your message here,must be less than 117 characters!"
  9. );
  10. $post = $connection->post('statuses/update_with_media',$params,true);

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

猜你在找的PHP相关文章