2012-03-22 8 views
6

मैं पर consumer_key और consumer_secret का उपयोग करके PHP का उपयोग करके लॉगिन कैसे अपलोड कर सकता हूं?PHP पर उपयोग करके ट्विटर पर छवियां अपलोड करें

कृपया मेरी मदद करें & बहुत बहुत धन्यवाद।

+0

दोस्तों अगर इस सवाल का सहायक, आप भी न केवल जवाब वोट दें कर सकते हैं ... –

उत्तर

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

मैं '$ tmhOAuth-> अनुरोध करने के लिए एक' $ headers' चर जोड़ने के लिए किया था() 'कॉल। मैं ट्विटर से 417 त्रुटि वापस प्राप्त कर रहा था, और गुगलिंग ने मुझे मल्टीपार्ट 'सत्य' के बाद 'सरणी' ('expect' => '' ') में चिपकने के लिए प्रेरित किया। यह उसके बाद पूरी तरह से काम किया। –

+1

क्या यह अभी भी काम कर रहा है? –

+0

मैंने आईफोन ऐप के लिए इस्तेमाल किया, यह ऐप स्टोर पर है और हाँ यह अभी भी काम कर रहा है। –

3

वैसे आपके उपयोगकर्ता को आपके एपीपी के साथ ओएथ के साथ अधिकृत होना है, तो आप ट्वीट पोस्ट करने के लिए एपीआई का उपयोग करते हैं। POST statuses/update & POST statuses/update_with_media, के अनुसार, लेकिन मुझे छवि पोस्ट करने में परेशानी थी (लगभग एक साल पहले, शायद वे इसे अब तक तय कर चुके हैं)।

2

आप ऐप को अधिकृत करने के लिए Oauth का उपयोग कर सकते हैं। मुझे this मार्गदर्शिका उपयोगी मिली, क्योंकि यह दिखाता है कि एपीआई से कैसे जुड़ना है, और ट्विटर पर कैसे पोस्ट करें। का उपयोग 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