I have some code to get Facebook page likes:
<?php
function fbLikeCount($id,$access_token) {
//Request URL
$json_url ='https://graph.facebook.com/'.$id.'?fields=fan_count&access_token='.$access_token;
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if ($json_output->fan_count) {
return $likes = $json_output->fan_count;
} else {
return 0;
}
}
echo fbLikeCount('194583094853853845','APP ID|ACCESSCODE');
?>
The above method works; I can retrieve the page's like count from Facebook Page ID 194583094853853845.
I was given the task to allow the function to retrieve the like count of multiple pages. To that end, I changed the code so something like this:
<?php
function fbLikeCount($id,$access_token) {
//Request URL
$retrievedID == 1633003770266238; // dynamic variable from another field $output["id"];
$json_url = 'https://graph.facebook.com/'.$id.'?fields=fan_count&access_token='.$access_token;
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->fan_count) {
return $likes = $json_output->fan_count;
} else {
return 0;
}
}
echo fbLikeCount($retrievedID,'APP ID|ACCESSCODE');
?>
I use $retrievedID
to pass the page ID which gets the data from another field / variable. $retrievedID
may equal 1633003770266238 in one instance, while it may equal 1633456456456 for a different instance.
However, this doesn't seem to work. How can I fix it?