无法 file_get_contents 工作

发布于 2024-11-10 02:33:38 字数 494 浏览 7 评论 0原文

我正在使用 facebook graph api 来检索登录用户的信息。

$facebookdata = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $fb_accesstoken . '&fields=name,picture'),true);

现在,当我var_dump时,我得到了null。我确实尝试转到网址 https://graph.facebook.com /me?access_token=ACCESS_TOKEN&fields=id,名称,图片。它显示名称和照片网址。

我很感激任何帮助。

I am using facebook graph api, to retrieve logged in user's info.

$facebookdata = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $fb_accesstoken . '&fields=name,picture'),true);

Now When I var_dump I get null. I did try to goto url https://graph.facebook.com/me?access_token=ACCESS_TOKEN&fields=id,name,picture. It shows the name and photourl.

I appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的夜不怕黑 2024-11-17 02:33:38

您可以使用 Facebook PHP SDK(在 github 上查看file_get_contents >) 哪个更容易使用:

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$facebook->setAccessToken("...");

$args['fields'] = "name,picture";
$facebookdata = $facebook->api('/me', $args);

查看 SDK 示例< /a> 输出(有据可查)如果您想查看获取用户访问令牌的流程。

希望有帮助!

Instead of using file_get_contents you can you the Facebook PHP SDK (see on github) which is easier to use :

require "facebook.php";
$facebook = new Facebook(array(
    'appId'  => YOUR_APP_ID,
    'secret' => YOUR_APP_SECRET,
));

$facebook->setAccessToken("...");

$args['fields'] = "name,picture";
$facebookdata = $facebook->api('/me', $args);

Check the example of the SDK out (well documented) if you want to see the flow to get the user access token.

Hope that helps !

最偏执的依靠 2024-11-17 02:33:38

检查您的 php.ini 中是否启用了 allow_url_fopen。否则请进行调整,因为此时 file_get_contents 将无法从 http URL 读取。

其次,提高您的 error_reporting 级别。它应该已经告诉你了。

如果您没有收到错误消息,则使用 PHP user_agent 的访问可能会被阻止。还有另一个 php.ini 设置。另外,也可以尝试 curl

Check if allow_url_fopen is enabled in your php.ini. Otherwise adapt, because file_get_contents will not be able to read from http URLs then.

Second, raise your error_reporting level. It should have told you so.

If you get no error message, then access with the PHP user_agent might be blocked. There is another php.ini setting for that. Also alternatively, just try curl.

残龙傲雪 2024-11-17 02:33:38

您应该确定您的请求返回什么 http 响应代码。

我发现 file_get_contents() 仅在 http 响应为 200 时才有效,好吧。

如果响应代码为 404(未找到页面)、400(错误请求)等,它不会返回字符串。

使用不正确的 access_token 访问 Facebook API 将返回 400。

示例:

带有 'http://www 的 file_get_contents() .google.co.uk' 有效,来自 google 的 http 响应是 200

file_get_contents() ,“http://www.google.co.uk/me” 是 null,来自 google 的 http 响应是 404

You should determine what http response code is being returned by your request.

I've found that file_get_contents() only works when the http response is 200, OK.

It doesn't return a string if the response code is 404 (page not found), 400 (bad request), etc.

Hitting the Facebook API with an incorrect access_token returns 400.

Example:

file_get_contents() with 'http://www.google.co.uk' works, http response from google is 200

file_get_contents() with 'http://www.google.co.uk/me' is null, http response from google is 404

飘落散花 2024-11-17 02:33:38
 //i had the same problem before.for some reason the server has a problem with file_get_contents and i used this function.try it instead
 /* gets the data from a URL */
 function get_myurl_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
 }
 $url = 'https://graph.facebook.com/me?access_token=' . $fb_accesstoken . '&fields=name,picture';
 $facebookdata = json_decode(get_myurl_data($url));
 //i had the same problem before.for some reason the server has a problem with file_get_contents and i used this function.try it instead
 /* gets the data from a URL */
 function get_myurl_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
 }
 $url = 'https://graph.facebook.com/me?access_token=' . $fb_accesstoken . '&fields=name,picture';
 $facebookdata = json_decode(get_myurl_data($url));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文