使用PHP在Twitter上分享图片

3
<?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>

我尝试了上面的代码来在Facebook和Twitter上分享图片。在Facebook上它可以正常工作,但是在Twitter上无法显示图片,只显示链接。请帮助我在PHP中分享图片到Twitter。提前感谢...

1个回答

1
即使Twitter API文档上的代码和示例很简单,但要找到正确的代码来使用Twitter API发布推文图片并不容易。
要创建Twitter应用程序,您需要从https://dev.twitter.com/进行操作。
在Twitter开发网站上,您必须指定应用程序的名称和描述,以及主页和回调页的URL(稍后会详细介绍这两个页面)。此外,您必须确保将Twitter应用程序访问权限设置为“读写”,以授权其代表用户发布推文图片。
正确创建应用程序后,Twitter将为您提供“使用者密钥”和“使用者密钥密码”,您需要保留这两个字符串变量,因为它们在与Twitter API通信以发布推文图片时是必需的。 下载Twitter代码库下载所需的PHP库
要进行Twitter身份验证和上传图像到Twitter,您需要tmhOAuth.php和tmhUtilities.php。您可以从https://github.com/opauth/twitter/tree/master/Vendor/tmhOAuth下载它们。
推文图片代码如何工作?

推文图片的代码分为两个文件,“start.php”是代码起点,第二个文件“callback.php”是Twitter在用户授权我们的应用程序后将用户重定向回来的地方。(在上面的步骤中,我们已经更新了应用程序设置中的callback.php文件的URL)

i)在“start.php”中,我们首先要做的是使用我们创建应用程序时获得的密钥和密码向Twitter API请求临时访问令牌(此过程称为获取请求令牌)。

$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). 在获得临时访问令牌后,我们需要将它们保存在cookie中以便在用户验证我们的应用程序并重定向回来后进行后续使用。

“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). 要求用户授权我们的应用程序需要重定向到 Twitter API 页面,用户将在该页面填写他的用户名和密码并完成授权过程。
$url = $tmhOAuth->url("oauth/authorize", "") . '?oauth_token=' . $temp_token;
header("Location:".$ url);
exit();

iv). 当我们的应用程序被授权时,Twitter API将重定向用户到App设置中指定的“callback.php” URL。

v). 在“callback.php”文件中,实际的推文图片代码存在。首先,我们从cookies中检索临时访问令牌,然后将其与正确的访问令牌交换。

  $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)。在获取正确的访问令牌后,我们会发布我们想要的图像。
$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)从 Twitter API 返回的代码将告诉我们操作是否正确完成。

    if ($code == 200){
      echo '<h1>Your image tweet has been sent successfully</h1>';
      }else{
      tmhUtilities::pr($tmhOAuth->response['response']);
     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接