2014-07-03 73 views
0

我有以下的工作代碼,代碼只是發佈狀態和圖像om我的推特頁面。發佈狀態+圖片上有推特TwitterAPIExchange

 require_once('TwitterAPIExchange.php'); 

     $settings = array(
      'oauth_access_token' => "xxx", 
      'oauth_access_token_secret' => "xxx", 
      'consumer_key' => "xxx", 
      'consumer_secret' => "xxx" 
     ); 

     $url = "https://api.twitter.com/1.1/statuses/update_with_media.json"; 
     $requestMethod = "POST"; 

     $tweetmsg = $_POST['post_text']; 
     $twimage = "Images/twitter-icon.png"; 

     $postfields = array(
      'status' => $tweetmsg, 
      'media[]' => "@{$twimage}" 
     ); 
     try { 
      $twitter = new TwitterAPIExchange($settings); 
      $twitter->buildOauth($url, $requestMethod) 
        ->setPostfields($postfields) 
        ->performRequest(); 

      echo "Success, you just tweeted!"; 
     } catch (Exception $ex) { 
      echo $ex->getMessage(); 
     } 
    } 

該圖像現在被放置在我的項目中的文件夾(圖像)。我希望用戶能夠從他自己的計算機中選擇一張圖片,寫入一個描述,然後發送它。我有以下簡單的HTML表單發佈:

<form method="post" action="index.php"> 
<textarea id="post_text" name="post_text" type="text"></textarea> 
<input type="file" name="post_file" id="post_file" multiple accept="image/*" value="Choose a file"/> 
<button type="submit" id="btn_submit">Submit</button> 
</from> 

那麼,你們有任何提示或指南,可以幫助我解決我的問題?我是以正確的方式思考,還是應該以另一種方式解決問題?謝謝!

回答

0

這是比我想象的要容易得多,我只是用$ _FILES到圖像存儲在一個變量,這樣我可以在$ postfields數組中使用它。

$url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json"; 
$requestMethod = "POST"; 

$tweetmsg = $_POST['post_description']; 
$twimg = $_FILES['pictureFile']['tmp_name']; 

    $postfields = array(
     'status' => $tweetmsg, 
     'media[]' => '@' . $twimg 
    ); 
    try { 
     $twitter = new TwitterAPIExchange($settings); 
     $twitter->buildOauth($url_media, $requestMethod) 
       ->setPostfields($postfields) 
       ->performRequest(); 

     echo "You just tweeted with an image"; 
    } catch (Exception $ex) { 
     echo $ex->getMessage(); 
    } 
0

您必須實現小幅邏輯這裏 - 現在對提交的點擊就以下 1.store在你的項目的一些文件夾中的圖像(請參閱下面的代碼來存儲圖像的文件夾中)

upload_file.php //在此處上傳文件代碼

2.存儲文本並將圖像名稱上傳到數據庫中。 3.現在,您在圖像文件夾中有圖像...並且您的數據庫中還有圖像名稱和相應的消息。

4. $ tweetmsg = $ _POST ['post_text']; $ twimage =「Images/twitter-icon.png」; //在此之前

retrive the last inserted row and fetch message and image name 

    $tweetmsg = $row['msg']; 
    $image = $row['image_name']; 
    $twimage = "Images/".$image; 

我希望這將是對你的工作..謝謝

+0

而且您的網址,HTTPS://api.twitter.com/1.1/statuses/ update_with_media.json,是錯誤的。它應該是:https://upload.twitter.com/1.1/statuses/update_with_media.json –

+0

感謝您的提示,我會嘗試一下! – user2190027

3

接受的答案使用折舊API端點https://dev.twitter.com/rest/reference/post/statuses/update_with_media

這裏是一個有效的解決方案:

// send image to Twitter first 
$url = 'https://upload.twitter.com/1.1/media/upload.json'; 
$requestMethod = 'POST'; 

$image = 'full/path/to/image.jpg'; 

$postfields = array(
    'media_data' => base64_encode(file_get_contents($image)) 
); 

$response = $twitter->buildOauth($url, $requestMethod) 
    ->setPostfields($postfields) 
    ->performRequest(); 

// get the media_id from the API return 
$media_id = json_decode($response)->media_id; 

// then send the Tweet along with the media ID 
$url = 'https://api.twitter.com/1.1/statuses/update.json'; 
$requestMethod = 'POST'; 

$postfields = array(
    'status' => 'My amazing tweet' 
    'media_ids' => $media_id, 
); 

$response = $twitter->buildOauth($url, $requestMethod) 
    ->setPostfields($postfields) 
    ->performRequest(); 
+0

這應該是接受答案下的評論,或者如果您有其他更新的答案,您應該編輯幷包含上下文。 –

+0

我以爲我會鏈接到另一篇文章,以避免重複,但在這裏你去。 –

+0

如果答案適用,您應該將該問題標記爲重複。 (不是downvoter,順便說一句) –