【问题标题】:Graph API - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request图形 API - 无法打开流:HTTP 请求失败! HTTP/1.0 400 错误请求
【发布时间】:2013-01-22 18:55:40
【问题描述】:

我需要您的帮助来解决问题。 在我获得令牌后的某个时间,当尝试获取有关用户的数据时会发生此错误:

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

我该如何解决这个问题?为什么会这样?

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

谢谢。

【问题讨论】:

  • 将您的访问令牌粘贴到debugger。它是否有效并且是 user 访问令牌?

标签: facebook-graph-api graph sdk facebook-opengraph token


【解决方案1】:

我推荐使用 PHP SDK:https://github.com/facebook/facebook-php-sdk

如果您向下滚动到“使用”,还有一些示例代码,包括正确的登录处理。有了这个,你真的不需要考虑访问令牌,这一切都发生在后台。另请注意,访问令牌的有效期最长为 2 小时,如果您想延长它:使用 PHP SDK 的“setExtendedAccessToken”函数将其延长至最长 60 天。

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 2012-04-08
      • 1970-01-01
      • 2012-07-26
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多