【问题标题】:sharing images on twitter using php使用 php 在 twitter 上分享图片
【发布时间】:2014-09-02 15:10:14
【问题描述】:
<?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 中显示。仅显示链接。请帮我在 twitter 上用 php 分享图片。提前谢谢...

【问题讨论】:

    标签: php facebook twitter


    【解决方案1】:

    即使是 twitter API 文档中的代码和示例也是直截了当的,但要找出正确的 Twitter API 用于推文图像的代码并不容易。

    要创建 twitter 应用程序,您需要从:https://dev.twitter.com/

    在 twitter 开发站点中,您必须指定应用程序的名称和解密以及主页面的 URL 和回调页面(稍后将在这两个页面上详细介绍)。此外,您必须确保将您的 twitter 应用程序访问权限设置为“读写”,以便授权它代表用户发布图像。

    正确创建应用后,twitter 会为您提供“Consumer key”和“Consumer secret”,您需要保留这两个字符串变量,因为在与 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 已在上述步骤中的 App 设置中更新) 代码是如何工作的

    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 中以供用户验证我们的应用并重定向回

    后使用

    “回调.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();
    

    四)。当我们的应用获得授权后,Twitter API 会将用户重定向到应用设置中指定的“callback.php”URL。

    v)。在“callback.php”文件中存在推文图像的实际代码。首先,我们从 cookie 中检索临时访问令牌,并用正确的访问令牌交换它们。

      $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
     );
    

    七)。 twitter API 返回的代码会告诉我们操作是否正确。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-27
      • 2022-07-17
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多