'Getting public posts of a random user from Facebook API

I want to get the latest posts of some random users (public posts of curse). I figured since they are public posts I don't need to ask permission from the owner. Here is my code:

function fetchUrl($url){
     return file_get_contents($url);
}

$profile_id = "4";

$app_id     = "****";
$app_secret = "****";
    
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}");

$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/posts?{$authToken}");
$feedarray   = json_decode($json_object , true );

foreach ( $feedarray as $feed_data )
{
    var_dump( $feed_data);
}

It returns some data about the user activities but nothing about the post!

Here is the example

this is my fb acc ( I rarely use it ), I've just posted a post ( test3 ) https://www.facebook.com/Hereticcc

when I run this code with my account id

$json_object = fetchUrl("https://graph.facebook.com/{$profile_id}/posts?{$authToken}&limit=50");
$feedarray   = json_decode($json_object , true );
foreach ( $feedarray['data'] as $k=>$v )
{
    /// var_dump( $v['story']);
    echo  $v['story']; echo '<br />';
}

here is what I get! no sign of the post that I've just added only likes and friendships

Mj Tb Z likes Facebook Developers.
Mj Tb Z and Ahmad Masajedi are now friends.
Mj Tb Z and Di M AH are now friends.
Mj Tb Z likes Cult of Luna.
Mj Tb Z likes a link.
Mj Tb Z likes a status.
Mj Tb Z likes a link.
Mj Tb Z and Eh San Mans are now friends.
Mj Tb Z likes a link.
Mj Tb Z likes Mist Within.
Mj Tb Z likes Filmhaa.
Mj Tb Z likes a link.
Mj Tb Z and Mamad RJ are now friends.
Mj Tb Z likes a link.
Mj Tb Z is now using Facebook in English (US).
Mj Tb Z is now friends with Mushu Khoshbakht and Samin Zavarkesh.
Mj Tb Z likes a photo.
Mj Tb Z updated his cover photo.
Mj Tb Z and Mohammad Khoshbakht are now friends.

I've tried FQL

here is my code

$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={$app_id}&client_secret={$app_secret}");

echo $fql_query_url = 'https://graph.facebook.com/'
        . 'fql?q=SELECT+message+FROM+stream+WHERE+source_id=4'
        . '&' . $authToken;
        
echo '<br />';
$fql_query_result = fetchUrl($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
    
echo '<pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';

Here is the result:

https://graph.facebook.com/fql?q=SELECT+message+FROM+stream+WHERE+source_id=4&access_token=284654551659478|D7T6A1uMSr1Qm1LLFBB9CQk_vK8

query results:Array
(
    [data] => Array
        (
        )

)


Solution 1:[1]

You cannot read the feed of posts (including their status updates which are public) for a random user without his/her permission. You need a valid User Access Token from a user with a read_stream permission in order to do that.

With a valid Access Token, you can achieve this for a particular user or his/her friends by using an FQL query on the stream table. You can use a query like:

SELECT post_id, message FROM stream WHERE source_id = {User_ID} LIMIT 50 

Or, if you particularly concerned about the statuses of a user or his/her friends, you can use status table like:

SELECT status_id, message FROM status WHERE uid = {User_ID} LIMIT 50

There is however a way to read the stream of user status updates and page status updates as they are posted to Facebook. This can be done by using the Public Feed API. But this will only include status updates that have their privacy set to ‘public’ are included in the stream. Also, you cannot apply to use the API at this time as its access is limited.

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