php 从 Facebook Graph API 获取用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5788937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get user's name from Facebook Graph API
提问by John Doe
I would like to know how is it possible to retrieve a string from an external page.
我想知道如何从外部页面检索字符串。
For example: In a PHP website, the user sends a facebook id, ex: 1157251270
例如:在 PHP 网站中,用户发送 facebook id,例如:1157251270
And the website returns the namefrom http://graph.facebook.com/1157251270.
该网站从http://graph.facebook.com/1157251270返回名称。
I hope I made it clear.
我希望我说清楚了。
Thank you
谢谢
回答by Gordon
The Graph API returns JSON strings, so you can use:
Graph API 返回 JSON 字符串,因此您可以使用:
echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;
or more verbose:
或更详细:
$pageContent = file_get_contents('http://graph.facebook.com/1157251270');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name; // Romanos Fessas
回答by jesal
If you are using Facebook's PHP SDK, you can also do this to query their graph API:
如果你使用 Facebook 的 PHP SDK,你也可以这样做来查询他们的图形 API:
$fb = new Facebook();
$object = $fb->api('/1157251270');
回答by Adnan
you get it by:
你可以通过:
$link = json_decode(file_get_contents('http://graph.facebook.com/1157251270'));
echo $link->name;
Nice tut: http://webhole.net/2009/08/31/how-to-read-json-data-with-php/
不错的啧啧:http: //webhole.net/2009/08/31/how-to-read-json-data-with-php/
回答by dwarfy
Either you use :
要么你使用:
$res_json = file_gets_contents("http://graph.facebook.com/1157251270")
$res = json_decode($res_json)
Or, if you prefer curl (here with https and access token) :
或者,如果您更喜欢 curl(此处使用 https 和访问令牌):
$ch4 = curl_init();
curl_setopt($ch4, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch4, CURLOPT_URL, "https://graph.facebook.com/1157251270?access_token=YOUR_ACCESS_TOKEN");
curl_setopt($ch4, CURLOPT_SSL_VERIFYPEER, false);
if(!$result = curl_exec($ch4))
{
echo curl_error($ch4);
} else {
$res = json_decode($res_json)
}
curl_close($ch4);