Php 如何从Facebook Graph API回显值

Php 如何从Facebook Graph API回显值,php,arrays,json,facebook-graph-api,Php,Arrays,Json,Facebook Graph Api,我正在尝试使用Facebook Graph API将员工Facebook档案中的详细信息导入我们的个人网站 从以下URI获取JSON对象: 我想我可以简单地转换成数组并回显所需的值。然而,我得到了错误 代码如下: <?php $facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING")); echo $facebook["about"]; ?> 如果要查看

我正在尝试使用Facebook Graph API将员工Facebook档案中的详细信息导入我们的个人网站

从以下URI获取JSON对象:

我想我可以简单地转换成数组并回显所需的值。然而,我得到了错误

代码如下:

<?php
$facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING"));
echo $facebook["about"];
?>

如果要查看呈现的源代码,请使用以下URL:


提前谢谢

不带第二个参数的json_decode将返回一个对象,如果希望返回数组,请添加第二个参数TRUE

试试这个:

$facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING"), TRUE);

echo $facebook["about"];
或者,如果不想将TRUE添加到第二个参数,则使用object


$facebook->about

不带第二个参数的json_decode将返回一个对象,如果希望返回数组,请添加第二个参数TRUE

试试这个:

$facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING"), TRUE);

echo $facebook["about"];
或者,如果不想将TRUE添加到第二个参数,则使用object


$facebook->about

问题在于它返回的是一个stdClass而不是数组,您可以通过使用
var\u dump($facebook)
来判断,要将stdClass转换为数组,请使用
get\u object\u vars()

以下是您的解决方案:

<?php
$facebook = get_object_vars(json_decode(file_get_contents("http://graph.facebook.com/RDTATTOOANDPIERCING")));
echo $facebook['about'];
?>
正如您所看到的,它以object(stdClass)开始,而应该是一个数组,因此添加代码
get\u object\u vars()
将返回一个数组格式,而不是stdClass,您可以从这个
var\u dump
中看到:

