Search code examples
phpfacebookfacebook-graph-api

Facebook API, get page post link (PHP)


Here's some code:

$facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
$facebookData = json_decode(curlRequest($facebookUrl))->data;

curlRequest is successfully returning data but it's limited. The response has the following items:

  • message
  • story
  • created_time
  • id

It's bad enough these don't include a photo (which all of these posts do) but what's worse is that I have no link that takes me to the post.

Twitter has a redirect using 'https://twitter.com/statuses/'. $post->id; does Facebook have something similar? Or better yet how do I get all of the data for these posts?


Solution

  • Link to the post would be

    $facebookUrl = 'https://graph.facebook.com/'.$facebookPageId.'/posts?&access_token='.$facebookAppId.'|'.$facebookAppSecret;
    $facebookData = json_decode(curlRequest($facebookUrl))->data;
    
    $link = "http:/fb.com/".$facebookData->id; //This short link will redirect to the pages' post
    

    The $facebookData->id is made up of unique values i.e. the part before "_" (underscore) describes the parent (page,user,group,event) and string after underscore is the post id of its parent.