Graph API:如何获取当前用户的信息? [英] Graph API: How to grab current user's information?

查看:103
本文介绍了Graph API:如何获取当前用户的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个PHP脚本(id和name)来获取用户的一些基本信息。



我尝试过以下方法:

  $ retrieve = curl_init(https://graph.facebook.com/me?access_token=$accesstoken); 
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ retrieve,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
$ data = curl_exec($ retrieve);
curl_close($ retrieve);

  $ user = json_decode(file_get_contents(
https://graph.facebook.com/me?access_token=$accesstoken\"))->me;

cURL方法(前)刚刚超时。 file_get_contents(后一个)方法根本不返回任何东西...



可能是什么原因造成的?我正在使用cURL吗?

解决方案

对于图形API,您可以使用图形api方法rahter than curl
以下代码获取当前用户的信息

 
define('FACEBOOK_APP_ID','您的API ID');
define('FACEBOOK_SECRET','你的秘密');
函数get_facebook_cookie($ app_id,$ application_secret)
{
$ args = array();
parse_str($ _ COOKIE ['fbs_'。$ app_id],'\\''),$ args);
ksort($ args);
$ payload = ';
foreach($ args as $ key => $ value)
{
if($ key!='sig')
{
$ payload = $ key $'
$ b}
if(md5($ payload。$ application_secret)!= $ args ['sig'])
{
return null;
}
return $ args;
}
$ cookie = get_facebook_cookie(FACEBOOK_APP_ID,FACEBOOK_SECRET);
$ user = json_decode(file_get_contents(' https://graph.facebook.com/me?access_token='.$cookie['access_token']));

它的pretey简单


I'm trying to get some basic information about a user in a PHP script (id and name).

I have tried the following methods:

$retrieve = curl_init("https://graph.facebook.com/me?access_token=$accesstoken");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($retrieve, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($retrieve);
curl_close($retrieve);

and

$user = json_decode(file_get_contents(
         "https://graph.facebook.com/me?access_token=$accesstoken"))->me;

The cURL method (former) just times out. The file_get_contents (latter) method just doesn't return anything at all...

What could be causing this? Am I using cURL correctly?

解决方案

for graph api you can use graph api methods rahter than curl the following code grabs information of current user

define('FACEBOOK_APP_ID', 'Your API ID');
define('FACEBOOK_SECRET', 'YOUR SECRET');
function get_facebook_cookie($app_id, $application_secret)
{
    $args = array();
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    ksort($args);
    $payload = '';
    foreach ($args as $key => $value)
    {
        if ($key != 'sig')
        {
            $payload .= $key . '=' . $value;
        }
    }
    if (md5($payload . $application_secret) != $args['sig'])
    {
        return null;
    }
    return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$user=json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']));

its prettey easy

这篇关于Graph API:如何获取当前用户的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