从Facebook Graph API获取用户名

Joh*_*Doe 19 php facebook

我想知道如何从外部页面检索字符串.

例如:在PHP网站中,用户发送facebook id,例如:1157251270

该网站从http://graph.facebook.com/1157251270返回名称.

我希望我说清楚.

谢谢

Gor*_*don 26

Graph API返回JSON字符串,因此您可以使用:

echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;
Run Code Online (Sandbox Code Playgroud)

或者更详细:

$pageContent = file_get_contents('http://graph.facebook.com/1157251270');
$parsedJson  = json_decode($pageContent);
echo $parsedJson->name; // Romanos Fessas
Run Code Online (Sandbox Code Playgroud)

请参阅json_decode- 解码JSON字符串


jes*_*sal 6

如果您使用Facebook的PHP SDK,您也可以执行此操作来查询其图形API:

$fb = new Facebook();
$object = $fb->api('/1157251270');
Run Code Online (Sandbox Code Playgroud)


Adn*_*nan 5

你得到它:

$link = json_decode(file_get_contents('http://graph.facebook.com/1157251270'));
echo $link->name;
Run Code Online (Sandbox Code Playgroud)

好的tut:http: //webhole.net/2009/08/31/how-to-read-json-data-with-php/