2013-11-21 34 views
0

我正在使用HybridAuth庫。 我希望能夠發佈消息到我的經過身份驗證的用戶twitter個人資料的圖像。HybridAuth發送帶圖像的鳴叫

setUserStatus方法很適合自動發送推文。

我寫了下面的方法:

function setUserStatus($status, $image) 
{ 
    //$parameters = array('status' => $status, 'media[]' => "@{$image}"); 
    $parameters = array('status' => $status, 'media[]' => file_get_contents($image)); 
    $response = $this->api->post('statuses/update_with_media.json', $parameters); 

    // check the last HTTP status code returned 
    if ($this->api->http_code != 200){ 
     throw new Exception("Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus($this->api->http_code)); 
    } 
} 

我從Twitter獲得的消息是:

Ooophs,我們得到了一個錯誤:更新用戶狀態失敗! Twitter返回一個錯誤。 403禁止:請求被理解,但它已被拒絕。

如何獲得有關錯誤的更精確信息? 是否有人已經成功發送附在推文上的圖片?

謝謝!

雨果

+0

嘿,你有沒有解決這個問題? –

+0

不!當我將要處理它時,我將直接使用twitter API ... – hugsbrugs

回答

3

感謝@ Heena爲了讓自己在這個問題上醒來,我做了它;)

function setUserStatus($status) 
{ 
    if(is_array($status)) 
    { 
     $message = $status["message"]; 
     $image_path = $status["image_path"]; 
    } 
    else 
    { 
     $message = $status; 
     $image_path = null; 
    } 

    $media_id = null; 

    # https://dev.twitter.com/rest/reference/get/help/configuration 
    $twitter_photo_size_limit = 3145728; 

    if($image_path!==null) 
    { 
     if(file_exists($image_path)) 
     { 
      if(filesize($image_path) < $twitter_photo_size_limit) 
      { 
       # Backup base_url 
       $original_base_url = $this->api->api_base_url; 

       # Need to change base_url for uploading media 
       $this->api->api_base_url = "https://upload.twitter.com/1.1/"; 

       # Call Twitter API media/upload.json 
       $parameters = array('media' => base64_encode(file_get_contents($image_path))); 
       $response = $this->api->post('media/upload.json', $parameters); 
       error_log("Twitter upload response : ".print_r($response, true)); 

       # Restore base_url 
       $this->api->api_base_url = $original_base_url; 

       # Retrieve media_id from response 
       if(isset($response->media_id)) 
       { 
        $media_id = $response->media_id; 
        error_log("Twitter media_id : ".$media_id); 
       } 

      } 
      else 
      { 
       error_log("Twitter does not accept files larger than ".$twitter_photo_size_limit.". Check ".$image_path); 
      } 
     } 
     else 
     { 
      error_log("Can't send file ".$image_path." to Twitter cause does not exist ... "); 
     } 
    } 

    if($media_id!==null) 
    { 
     $parameters = array('status' => $message, 'media_ids' => $media_id); 
    } 
    else 
    { 
     $parameters = array('status' => $message); 
    } 
    $response = $this->api->post('statuses/update.json', $parameters); 

    // check the last HTTP status code returned 
    if ($this->api->http_code != 200){ 
     throw new Exception("Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus($this->api->http_code)); 
    } 
} 

爲了使它工作,你需要做的是這樣的:

$config = "/path_to_hybridauth_config.php"; 
$hybridauth = new Hybrid_Auth($config); 
$adapter = $hybridauth->authenticate("Twitter"); 

$twitter_status = array(
    "message" => "Hi there! this is just a random update to test some stuff", 
    "image_path" => "/path_to_your_image.jpg" 
); 
$res = $adapter->setUserStatus($twitter_status); 

享受!

2

我不明白它hybridauth然後我用這個庫 https://github.com/J7mbo/twitter-api-php/archive/master.zip

然後我成功了使用下面的代碼:(在其他地方出現在堆棧)

<?php 
    require_once('TwitterAPIExchange.php'); 
    $settings= array(
    'oauth_access_token' => ''; 
    'oauth_access_secret' => ''; 
    'consumer_key' => ''; 
    'consumer_secret' => ''; 
    // paste your keys above properly 
    ) 

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

    $tweetmsg = $_POST['post_description']; //POST data from upload form 
    $twimg = $_FILES['pictureFile']['tmp_name']; // POST data of file upload 

    $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

來硬編碼消息和圖像,像 $ tweetmsg =「查看我的新照片」; $ twimg =「catfight.jpg」; –

+0

嗨update_with_media.json已棄用[Twitter的API](https://dev.twitter.com/rest/reference/post/statuses/update_with_media),你應該考慮使用media/upload.json的新方法,然後狀態/更新。 JSON – hugsbrugs