Adding a comment question using api chart

I put the last 3 messages on the web page, if the user is logged in, he can add a comment / like, the problem is that the user logged in and tried to add as a comment or comment, he gets permission error # 200, if I logged in I can add to the system as a comment or comment (application for me), I get all permissions from the user, since I can give him permission to add as / comment.

THE 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 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 redirect to the ALLOW page so we can get access //Create a login URL using the Facebook library 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 clicks on the 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 ?>" }); }); 
0
facebook-graph-api
source share
1 answer

Double check and make sure that the variable comm_id is generated with both the user ID of the person who posted the message and the identifier of the message itself, with an intermediate sign, for example, USERID_POSTID. This is how facebook gives you if you call on the api chart

 $post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token); 

Not sure if you get comm_id from another source or not. Also noticed in your code

 access_token : "<?php echo $access_token ?>" 

After the echo, you forgot the half-column. It must be

  access_token : "<?php echo $access_token; ?>" 

Hope this helps. I see that you use a lot of jquery to perform similar functions. I like to stick with the w / server side code for such things, I feel that for some reason it is more stable.

Here is how I did it. I have a similar 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 likepost.php page is a script similar to the one on this page

How Facebook Post externally using Graph Api - example

This worked very well for me. It also allows me to update the number of likes that the column has on the front side to the right above a similar button, if it was done. Good luck

UPDATE

If you want to check if the user likes the message, it is quite simple with the facebook api schedule

 //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 the facebook user ID for the registered user, in this example it is stored in the $ user variable.

0
source share

Source: https://habr.com/ru/post/650451/


All Articles