2011-09-22 145 views
0

如何在twitter中使用php共享图像?我知道它使用Twitter的图片上传api。但我不知道如何实现这一点?请帮忙找到解决办法。提前感谢。Twitter图像共享

回答

3

以下是关于twitter图片共享的文档。

https://dev.twitter.com/docs/api/1/post/statuses/update_with_media

下面是支持Twitter图片上传唯一的PHP库。

https://github.com/themattharris/tmhOAuth

和这里的图像共享工作的例子。一对夫妇编辑你的套餐。

<?php 

if ($_POST['message'] && $_FILES['image']['tmp_name']) 
     { 
       require 'tmhOAuth.php'; 

       list($oauth_token, $oauth_token_secret) = explode('|', $GLOBALS['user']['password']); 

       $tmhOAuth = new tmhOAuth(array(
         'consumer_key' => OAUTH_CONSUMER_KEY, 
         'consumer_secret' => OAUTH_CONSUMER_SECRET, 
         'user_token'  => $oauth_token, 
         'user_secret'  => $oauth_token_secret, 
       )); 

       $image = "{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}"; 

       $code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json', 
                          array(
                           'media[]' => "@{$image}", 
                           'status' => " " . $status //A space is needed because twitter b0rks if first char is an @ 
                         ), 
                          true, // use auth 
                          true // multipart 
                       ); 

       if ($code == 200) { 
         $json = json_decode($tmhOAuth->response['response']); 

         if ($_SERVER['HTTPS'] == "on") { 
           $image_url = $json->entities->media[0]->media_url_https; 
         } 
         else { 
           $image_url = $json->entities->media[0]->media_url; 
         } 

         $text = $json->text; 

         $content = "<p>Upload success. Image posted to Twitter.</p> 
                 <p><img src=\"" . IMAGE_PROXY_URL . "x50/" . $image_url . "\" alt='' /></p> 
                 <p>". twitter_parse_tags($text) . "</p>"; 

       } else { 
         $content = "Damn! Something went wrong. Sorry :-(" 
           ."<br /> code=" . $code 
           ."<br /> status=" . $status 
           ."<br /> image=" . $image 
           ."<br /> response=<pre>" 
           . print_r($tmhOAuth->response['response'], TRUE) 
           . "</pre><br /> info=<pre>" 
           . print_r($tmhOAuth->response['info'], TRUE) 
           . "</pre><br /> code=<pre>" 
           . print_r($tmhOAuth->response['code'], TRUE) . "</pre>"; 
       } 
     } ?> 
+0

谢谢!这个工作做得很好,稍微调整了一下,但是请注意,它不适用于新的Twitter API 1.1端点。 –

+0

更新:得到它与API v.1.1一起工作,对于我以前的评论感到抱歉,您需要将端点url更改为https://api.twitter.com/1.1/statuses/update_with_media.json –

0

还有另外一个库(它分叉),可以用于图像上传,请参阅:TwitterOauth

  1. 下载:Oauth File
  2. 使用代码:
$path = '/absolute/path/to/background.jpg'; 
$meta = getimagesize($path); 
$raw['image'] = $path.';type='.$meta['mime']; 
$param['tile'] = 'true'; 
$status = $connection->post('account/update_profile_background_image',$param,$raw); 
-1

最初我在提供媒体文件路径时遇到了同样的错误。实际上你需要提供字节读取。因此请设置媒体参数如下所示,将工作:

$filename = "image.jpg"; 
$handle = fopen($filename, "rb"); 
$image = fread($handle, filesize($filename)); 
fclose($handle); 


$params = array('media[]' => "{$image};type=image/jpeg;filename={$filename}" , 
      'status' => 'my status'); 
$response =$twitteroauth->post('statuses/update_with_media', $params,true,true); 

您可以访问此链接,完整的参考http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/