2011-09-22 188 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/