array(20) { ["about"]=> string(60) "Red Dragon Tattoo & Piercing is devoted to inking Cincinnati" ["checkins"]=> int(144) ["description"]=> string(509) "Red Dragon has been open since Feb. 2009, After Gabriel started constructing J began to Fill the chairs with his large following, after only a few months we brought on Chuck and Jocelyn to help with the overwhelming amount of people ready to get tattooed in the Colerain area and we've been inking up Cincinnati since then. We appreciate artfull expressions, and can help you make your own statement to this world through your body. If it's a body piercing, or a tattoo, what better way to express yourself." ["general_info"]=> string(59) "Artists: J, Chuck Hagedorn, Jocelyn Taylor, Gabriel Lowe " ["hours"]=> object(stdClass)#2 (12) { ["mon_1_open"]=> string(5) "13:00" ["mon_1_close"]=> string(5) "20:00" ["tue_1_open"]=> string(5) "13:00" ["tue_1_close"]=> string(5) "20:00" ["wed_1_open"]=> string(5) "13:00" ["wed_1_close"]=> string(5) "20:00" ["thu_1_open"]=> string(5) "13:00" ["thu_1_close"]=> string(5) "20:00" ["fri_1_open"]=> string(5) "13:00" ["fri_1_close"]=> string(5) "20:00" ["sat_1_open"]=> string(5) "13:00" ["sat_1_close"]=> string(5) "20:00" } ["is_published"]=> bool(true) ["location"]=> object(stdClass)#3 (7) { ["street"]=> string(17) "9242 Colerain Ave" ["city"]=> string(10) "Cincinnati" ["state"]=> string(2) "OH" ["country"]=> string(13) "United States" ["zip"]=> string(10) "45251-2406" ["latitude"]=> float(39.239511218398) ["longitude"]=> float(-84.592958873707) } ["parking"]=> object(stdClass)#4 (3) { ["street"]=> int(0) ["lot"]=> int(1) ["valet"]=> int(0) } ["phone"]=> string(17) "+1 (513) 385-6500" ["price_range"]=> string(10) "$$$$ (50+)" ["talking_about_count"]=> int(97) ["username"]=> string(19) "RDTATTOOANDPIERCING" ["website"]=> string(20) "rdtattoopiercing.com" ["were_here_count"]=> int(535) ["category"]=> string(14) "Local business" ["id"]=> string(15) "151284611579488" ["name"]=> string(28) "Red Dragon Tattoo & Piercing" ["link"]=> string(43) "http://www.facebook.com/RDTATTOOANDPIERCING" ["likes"]=> int(835) ["cover"]=> object(stdClass)#5 (3) { ["cover_id"]=> float(4.636681470078E+14) ["source"]=> string(94) "http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash3/s720x720/547023_463668147007798_1259271309_n.jpg" ["offset_y"]=> int(0) } }

问题是它返回的是一个stdClass而不是数组,通过使用
var\u dump($facebook)
,您可以看出,要将stdClass转换为数组,请使用
get\u object\u vars()

以下是您的解决方案:

<?php
$facebook = get_object_vars(json_decode(file_get_contents("http://graph.facebook.com/RDTATTOOANDPIERCING")));
echo $facebook['about'];
?>
正如您所看到的,它以object(stdClass)开始,而应该是一个数组,因此添加代码
get\u object\u vars()
将返回一个数组格式,而不是stdClass,您可以从这个
var\u dump
中看到:

array(20) { ["about"]=> string(60) "Red Dragon Tattoo & Piercing is devoted to inking Cincinnati" ["checkins"]=> int(144) ["description"]=> string(509) "Red Dragon has been open since Feb. 2009, After Gabriel started constructing J began to Fill the chairs with his large following, after only a few months we brought on Chuck and Jocelyn to help with the overwhelming amount of people ready to get tattooed in the Colerain area and we've been inking up Cincinnati since then. We appreciate artfull expressions, and can help you make your own statement to this world through your body. If it's a body piercing, or a tattoo, what better way to express yourself." ["general_info"]=> string(59) "Artists: J, Chuck Hagedorn, Jocelyn Taylor, Gabriel Lowe " ["hours"]=> object(stdClass)#2 (12) { ["mon_1_open"]=> string(5) "13:00" ["mon_1_close"]=> string(5) "20:00" ["tue_1_open"]=> string(5) "13:00" ["tue_1_close"]=> string(5) "20:00" ["wed_1_open"]=> string(5) "13:00" ["wed_1_close"]=> string(5) "20:00" ["thu_1_open"]=> string(5) "13:00" ["thu_1_close"]=> string(5) "20:00" ["fri_1_open"]=> string(5) "13:00" ["fri_1_close"]=> string(5) "20:00" ["sat_1_open"]=> string(5) "13:00" ["sat_1_close"]=> string(5) "20:00" } ["is_published"]=> bool(true) ["location"]=> object(stdClass)#3 (7) { ["street"]=> string(17) "9242 Colerain Ave" ["city"]=> string(10) "Cincinnati" ["state"]=> string(2) "OH" ["country"]=> string(13) "United States" ["zip"]=> string(10) "45251-2406" ["latitude"]=> float(39.239511218398) ["longitude"]=> float(-84.592958873707) } ["parking"]=> object(stdClass)#4 (3) { ["street"]=> int(0) ["lot"]=> int(1) ["valet"]=> int(0) } ["phone"]=> string(17) "+1 (513) 385-6500" ["price_range"]=> string(10) "$$$$ (50+)" ["talking_about_count"]=> int(97) ["username"]=> string(19) "RDTATTOOANDPIERCING" ["website"]=> string(20) "rdtattoopiercing.com" ["were_here_count"]=> int(535) ["category"]=> string(14) "Local business" ["id"]=> string(15) "151284611579488" ["name"]=> string(28) "Red Dragon Tattoo & Piercing" ["link"]=> string(43) "http://www.facebook.com/RDTATTOOANDPIERCING" ["likes"]=> int(835) ["cover"]=> object(stdClass)#5 (3) { ["cover_id"]=> float(4.636681470078E+14) ["source"]=> string(94) "http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash3/s720x720/547023_463668147007798_1259271309_n.jpg" ["offset_y"]=> int(0) } }

描述“获取错误”并指定您正在阅读的资源以获得结果。描述“获取错误”并指定您正在阅读的资源以获得结果。