你能告诉我一些步骤如何在我的网站(php)中使用类似按钮来浏览Facebook上的页面照片?
我知道我必须使用GRAPH API并且必须通过HTTP执行POST到/喜欢..但我不知道如何使用PHP代码执行此操作。
有人有一个例子吗?
谢谢
答案 0 :(得分:5)
只要您获得了用户的publish_stream
权限,就可以获得所需的任何照片。如果您尝试将照片设为页面,请确保该页面有access_token
(通过用户帐户上的/accounts
连接获得)。
获得访问令牌后就像向 HTTP POST 发布类似于此的URL一样简单:
https://graph.facebook.com/PHOTO_ID/likes?access_token=ACCESS_TOKEN
Photo_ID = Facebook中的照片ID
Access_Token =使用publish_stream权限从Facebook获取的访问令牌。
更新
PHP示例代码基于PHP Form CURL Post
<?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 请求。
答案 1 :(得分:2)
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');
}
答案 2 :(得分:1)
您可以通过为登录网址构建一组权限来获取权限。下面我在权限范围内请求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>';
}
?>