'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 1:[1]

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.

Solution 2:[2]

Adding to Nishant's helpful answer above, Facebook has a lot of other data you can get as part of the Post object, as outlined in their docs. By default, though, all it sends is

  • message
  • story
  • created_time
  • id

In order to retrieve any other fields, the request has to specify those fields in the query params. For example, if I want the picture and link associated with a Post, I would construct my GET request thus:

https://graph.facebook.com/POST_ID?fields=picture,link&access_token=YOUR_TOKEN

And I would get a response like this:

{
  "link": "http://example.com/link",
  "picture": "https://example.com/picture",
  "id": POST_ID
}

I don't know PHP so can't give the specific syntax for OP's question, but this should get you most of the way there.

Solution 3:[3]

Example link for Facebook post:

facebook.com/123123123/posts/890890890/

pattern:

facebook.com/{{page_id}}/posts/{{post_id}}/

where from API post_id => {{page_id}}_{{post_id}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nishant Ghodke
Solution 2 supremebeing7
Solution 3 PLI52KA