adding like comment issue using graph api

前端 未结 1 585
北恋
北恋 2021-01-28 09:20

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

相关标签:
1条回答
  • 2021-01-28 09:38

    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.

    0 讨论(0)
提交回复
热议问题