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?
Now the count is not showing at all and gets the error.
That is not how you report error in your code. Either write what it is returning as error or don't say that there is error at all.
Pretty much, looking at your code the answer should be $retrievedID
. Just look at where it is defined in the code you posted above.
Remove it from the inside of fbLikeCount()
function and move it before you call it in the main scope.
function fbLikeCount($id, $access_token) {/* content */}
$retrievedID == 1633003770266238; // dynamic variable from another field $output["id"];
echo fbLikeCount($retrievedID,'APP ID|ACCESSCODE');
That's everything I can see at the moment that is wrong with the code above.