Donnie D'Amato
Donnie D'Amato

Reputation: 3940

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:

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?

Upvotes: 2

Views: 2316

Answers (3)

PLI52KA
PLI52KA

Reputation: 19

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}}

Upvotes: 0

supremebeing7
supremebeing7

Reputation: 1083

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.

Upvotes: 0

Nishant Ghodke
Nishant Ghodke

Reputation: 923

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.

Upvotes: 2

Related Questions