简体   繁体   中英

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?

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);
$json = @file_get_contents('https://graph.facebook.com/me?access_token='.$access_token) or die("ERROR"); 

使用这将帮助你很多

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM