繁体   English   中英

如何从Facebook Graph API回显值

[英]How to echo a value from Facebook Graph API

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

从以下URI获取JSON对象: https : //graph.facebook.com/RDTATTOOANDPIERCING

我认为我将能够简单地转换为数组并回显所需的值。 但是我遇到了错误。

这是代码:

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

如果要查看正在渲染的源,请使用以下URL: http : //www.rdtattoopiercing.com/

提前谢谢!

没有第二个参数的json_decode将返回一个对象,如果要返回数组,则添加第二个参数TRUE

尝试这个:

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

echo $facebook["about"];

或使用object代替,如果您不想在第二个参数中添加TRUE

$facebook->about;

问题是它返回的是stdClass而不是数组,您可以使用var_dump($facebook)告诉我们,要将stdClass转换为数组,请使用get_object_vars()
这是您的解决方案:

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

这是转换为数组之前的var_dump

object(stdClass)#1 (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) } }

如您所见,它以object(stdClass)开头,而应该是一个数组,因此添加代码get_object_vars()将返回数组格式,而不是stdClass,如您从此var_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) } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM