Invalid argument supplied for foreach() while feching data in facebook

430 Views Asked by At

I want to fetch facebook wall feed into my webpage but I am getting a typical error despite the procedure followed.

Warning: Invalid argument supplied for foreach() in /home/u500024704/public_html/fb.php on line 18

I pasted the code in an php file in a div wrapper and its uploaded into the server

the code is as follows.

<?php
$page_id = 'fbpage';
$access_token = 'app_id';
$a = 0;
//Get the JSON
$json_object = @file_get_contents('https://graph.facebook.com/' . $page_id . 
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);

foreach ($fbdata->data as $post )
{
$likecount = $post->likes->data;
$posts .= '<div width="500"><div width="500" style="border: solid; border-color:    #82878e; border-width: 1px;"><h4><a href="' . $post->link .'"><img style="float: left; margin: 10;" height ="100" src="' . $post->picture . '" /></a>';
$posts .= '<a href="'. $post->link .'">' . $post->message . '</a></h4>';
$posts .= '<p>' . $post->description . '</p>';
foreach ($likecount as $likes){
$a++;
}
$posts .= '<p>' . $a . ' people like this post </p>
$a = 0;
<p>&nbsp;</p>
</div> 
';
}
echo ($posts);
?>

Thankyou in advance

1

There are 1 best solutions below

0
Nithya Sivakumar On

It is always a good practice to ensure an array is non-empty before looping through it

if ($posts)
{
  foreach ($posts as $post)
  {
     ...
  }
}