Getting 404 Error from Facebook Graph API

风流意气都作罢 提交于 2019-12-18 06:57:57

问题


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?


回答1:


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);



回答2:


$json = @file_get_contents('https://graph.facebook.com/me?access_token='.$access_token) or die("ERROR"); 

use this will help you alot



来源:https://stackoverflow.com/questions/6903240/getting-404-error-from-facebook-graph-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!