停留在Graph API(Facebook API)上,如何使用此代码获取姓名,电子邮件和性别? [英] Stuck on Graph API (facebook API), how do i get name, email and sex using this code?
问题描述
代码
<?php
define('FACEBOOK_APP_ID','MY.APP.CODE');
define('FACEBOOK_SECRET','MY.APP.SECRET');
函数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;
}
返回的$ args;
}
$饼干= get_facebook_cookie(FACEBOOK_APP_ID,FACEBOOK_SECRET);
个?
将!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml
xmlns:fb =http://www.facebook.com/2008/fbml> ;
< body>
<?php if($ cookie){
echo(ID is:。$ cookie ['uid']);
echo ('< br>个人资料图片是:< IMG SRC =http://graph.facebook.com/'。$ cookie ['uid']。'/ picture?type = large>< br> ;');
} else {
echo('< fb:login-button perms =email,user_birthday,publish_s tream,user_status>< / fb:login-button>');
}?>
< div id =fb-root>< / div>
< script src =http://connect.facebook.net/en_US/all.js>< / script>
< script>
FB.init({appId:'<?= FACEBOOK_APP_ID?>',status:true,
cookie:true,xfbml:true});
FB.Event.subscribe('auth.login',function(response){
window.location.reload();
});
< / script>
< / body>
< / html>
输出
这里的id是:这里的一个数字图片
是:这里有一张图片
问题
正如你所看到的,这段代码给了我登录时用户的脸书ID和个人资料图片,对应用程序的权限。正如我所说,这段代码工作正常,但我需要更多的信息,我如何检查电子邮件,性别和名称使用相同的逻辑代码?请不要使用外部库(如 facebook.php )
请记住,这是一个非常简单的应用程序,所以我不想得到它在代码复杂,我知道该解决方案与解析JSON响应有关,但我找不到任何好的和简单的代码。感谢!!!!
图表API以JSON形式返回结果。
<例如
GET http://graph.facebook.com/4 HTTP / 1.1
给出
{id :4,name:Mark Zuckerberg,first_name:Mark,last_name:扎克伯格,link:http:\ / \ / www.facebook.com\\ \\ / zuck,gender:male,locale:en_US}
这里 name
是名字; 性别
是性别。如果您已授予用户的电子邮件许可,您还可以收到电子邮件。
Well, this is the issue. I have this code, who works very well:
The code
<?php
define('FACEBOOK_APP_ID', 'MY.APP.CODE');
define('FACEBOOK_SECRET', 'MY.APP.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);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($cookie) {
echo("The ID is: " . $cookie['uid']);
echo('<br>The profile Picture is: <IMG SRC="http://graph.facebook.com/' . $cookie['uid'] . '/picture?type=large"><br>');
} else {
echo('<fb:login-button perms="email,user_birthday,publish_stream,user_status"></fb:login-button>');
} ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
The output
The id is: A number here The picture is: A picture here
The problem
As you can see, this code give me the facebook's ID of the user and the profile picture when he login and give the permissions to the app. As i said, this code works fine, but i need more information, how do i retrive the email, the sex and the name using the same logic of this code?. Please do not use external libiries (like the facebook.php)
Remember that this is a very simple app so i dont want to get it complex in the code, i know that the solution have something to do with parsing JSON responses, but i cant find any good and simple code. Thanks!!!!
Graph API returns result as JSON.
For example
GET http://graph.facebook.com/4 HTTP/1.1
gives
{"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"http:\/\/www.facebook.com\/zuck","gender":"male","locale":"en_US"}
Here name
is name; gender
is sex. If you have granted email permission from user, you got the email also.
这篇关于停留在Graph API(Facebook API)上,如何使用此代码获取姓名,电子邮件和性别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!