简体   繁体   中英

How to echo a value from Facebook Graph API

I'm attempting to use the Facebook Graph API to import details from employees Facebook profiles into our personal website.

Fetching the JSON object from the following URI: https://graph.facebook.com/RDTATTOOANDPIERCING

I thought that I would be able to simply convert to an array and echo the desired value. However I'm getting errors.

Here's the code:

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

Here's the URL if you want to see the source being rendered: http://www.rdtattoopiercing.com/

Thanks ahead of time!

json_decode without the second parameter will return an object, if you want an array to be returned add a second parameter TRUE

try this:

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

echo $facebook["about"];

or use object instead if you don't want to add TRUE to the second parameter

$facebook->about;

The problem is that it returned a stdClass not an array, You could tell that by using var_dump($facebook) , To convert an stdClass to an array use get_object_vars() .
Here is your solution:

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

Here is the var_dump before the conversion to an array:

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) } }

As you can see it begins with object(stdClass) instead it should be an array, so adding the code get_object_vars() will return an array format not an stdClass, as you can see from this 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) } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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