Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharing images on twitter using php

<?php
    $title = urlencode('Nature'); 
    $url = urlencode('http://amazingpics.net/content/Nature/Amazing%20Nature%20698.jpg');
    $image = urlencode('http://trainees.ocs.org/training/hariharan/01-09-2014/images/img2.jpg');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sharing Images</title>
<link href="css/share.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="all">
  <div class="top">
  <div class="nature" align="center">
  <p class="nat">I LOVE NATURE</p>
  </div>
  <p>&nbsp;</p>
    <div class="img"><img src="images/img2.jpg" height="250" width="500" /></div>
    <div class="share"><a onClick="window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php echo $title;?>&amp;p[url]=<?php echo $url; ?>&amp;&amp;p[images][0]=<?php echo $image;?>','sharer','toolbar=0,status=0,width=600,height=400');" href="javascript: void(0)"><img src="images/share.png" width="200" height="40" /></a></div>
    <div class="share"><a onClick="window.open('http://twitter.com/intent/tweet?url=<?php echo $url;?>','sharer','toolbar=0,status=0,width=600,height=400');" href="javascript: void(0)"><img src="images/twitter.png" width="200" height="40" /></a></div>
    <p>&nbsp;</p>
  </div>
</div>
</body>
</html>

I tried the above code for sharing images on facebook and twitter. it works correctly in facebook, but the image cannot displayed in twitter. the link only displayed. please help me to share images on twitter in php. thanks in advance...

like image 808
hari krishnan Avatar asked Jun 28 '26 15:06

hari krishnan


1 Answers

Even the code and examples on twitter API documentation is straight forward but It wasn’t easy to figure out the right code with twitter API for tweet images.

To create twitter application you need to do that from : https://dev.twitter.com/

In twitter dev site, you have to specify the name and decryption of your application plus the URL to your main page and the callback page (more on these two page later). Also you have to make sure you set your twitter application access to “Read and Write” in order to give it authorization to tweet images on user behalf.

After the app is created correctly, twitter will provide you with a “Consumer key” and “Consumer secret”, you need to keep these two string variables because they are required to identify your application while communicating with twitter API to tweet images. Download twitter code libraryDownload the Required PHP Libraries

For twitter authentication and image uploading to twitter you need tmhOAuth.php and tmhUtilities.php you can download them from https://github.com/opauth/twitter/tree/master/Vendor/tmhOAuth 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 in steps above) How the code works

i)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"]);

ii). 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/');
setcookie("Temp_Secret", $temp_secret, $time + 3600 * 30, '/twitter_test/'); setcookie("Tweet_Txt", $txt, $time + 3600 * 30, '/twitter_test/');
setcookie("Img_Url", $img, $time + 3600 * 30, '/twitter_test/');

iii). 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;
header("Location:".$ url);
exit();

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

v). 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
     ));
     $tmhOAuth->request("POST", $tmhOAuth->url("oauth/access_token", ""), array( 
      // pass the oauth_verifier received from Twitter 


   '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'];

vi). After we get the correct access token, we tweet the image we want.

$img = './'.$img;
$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
 array(
 'media[]' => "@{$img}",
 'status' => "$txt" 
 ),
 true, // use auth
 true // multipart
 );

vii). The returned code from twitter API will tell us if the operation was done correctly or not.

    if ($code == 200){
      echo '<h1>Your image tweet has been sent successfully</h1>';
      }else{
      tmhUtilities::pr($tmhOAuth->response['response']);
     }
like image 184
Syed Ibrahim Avatar answered Jun 30 '26 05:06

Syed Ibrahim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!