获取用户家乡和当前位置

发布于 2024-12-11 16:31:27 字数 1366 浏览 0 评论 0原文

我正在使用 facebook 示例中的以下脚本:

该脚本可以很好地抓取电子邮件,但家乡和位置会在我的服务器上触发 500 错误。我需要抓取数据进行统计。

<?php 

$app_id = "API_ID_GOES_HERE";
$app_secret = "SECRET_GOES_HERE";
$my_url = "REDIRECT_URL_GOES_HERE";
$permissions ="user_hometown,user_location,email";

session_start();
$code = $_REQUEST["code"];

if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=".$permissions. 
  "&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
  }

if($_REQUEST['state'] == $_SESSION['state']) {
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&scope=".$permissions. "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));

 echo("Hello " . $user->name);
 echo ("Location ". $user->location);


}
else {
  echo("The state does not match. You may be a victim of CSRF.");
}

?>

I am using the following script from facebook example:

The script grabs the e-mail fine but hometown and location trigger a 500 error on my server. I need to grab the data for statistics.

<?php 

$app_id = "API_ID_GOES_HERE";
$app_secret = "SECRET_GOES_HERE";
$my_url = "REDIRECT_URL_GOES_HERE";
$permissions ="user_hometown,user_location,email";

session_start();
$code = $_REQUEST["code"];

if(empty($code)) {
 $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
 $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=".$permissions. 
  "&state="
   . $_SESSION['state'];

 echo("<script> top.location.href='" . $dialog_url . "'</script>");
  }

if($_REQUEST['state'] == $_SESSION['state']) {
 $token_url = "https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&scope=".$permissions. "&client_secret=" . $app_secret . "&code=" . $code;

 $response = file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);

 $graph_url = "https://graph.facebook.com/me?access_token=" 
   . $params['access_token'];

 $user = json_decode(file_get_contents($graph_url));

 echo("Hello " . $user->name);
 echo ("Location ". $user->location);


}
else {
  echo("The state does not match. You may be a victim of CSRF.");
}

?>

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

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

发布评论

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

评论(2

掀纱窥君容 2024-12-18 16:31:27

我找到了答案。使用这个 $user->hometown->name 将获取 Hometown 数组和 Name 对象。

前任。 [家乡] => stdClass Object ( [id] => 111806898837603 [name] => San Antonio, Texas )

$user->hometown->name  

输出:

德克萨斯州圣安东尼奥

I found the answer. Using this $user->hometown->name Will get the Hometown array and the Name object.

EX. [hometown] => stdClass Object ( [id] => 111806898837603 [name] => San Antonio, Texas )

$user->hometown->name  

OUTPUT:

San Antonio, Texas

不打扰别人 2024-12-18 16:31:27

我将获取您创建的 URL,以从图形 api(即 $graph_url)获取用户信息,并将它们粘贴到您的浏览器中以检查 facebook 返回的数据,然后从那里开始。如果 URL 返回正确的信息,那么您就知道您得到了正确的数据。使用 Graph API Explorer (https://developers.facebook.com/tools/explorer) 来测试您为图形 API 创建的 URL。您可以查看授予不同权限后的结果,并为响应编写相应的代码。希望有帮助!

I would take the URLs you are creating to get the user info off the graph api (i.e. $graph_url) and paste them into your browser to inspect the data facebook is returning, and go from there. If the URL's are returning proper information, then you know you are getting the right data back. Use the Graph API Explorer (https://developers.facebook.com/tools/explorer) to test the URL's you are creating for the graph API. You can see what results look like with different permissions granted and code accordingly for the response. Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文