将Facebook页面的JSON输出转换为PHP字符串

时间:2015-08-09 13:54:31

标签: php json facebook

所以我可以使用以下网址来获取具有特定页面的Facebook喜欢数量的JSON对象。但我正在努力对数据做任何事情 - 如何将返回的数字用作PHP变量?

网址:https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/%27&format=json

2 个答案:

答案 0 :(得分:1)

你可以使用json_decode(参见:http://docs.php.net/json_decode

<?php
$json_url = "https://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/%27&format=json";
$json = file_get_contents($json_url);
$json = json_decode($json, true);

print_r($json);

echo "Number of likes : " . $json[0]['like_count'];

?>

答案 1 :(得分:0)

当您返回JSON字符串时,为了在PHP中使用此数据,您可以使用json_decode() function将其转换为PHP等效数据类型,如下所示:_

<?php
$json_string = file_get_contents('http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url=%27https://www.facebook.com/%27&format=json');

$json = json_decode($json_string);
echo json_last_error_msg().PHP_EOL;

echo $json[0]->like_count;