3

So I am getting this error and I have no clue why. When I run it on the server it's on, I get a 404 error with a simple request like this.

$json = file_get_contents('https://graph.facebook.com/me?access_token='.LONGSTRING);

The error is:

function.file-get-contents: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

However, I can paste the URL that I am using with file_get_contents directly in the browser at the same time and it comes up. So it seems as if Facebook is blocking my server.

Also it works half the time.

Any ideas?

Kara
  • 6,115
  • 16
  • 50
  • 57
Jeffrey Hunter
  • 1,103
  • 2
  • 12
  • 19

2 Answers2

5

Try cURL, not sure why file_get_contents is unreliable but I have seen the same issue in the past. I use this geturl function to cURL a URL and pass the parameters in as a PHP array

function geturl($url, $params) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&'));
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

Which can then be called like so

$url = 'https://www.graph.facebook.com/me';
$params = array('access_token' => '*********************');
$graph_ret = geturl($url, $params);
DSchultz
  • 1,335
  • 6
  • 15
  • Worked...although the graph api didn't allow for POST so I just attached it to the URL and it worked. Hopefully it will work when the other doesn't. – Jeffrey Hunter Aug 02 '11 at 00:02
  • Glad it worked. FYI, the graph API definitely allows POST in some cases and even requires it for certain actions. – DSchultz Aug 02 '11 at 00:33
0
$json = @file_get_contents('https://graph.facebook.com/me?access_token='.$access_token) or die("ERROR"); 

use this will help you alot

Gabriel Mongeon
  • 7,251
  • 3
  • 32
  • 35