问题
I am Creating A facebook that Retrieves 10 Random friends.But I need some code to Retrieve Top friends using comment and like ativity.I used Following code but i get below error
Invalid argument supplied for foreach()
below is the code i tried so far.
$statuses = $facebook->api('/me/statuses');
foreach($statuses['data'] as $status){
// processing likes array for calculating fanbase.
foreach($status['likes']['data'] as $likesData){
$frid = $likesData['id'];
$frname = $likesData['name'];
$friendArray[$frid] = $frname;
}
foreach($status['comments']['data'] as $comArray){
// processing comments array for calculating fanbase
$frid = $comArray['from']['id'];
$frname = $comArray['from']['name'];
}
回答1:
That error message generally happens when your array variable is not set. If you could add line numbers to your code and give the full error message (including line number) it might help.
Can you show a print_r of $statuses['data']?
回答2:
I've been trying to get this to work myself. Found the solution. $facebook->api() is going to return a json array. This is not a valid element for a foreach() statement. You need to use json_decode($statuses) in order to loop through the array in the foreach() statement.
Foreach through JSONArray in PHP
来源:https://stackoverflow.com/questions/9407379/get-list-of-top-friends-for-facebook-app