[英]Like button to a page photo
你能告訴我步驟如何在我的網站(php)中做一個類似按鈕,在Facebook上喜歡頁面照片?
我知道我必須使用GRAPH API並且必須通過HTTP來執行POST到/喜歡..但我不知道如何使用PHP代碼執行此操作。
有人有一個例子嗎?
謝謝
只要您從用戶那里獲得了publish_stream
權限,就可以獲得所需的任何照片。 如果您嘗試將照片視為頁面,請確保您擁有該頁面的access_token
(通過用戶帳戶上的/accounts
連接獲得)。
獲得訪問令牌之后,就像向類似於此的URL發出HTTP POST一樣簡單:
https://graph.facebook.com/PHOTO_ID/likes?access_token=ACCESS_TOKEN
Photo_ID = Facebook中的照片ID
Access_Token =使用publish_stream權限從Facebook獲取的訪問令牌。
UPDATE
基於PHP Form CURL Post的 PHP示例代碼
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/PHOTO_ID/likes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'Access_Token' => 'token_value'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
我會檢查這個,因為我不確定它是多么准確,因為我通常不編碼PHP。 無論如何,帖子應該是原始的HTTP POST請求。
Fabio這里是我能夠工作的php帖子片段。 Snippet包括一個curl來獲取應用程序訪問令牌,api帖子包含一個對象,在這種情況下是我的應用程序上的一個帖子。
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
if(substr($url,0,8)=='https://'){
// The following ensures SSL always works. A little detail:
// SSL does two things at once:
// 1. it encrypts communication
// 2. it ensures the target party is who it claims to be.
// In short, if the following code is allowed, CURL won't check if the
// certificate is known and valid, however, it still encrypts communication.
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
$app_access_token = GetCH();
if($_GET['postid']){
$postid = $_GET['postid'];
}else{
$postid = '135669679827333_151602784936066';
}
if($user){
$pageLike = $facebook->api('/'.$postid.'/likes?access_token='.$access_token.'&method=post', 'POST');
}
您可以通過為登錄URL構建一組權限來獲取權限。 下面我在權限范圍內請求read_stream,publish_stream,publish_actions,offline_access。
注意:注銷網址需要應用訪問令牌。
<?php
$url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APP-SECRET',
'cookie' => true, // enable optional cookie support
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
//$pageInfo = $facebook->api('/'.$pageid.'?access_token='.$_SESSION['fb_112104298812138_access_token].');
//$pageInfoUser = $user_profile[id];
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
/* */
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
scope => 'read_stream,publish_stream,publish_actions,offline_access',
redirect_uri => $url
);
$loginUrl = $facebook->getLoginUrl($params);
}
$access_token = $_SESSION['fb_135669679827333_access_token'];
?>
<?php
if(!$user){
echo ' : <a href="'.$loginUrl.'" target="_self">Login</a> ';
}else{
echo '<a href="'.$logoutUrl.'?'.$app_access_token.'" target="_blank">Logout</a>';
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.