ouput facebook url comments json_decode - php

I am trying to output the comments of a facebook page in PHP. For example:
http://graph.facebook.com/comments/?ids=http://www.example.com
Can someone please explain how to decode this correctly so I can have all comments appearing on a page?
I have tried a couple of different scripts/expamples but they all seem to be returning no results or an error, so i think maybe the graph API has changed since.
//get the json string from URL
$data = file_get_contents("http://www.example.com");
//transform json to associative array
$data = json_decode($data, true);
//use only the comments array
$comments = $data['http://www.example.com']['comments']['data'];
foreach($comments as $comment) {
echo $comment['from']['name'];
echo $comment['message'];
}

//get the json string from URL
$data = file_get_contents("http://graph.facebook.com/comments/?ids=http://www.example.com");
//transform json to associative array
$data = json_decode($data, true);
//use only the comments array
$comments = $data['http://www.example.com']['comments']['data'];
//you should only see the comments array printed
echo "<pre>"; print_r($comments); echo "</pre>";

Related

Read curl php result

I'm doing a curl and getting the JSON result as:
$data = json_decode($result, true);
var_dump($data);
$data = $data["data"];
echo $data;
However, the echo of $data is "{". If I do a var_dump before assigning again the variable I see:
string(727796) "{"data":["base64_image1", "base64_image2",... ]}"
Why am I not able to access the data list? I just want to do a foreach over this list, but I cannot.
output is json so try to json_decode() again

Having trouble accessing values from json_decode

I have a json object from a curl get request.
// Curl Stuff
$resp = curl_exec($curl);
{"food": {"id":585897,"foodGroup":"meats","calories":1109,"foodTier":30}}
I saved it in a variable with json_decode
$data = json_decode($resp, TRUE);
I've tried accessing the data in a couple ways but I get no response
echo $data[0][1];
echo $data[0]['id'];
Also if someone can point me in the right direction of looping through this data I would appreciate that.
With the true parameter in your json_decode, you have associative array, so it must be:
echo $data['food']['id'];
Get the id by numeric:
$da = array();
foreach($data as $key=>$val){
$da[$key] = array_values($val);
}
print_r($da);
echo $da['food'][0];

Parse Data from JSON URL

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.

get Specific JSON Response Value

json response as below:
{"Success":true,"ErrorCode":0,"UserInformation":{"UserID":"19"}......
how do I get the UserID value by using json_decode?
I tried the below code with no luck.
$Response = json_decode($Response[2]);
echo $Response[0][0][0];
This should work:
<?php
$json = '{"Success":true, "ErrorCode":"0","UserInformation":{"UserID":"19"}}';
$response = json_decode($json, true);
echo $userId = $response["UserInformation"]["UserID"];
?>
Please check the PHP documentation of json_decode at http://php.net/json_decode
You can use object as well if you skip the second parameter of the json_decode function.

Decoding JSON into an array for a xbox live image script?

I'm trying to turn a JSON file of Xbox Live data into variables that I can use in a PHP generated image. My JSON file is here: http://www.xboxgamercard.org/gamercard/test3/xbox.php
I have tried this:
$request_url = 'xbox.php';
$json = file_get_contents($request_url);
$decode = json_decode($json, true);
var_dump($decode['gamertag'][0]);
but it just returns NULL.
I would like to use the JSON as shown here:
$gamertag = $data['Gamertag'];
echo $gamertag;
You need to add the full url eg:
<?php
$request_url = 'http://www.xboxgamercard.org/gamercard/test3/xbox.php';
$json = file_get_contents($request_url);
$data = json_decode($json, true);
//Example output
echo $data['gamertag']; //Crylics
echo $data['gamerscore']; //7492
/* Too access the recent_games key you will need
to loop through it or access it like
*/
echo $data['recent_games'][1]['title']; //Call of Duty: WaW
?>

Categories