2013-01-22 169 views
0

我需要您的帮助来解决问题。 之后的某个时间,我得到了令牌时一试,以获取有关用户的数据此错误发生:图形API - 无法打开流:HTTP请求失败! HTTP/1.0 400错误请求

A PHP Error was encountered 

Severity: Warning 

Message: file_get_contents(https://graph.facebook.com/me?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

的代码执行是这样的:

$url = "https://graph.facebook.com/me?access_token=".$token; 
$dados = @json_decode(file_get_contents($url)); 

我怎样才能解决这个问题呢?为什么会发生?

我搜索了很多,但没有找到解决方案。请有人帮助我。

谢谢。

+0

将您的访问令牌粘贴到[debugger](https://developers.facebook.com/tools/debug)中。它是有效的和*用户*访问令牌? – cpilko

回答

0

我建议使用PHP SDK:https://github.com/facebook/facebook-php-sdk

There's此​​外,如果您向下滚动到“使用”,包括正确的登录处理一些示例代码。有了这个,你并不需要考虑访问令牌,这一切都发生在后台。另外请记住,如果您想扩展访问令牌,则访问令牌最多只能使用2小时:使用PHP SDK的“setExtendedAccessToken”函数将其延长到60天。

0

尝试urlencode()您的令牌并将值传递给file_get_contents()。这可能工作。

file_get_contents will only work if you have allow_url_fopen = 1 in your php.ini 

对于像上述远程文件访问,可以考虑使用cURL函数PHP提供。

function url_get_contents ($Url) { 
    if (!function_exists('curl_init')){ 
     die('CURL is not installed!'); 
    } 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $Url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get the code of request 
    curl_close($ch); 

    if($httpCode == 400) 
     return 'No donuts for you.'; 

    if($httpCode == 200) //is ok? 
     return $output; 


} 
相关问题