添加像评论问题使用图表api [英] adding like comment issue using graph api
问题描述
CODE:
$ facebook = new Facebook(array(
'appId'=>'',
'secret'=>'',
'cookie'=> true,
));
$ user = $ facebook-> getUser();
if($ user){
if(session_id()){
} else {
session_start();
}
$ access_token = $ facebook-> getAccessToken();
//检查权限列表
$ permissions_list = $ facebook-> api(
'/ me / permissions','GET',array(
'access_token '=> $ access_token
)
);
//检查我们需要的权限是否已被用户允许
//如果没有,然后将它们重定向到Facebook的权限页面
$ permissions_needed = array ('publish_stream','read_stream','manage_pages');
foreach($ permissions_needed as $ perm){
if(!isset($ permissions_list ['data'] [0] [$ perm])|| $ permissions_list ['data'] [0] [$ perm]!= 1){
$ login_url_params = array(
'scope'=>'publish_stream,read_stream,manage_pages',
'fbconnect'=> 1 ,
'display'=>page,
'redirect_uri'=>'http://localhost/fb/index.php',
);
$ login_url = $ facebook-> getLoginUrl($ login_url_params);
header(Location:{$ login_url});
exit();
}
}
} else {
//如果没有,让我们重定向到ALLOW页面,以便我们可以访问
//创建一个登录URL使用Facebook库的getLoginUrl()方法
$ login_url_params = array(
'scope'=>'publish_stream,read_stream,manage_pages',
'fbconnect'=> 1,
'display'=>page,
'redirect_uri'=>'http://localhost/fb/index.php',
);
$ login_url = $ facebook-> getLoginUrl($ login_url_params);
//重定向到Facebook上的登录URL
header(Location:{$ login_url});
exit();
}
$ logoutUrl = $ facebook-> getLogoutUrl();
如果用户点击按钮:
jQuery('ul.fb_list a')。click(function(){
var comm_id = jQuery(this) .attr(class);
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token:< ?php echo $ access_token?>
});
});
双重检查并确保您的变量comm_id形成发布帖子的人的用户ID,然后发送本身的id,其间有一个下划线,就像这样 - USERID_POSTID。这是Facebook如何调用图形API给您的方式。
$ post_url =https://graph.facebook。 com /。 $ user_id。 /帖子?=的access_token。进行urlencode($的access_token);
不知道你是否从另一个来源获取comm_id。在你的代码中也注意到
access_token:<?php echo $ access_token?>
回呼后忘了分号。应该是这个
access_token:<?php echo $ access_token;?>
希望这有帮助。我看到你正在使用很多jQuery来执行类似的功能。我喜欢粘贴w /服务器端代码为这样的东西,我觉得它是更稳定的某些原因。
这是我做的。我有一个像这样的按钮 -
echo'< div id =like_container - '。$ i。'> ;
< div id =like_count>'。$ num_likes。'< / div>'
< a href =javascript:void()onclick =fb_Like(\' '。$ post_id.'\',\''$ access_token.'\','$ num_likes。','。$ i。')>< div class =unliked> < / DIV>< / A>
< / div>';
和fb_Like()是一个ajax调用,这样的 -
函数fb_Like(post_id,token,num_likes,id){
$ .ajax({
type:GET,
url:likepost.php,
data:'id ='+ post_id +'& token ='+ token +'& likes ='+ num_likes,
success:function(html)
{
$(#like_container - + id).empty()。html(html);
}
});
}
而且,likepost.php页面是类似于此页面的脚本
像外部使用Graph Api的Facebook发布 - 例子
这对我来说很好。它也让我更新的帖子喜欢的数量在前面的正面上方的像按钮,如果已经做了。祝你好运!
更新
如果你想检查用户是否喜欢帖子,这很简单w / the facebook graph api
//创建发布网址
$ post_url =https://graph.facebook .com /。 $ Page / User_id。 /帖子?=的access_token。进行urlencode($的access_token);
//获取Json内容
$ resp = file_get_contents($ post_url,0,null,null);
//将Post Entries作为Array
$ the_posts = json_decode($ resp,true);
foreach($ the_posts ['data'] as $ postdata){
foreach($ postdata ['likes'] ['data'] as $ like){
if($ like ['id'] == $ user){
$ likes = 1;
} else {continue;}
}
if($ likes == 1){
// do something
}
}
这假设您已经有一个Facebook用户标识在用户中,在本示例中,存储在变量$ user中。
i have put last 3 posts in web page , if user loged in he can add comment / like , problem is if user loged in and try add like or comment he is getting an permission error #200 , if i loged in i can add like or comment (application is for me) , am getting the all permission nedded from the user , so how can i give him permission to add like / comment.
CODE :
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
if (session_id()) {
} else {
session_start();
}
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions', 'GET', array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'read_stream', 'manage_pages');
foreach ($permissions_needed as $perm) {
if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri' => 'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
}else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri'=>'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
$logoutUrl = $facebook->getLogoutUrl();
and if the user click on like button :
jQuery('ul.fb_list a').click(function(){
var comm_id = jQuery(this).attr("class");
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token : "<?php echo $access_token ?>"
});
});
Double check and make sure your variable comm_id is formated with both the user id of the person who posted the post, and then the id of the post itself, with an underscore in between, like this - USERID_POSTID. This is how facebook gives it you if you call to the graph api
$post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token);
Not sure if you're getting comm_id from another source or not. Also noticed in your code
access_token : "<?php echo $access_token ?>"
You forgot a semi-colon after the echo call. Should be this
access_token : "<?php echo $access_token; ?>"
Hope this helps. I see you're using a lot of jquery to do the like functionality. I like to stick w/ server side code for stuff like this, I feel that it's more stable for some reason.
This is how I did it. I have a like button like this -
echo '<div id="like_container-'.$i.'">
<div id="like_count">'.$num_likes.'</div>'
<a href="javascript:void()" onclick="fb_Like(\''.$post_id.'\',\''.$access_token.'\','.$num_likes.','.$i.')"><div class="unliked"></div></a>
</div>';
and fb_Like() is an ajax call, something like this -
function fb_Like(post_id, token, num_likes, id){
$.ajax({
type: "GET",
url: "likepost.php",
data: 'id='+post_id+'&token='+token+'&likes='+num_likes,
success: function(html)
{
$("#like_container-"+id).empty().html(html);
}
});
}
And the likepost.php page is a script similar to the one on this page
Like a Facebook Post externally using Graph Api - example
This worked really well for me. It also let's me update the number of likes that the post has on the front end right above the like button if a like has been made. Good luck!
UPDATE
If you want to check if the user already likes a post, it's pretty simple w/ the facebook graph api
//Create Post Url
$post_url = "https://graph.facebook.com/" . $Page/User_id . "/posts?access_token=". urlencode($access_token);
//Get Json Contents
$resp = file_get_contents($post_url,0,null,null);
//Store Post Entries as Array
$the_posts = json_decode($resp, true);
foreach ($the_posts['data'] as $postdata) {
foreach ($postdata['likes']['data'] as $like){
if($like['id']==$user){
$liked=1;
}else{continue;}
}
if($liked==1){
//do something
}
}
This assumes that you already have a facebook user id for the logged in user, in this example, stored in the variable $user.
这篇关于添加像评论问题使用图表api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!