user3526204 user3526204 - 2 months ago 9
JSON Question

php file get contents not working while fetching fb page data

I have the following code to get fb page list

$graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$_SESSION['token'];
$pagedata=file_get_contents($graph_url_pages);
echo "DATA : <br>";
print_r($pagedata);
$pages=json_decode($pagedata,true);
var_dump($pages);
$dropdown = "";
for($i=0;$i<count($pages->data);$i++)
{
if($me=="iamindex"){
$dropdown .= "<option value='".$pages->data[$i]->access_token."-".$pages->data[$i]->id."'>".$pages->data[$i]->name."</option>";
}elseif($me=="iammulti"){
$dropdown .= "<input type='checkbox' name='page' id='status' value='".$pages->data[$i]->access_token."-".$pages->data[$i]->id."'>".$pages->data[$i]->name."<br>";
}else{$dropdown.="<option value=''>No Category Selection</option>";}

}


$pagedata and $pages is empty and so dropdown doesn't get populated.

Somewhere at the top of the page I am using
Ob_start()
and at the end
Ob_flush();
And they are needed. I don't know whether this is the issue.

Would be great if somebody can point out what is going wrong ! if more code is needed, I can provide. Please note I am an amateur programmer. Self taught.

Answer

I recommend using cURL

  $graph_url_pages = "https://graph.facebook.com/me/accounts?access_token=".$_SESSION['token'];

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_URL, $graph_url_pages);

  if(curl_error($c))
  {
     echo 'error:' . curl_error($c);
     //it will produce the error if any exists 
  } 

  $data = curl_exec($ch);
  curl_close($ch);
  print_r($data);
Comments