2012-03-22 100 views
6

如何使用consumer_keyconsumer_secretTwitter Wall上传图像而不使用PHP登录?使用PHP在Twitter上传图像

请帮我&非常感谢。

+0

家伙,如果这个问题有帮助,你也可以给予好评,不仅答案...... – 2013-03-14 07:26:12

回答

9

那么我得到的答案,下载Twitter Api为php &创建了一个函数。

function image_upload(){  

    define('YOUR_CONSUMER_KEY' , 'your twitter app consumer key'); 
    define('YOUR_CONSUMER_SECRET' , 'your twitter app consumer key secret'); 

    require ('twitt/tmhOAuth.php'); 
    require ('twitt/tmhUtilities.php'); 

    $tmhOAuth = new tmhOAuth(array(
      'consumer_key' => "YOUR_CONSUMER_KEY", 
      'consumer_secret' => "YOUR_CONSUMER_SECRET", 
      'user_token'  => "YOUR_OAUTH_TOKEN", 
      'user_secret'  => "YOUR_OAUTH_TOKEN_SECRET", 
    )); 

    $image = 'image.jpg'; 

    $code = $tmhOAuth->request('POST','https://upload.twitter.com/1/statuses/update_with_media.json', 
     array(
      'media[]' => "@{$image};type=image/jpeg;filename={$image}", 
      'status' => 'message text written here', 
     ), 
     true, // use auth 
     true // multipart 
    ); 

    if ($code == 200){ 
     tmhUtilities::pr(json_decode($tmhOAuth->response['response'])); 
    }else{ 
     tmhUtilities::pr($tmhOAuth->response['response']); 
    } 
    return tmhUtilities; 
} 
+0

我只好一个'$ headers'变量添加到'$ tmhOAuth->请求()'调用。我从Twitter上找回了417错误,谷歌搜索引导我在multipart'true'之后插入'array('Expect'=>'')'。它在那之后完美运作。 – 2012-12-12 00:12:58

+1

这仍然有效吗? – 2013-01-10 07:45:48

+0

我用于iPhone应用程序,这是在应用程序商店和是它仍然工作。 – 2013-01-10 07:47:29

2

您可以使用Oauth授权您的应用程序。 我发现this指南很有帮助,因为它显示了如何连接到API,以及如何在Twitter上发布。 使用update_with_media应该允许你张贴图像

1

update_with_media已过时,应考虑使用以下方法:https://dev.twitter.com/rest/public/uploading-media

使用优秀hybridauth库和更新Twitter.php setUserStatus功能有以下几点,就可以达到你想要的东西:

/** 
* update user status 
* https://dev.twitter.com/rest/public/uploading-media-multiple-photos 
*/ 
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); 

或为全文twitt:

$res = $adapter->setUserStatus("Just text"); 
+0

只为像我这样的人绊倒在这一个,媒体功能已合并一年前;-) https://github.com/hybridauth/hybridauth/blob/master/hybridauth/Hybrid/Providers/Twitter.php#L191 – Can 2015-12-19 08:43:57