Friday, 4 October 2013

Tweet Images using PHP (Twitter API)

Hi already I'm Expalin about how to create Twitter App ,Today i have worked on Twitter Oauth in this post expalin about how to tweet with images using php with Twitter API.

First we have to Create Twitter App The Twitter App Creation Process Find Here

How the Tweet Images Code Works?
The code for tweet images divided in two files, the first is “start.php” where the code start and a second file “callback.php” where twitter will redirect user back after giving authorization to our app. (the URL to our callback.php file has been updated in App settings at the time of app creation )

1.Edit "config.php" file with your App details
define('API_KEY', '');/*Your Consumer key*/
define('API_SEC','');/*Your Consumer secret*/
2. In “start.php” the first thing we have to do is asking for temporary access token from twitter API using the key and secret that we get them when we create the application (this process call get request token).
$tmhOAuth = new tmhOAuth(array(
    'consumer_key'    => API_KEY,
    'consumer_secret' => API_SEC, 
    'curl_ssl_verifypeer'   => false
   $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''));
   $response = $tmhOAuth->extract_params($tmhOAuth->response["response"]);  

3. After we have the temporary access token we need to save them in cookies for later use after the user authenticate our App and redirected back to “callback.php”.
$temp_token = $response['oauth_token']; 
 $temp_secret = $response['oauth_token_secret']; 
 $time = $_SERVER['REQUEST_TIME'];
 setcookie("Temp_Token", $temp_token, $time + 3600 * 30, 'twitter_test'); 
                                // 'twitter_test' is the cookie path on your server 
 setcookie("Temp_Secret", $temp_secret, $time + 3600 * 30, 'twitter_test'); 
 setcookie("Img_Url", $img, $time + 3600 * 30, 'twitter_test'); 
 setcookie("Tweet_Txt", $txt, $time + 3600 * 30, 'twitter_test');

4. Asking user to give authorization to our app requires a redirect to Twitter API page where user will fill his username and password and complete the authorization process.
$url = $tmhOAuth->url("oauth/authorize", "") . '?oauth_token=' . $temp_token;

5. When authorization is been given to our app, Twitter API will redirect the user to “callback.php” URL specified in App settings.

6. In “callback.php” file the actual code to tweet images exists. First we retrieve the temporary access token from cookies and we exchange them with correct access token.
$token = $_COOKIE['Temp_Token'];
  $secret = $_COOKIE['Temp_Secret'];
  $img = $_COOKIE['Img_Url'];
  $txt = $_COOKIE['Tweet_Txt'];
  $tmhOAuth = new tmhOAuth(array(
   'consumer_key'    => API_KEY,
    'consumer_secret' => API_SEC,
    'user_token'      => $token,
    'user_secret'     => $secret, 
    'curl_ssl_verifypeer'   => false
    /// Ask Twitter for correct access token 

   $tmhOAuth->request("POST", $tmhOAuth->url("oauth/access_token", ""), array('oauth_verifier'    => $_GET["oauth_verifier"])); 
   $response = $tmhOAuth->extract_params($tmhOAuth->response["response"]);
 $tmhOAuth->config["user_token"] = $response['oauth_token'];  
    $tmhOAuth->config["user_secret"] = $response['oauth_token_secret']; 

7. After we get the correct access token, we tweet the image we want.
$img = './'.$img;
   $code = $tmhOAuth->request('POST', '',
    'media[]'  => "@{$img}",
    'status'   => "$txt" // Don't give up..
    true, // use auth
    true  // multipart
8. The returned code from twitter API will tell us if the tweet was done successfully or not.
if ($code == 200){
      // tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
   echo 'Your image tweet has been sent successfully';
      // display the error 
   return tmhUtilities;

Hopefully you find this simple tutorial for tweet images and twitter API helpful.
Please share with me your feedback and comments.

No comments:

Post a Comment