Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 404 Error from Facebook Graph API

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?

like image 848
Jeffrey Hunter Avatar asked Jul 16 '26 07:07

Jeffrey Hunter


1 Answers

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);
like image 191
DSchultz Avatar answered Jul 18 '26 04:07

DSchultz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!