2014-09-02 70 views
2
<?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上分享图片。在此先感谢...在twitter上使用php共享图像

回答

1

甚至在twitter API文档上的代码和示例也很简单,但用推特图像的twitter API找出正确的代码并不容易。

要创建你需要做的是从Twitter应用程序:https://dev.twitter.com/

在Twitter上开发的网站,你必须指定名称和您的应用程序解密加上网址到你的主页和回调页(更多这两页后面)。此外,您必须确保将您的Twitter应用程序设置为“读取和写入”,以便授权其以用户名义发布图片。

应用程序创建正确后,twitter会为您提供一个“消费者密钥”和“消费者机密”,您需要保留这两个字符串变量,因为它们需要在与Twitter API进行通信时识别您的应用程序图片。 下载Twitter的代码libraryDownload的必需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"]); 

ⅱ)。我们有临时访问令牌之后,我们需要将它们保存在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(); 

ⅳ)。当授权给我们的应用程序时,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']); 
    }