2014-09-02 73 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']); 
    }