Facebook JSON array - php

How do I display the data->name?
{
"data": [
{
"name": "Aasdada",
"category": "asdasd",
"id": "2342342342",
"created_time": "2012-11-28T03:56:00+0000"
}
],
"paging": {
"next": "https://graph.facebook.com/&limit=5000&offset=5000&__after_id=12312"
}
}
I tried this but nothing comes out:
$user = json_decode(file_get_contents($graph_url), true);
echo("Hello " . $user->data->name);
//also tried this
echo("Hello " . $user[data]); //did not echo anything
echo("Hello " . $user->data[0]->name); //did not echo anything
$user->data[0]['name'] //still no luck
What could be the problem?
Anyways my code works with data that is not in array.
I'm just trying to make a simple if user liked page or not.
If data has no contents it means the user has not liked the page.
If it has data then it means the user has liked the page.
--edit--
So tried this out:
array(2) { ["data"]=> array(1) { [0]=> array(4) { ["name"]=> string(28) "Aasdda" ["category"]=> string(5) "asdad" ["id"]=> string(15) "3123123123" ["created_time"]=> string(24) "2012-11-28T03:56:00+0000" } } ["paging"]=> array(1) { ["next"]=> string(245) "https://graph.facebook.com/100023123123" } }

Try:
$user['data']['0']['name']

Try:
$user->data[0]['name']

Related

Further data retrieving inside array of JSON using PHP

I have a JSON file I want to fill in a table, but can't quite figure out how to retrieve the data inside and loop it through an array to get in a table I made.
This is my JSON file: test.JSON:
{
"data":{
"Chair":{
"id":24,"key":"Chair","name":"Chair","title":"oak home made"
},
"Table":{
"id":37,"key":"Table","name":"Table","title":"round white table"
},
"Closet":{
"id":18,"key":"Closet","name":"Closet","title":"big and red"
},
"Sofa":{
"id":110,"key":"Sofa","name":"Sofa","title":"room for five persons"
}
},
"type":"furniture","version":"1.1.0"
}
Then with PHP I used this: test.PHP:
$url = 'test.json';
$result=file_get_contents($url);
$decoded=json_decode($result, true);
var_dump($decoded);
This is what I get:
array(3) { ["data"]=> array(4)
{
["Chair"]=> array(4)
{ ["id"]=> int(24) ["key"]=> string(5) "Chair" ["name"]=> string(5) "Chair" ["title"]=> string(13) "oak home made" }
["Table"]=> array(4)
{ ["id"]=> int(37) ["key"]=> string(5) "Table" ["name"]=> string(5) "Table" ["title"]=> string(17) "round white table" }
["Closet"]=> array(4)
{ ["id"]=> int(18) ["key"]=> string(6) "Closet" ["name"]=> string(6) "Closet" ["title"]=> string(11) "big and red" }
["Sofa"]=> array(4)
{ ["id"]=> int(110) ["key"]=> string(4) "Sofa" ["name"]=> string(4) "Sofa" ["title"]=> string(21) "room for five persons" }
}
["type"]=> string(9) "furniture" ["version"]=> string(5) "1.1.0"
}
Then I am trying to retrieve "Chair", "Table", "Closet" and "Sofa", but I don't know how to do it. When I try this:
foreach ($decoded as $key => $value) {
echo "key: ".$key;
echo "</br></br>";
echo "value: ".$value;
echo "</br></br>";
return;
}
I get:
key: data
value: Array
Can someone help me get "Chair", "Table", "Closet" and "Sofa", including each of thoses' id, key, name and title?
I have received guides, but they won't help me because I feel like this type of "array" is different then the links for guides I receive.
Thanks!
You are looping through the array on the top most level, while you want to be a level lower than that to be able to loop through all the elements of data, like this:
foreach ($summonerDecoded2['data'] as $key => $value)
{
echo "key: ".$key."</br>";
echo "value id: ".$value['id']."</br>";
echo "value key: ".$value['key']."</br>";
echo "value name: ".$value['name']."</br>";
echo "value title: ".$value['title']."</br>";
}
You can just do this:
$url = 'test.json';
$result=file_get_contents($url);
$decoded = json_decode($result, true);
foreach ( $decoded["data"] as $key => $value) {
echo $value[ "key" ];
echo "<br />";
//You can access data by:
/*
echo $value[ "id" ];
echo $value[ "key" ];
echo $value[ "name" ];
echo $value[ "title" ];
*/
}
This will result to:
Chair
Table
Closet
Sofa

How to access XML response

Good day, I want to access the XML response and echo it to display its value but I don't know how to do it. I already tried some few answers in StackOverflow but I fail.
This is my code.
<?php
error_reporting(E_ALL);
require_once 'ruFunctions.php';
$rentalsUnited = new rentalsUnited();
$ru= $rentalsUnited->getOwners();
if($ru != null){
$data= simplexml_load_string($ru);
var_dump($data); // it will return boof(false)
var_dump($ru);
echo $data->Pull_ListAllOwners_RS->Status['ID']; //Trying to get property of non-object
}
?>
Results for var_dump($ru);
object(SimpleXMLElement)#2 (3) {
["Status"]=>
string(7) "Success"
["ResponseID"]=>
string(32) "44065d9888304e8cba912bce4d131ab1"
["Owners"]=>
object(SimpleXMLElement)#3 (1) {
["Owner"]=>
object(SimpleXMLElement)#4 (7) {
["#attributes"]=>
array(1) {
["OwnerID"]=>
string(6) "429335"
}
["FirstName"]=>
string(5) "Test"
["SurName"]=>
string(7) "Tester"
["CompanyName"]=>
string(15) "Test Helpers"
["Email"]=>
string(23) "info#Test.com"
["Phone"]=>
string(12) "+13474707707"
["UserAccountId"]=>
string(3) "602"
}
}
}
It looks like $ru is already a SimpleXMLElement, so trying to call simplexml_load_string will fail on this.
You can see some of the details by
if($ru != null){
echo $ru->Status;
}
You can (probably) list the owners by...
if($ru != null){
foreach ($ru->Owners->Owner as $owner ) {
echo "ownerId=".$owner['OwnerID'].PHP_EOL;
echo "FirstName=".$owner->FirstName.PHP_EOL;
}
}

PHP loop through nested JSON to find value

I am trying to loop through nested JSON data to find a name with a specific string and extract the string value associated with that. In this case the name string is "myKey" and the value string is "12345678".
After looking at several similar questions and documentation, I have tried different approaches such as doing it with objects or associative arrays, but still end up not being able to get to the information I want or receive errors.
Types of errors:
Notice: Array to string conversion
Warning: Invalid argument supplied for foreach()
Trying to get property of non-object
Here is a snippet of the decoded JSON using $myObj = json_decode($result);
object(stdClass)#4 (3) {
["info"]=>
object(stdClass)#5 (10) {
.
.
.
}
["stuff"]=>
array(1) {
.
.
.
}
["result"]=>
array(3) {
[0]=>
object(stdClass)#7 (3) {
["name"]=>
["value"]=>
["description"]=>
}
[1]=>
object(stdClass)#8 (2) {
["name"]=>
string(4) "Units"
["value"]=>
array(2) {
[0]=>
array(6) {
[0]=>
object(stdClass)#9 (3) {
.
.
.
}
.
.
.
[5]=>
object(stdClass)#14 (2) {
["name"]=>
string(10) "Components"
["value"]=>
array(1) {
[0]=>
array(14) {
[0]=>
object(stdClass)#15 (3) {
.
.
.
}
[1]=>
object(stdClass)#16 (3) {
["name"]=>
string(5) "myKey"
["value"]=>
string(8) "12345678"
["description"]=>
}
.
.
.
Here is a snippet of the PHP code I tried:
$myObj = json_decode($result);
// or I have tried
// $myObj = json_decode($result, true);
// here are different snippets of code I tried
foreach($myObj->result as $test) {
echo '<pre>';
print_r($test->name);
echo "<br>";
if ($test->name == "Units") {
$resultName = $test->name;
echo $resultName . "<br>";
}
echo '</pre>';
}
/*
foreach($myObj->result as $test) {
echo $test . "<br>";
foreach($test->name as $test1) {
echo $test1 . "<br>";
foreach($test1->value as $test2) {
echo $test2 . "<br>";
}
}
}
*/
/*
foreach($myObj->result as $test) {
if (($test->name) == "Units") {
// grab the value that corresponds to the name
$units = $test->name;
if (($units->name) == "Components") {
$components = $units->name;
print_r($components);
}
}
}
*/
I can access what I need directly, by saying:
print_r($myObj->result[1]->value[0][5]->value[0][1]->name);
print_r($myObj->result[1]->value[0][5]->value[0][1]->value);
but the location of the value may vary, so I need to find the names of the objects by looping
Can anyone provide a better approach using objects (or possibly even associative arrays)?
UPDATED TO INCLUDE SNIPPET OF ORIGINAL JSON (before decode)
string(21420) "{
"info": {
.
.
.
},
"stuff": [{
"name":
"type":
.
.
.
}],
"result": [
{
"name":
"value":
"description":
},
{
"name": "Units",
"value": [
[
{
"name":
"value":
"description":
},
.
.
.
{
"name": "Components",
"value": [
[
{
"name":
"value":
"description":
},
{
"name": "myKey",
"value": "12345678",
"description":
},
.
.
.
] (end inner Components value)
] (end outer Components value)
] (end inner Units value)
] (end outer Units value)
} (end results Units)
] (end result)
} (end opening)
It feels like you need some recursive function (a function that calls itself until it finds the the result) to find the value within the nested array.
Take a look at Recursive array_search
Of course you will have to change the function in that question, but I have similar issue once and it was very userful.
Below is the code for encoding and decoding. hope this helps you
//Encoding
//Array Values
$a1=array("name"=>"abc", "value"=>"def", "description"=>"ghi");
$a2=array("name"=>"jkl", "value"=>"mno", "description"=>"pqr");
$a3=array("name"=>"stu", "value"=>"wxy", "description"=>"zab");
//Array of Value Arrays
$info=array($a1, $a2, $a3);
$result=array($a1, $a2, $a3);
$stuff=array($a1, $a2, $a3);
//The Main Enclosed Array
$main=json_encode(array("info"=>$info, "result"=>$result, "stuff"=>$stuff));
//Decoding
//Fetching the name from result's $a1 array
$main_array=json_decode($main);
echo $main_array->result[1]->name; //Displays jkl

Update Check with PHP

I'm working on an update system that checks a remote file string
$local = simplexml_load_file(root_p.'/version.xml');
$remote = simplexml_load_file("mygithuburltoblob/version.xml");
if($local->build == $remote->build) {
} else {
echo "Version ".$remote->version." Available now";
}
But even if the build numbers match it still returns that the update is available. Does anyone know why that would be?
(Yes root_p is already defined, the problem isn't loading and retrieving the values)
Remote Var Dump
object(SimpleXMLElement)#12 (6) { ["title"]=> string(11) "Loopy Cubix" ["author"]=> string(12) "Morgan Green" ["version"]=> string(3) "1.0" ["build"]=> string(4) "1111" ["type"]=> string(5) "Alpha" ["feed"]=> object(SimpleXMLElement)#15 (0) { } }
Local Var Dump
object(SimpleXMLElement)#11 (6) { ["title"]=> string(24) "Looped Cubix Pre Release" ["author"]=> string(12) "Morgan Green" ["version"]=> string(3) "1.0" ["build"]=> string(4) "1111" ["type"]=> string(6) "Closed" ["feed"]=> object(SimpleXMLElement)#15 (0) { } }
On the top of the page is my output from
<?php
$local = simplexml_load_file(root_p.'/version.xml');
$remote = simplexml_load_file("https://raw.githubusercontent.com/Doxramos/Invontrol/master/version.xml");
echo "Local: ". gettype($local->build);
foreach($local->build as $build) {
echo $build. "<br />";
}
echo "Remote: ". gettype($remote->build);
foreach($remote->build as $build) {
echo $build. "<br />";
}
Shows both as an object with the same value.
As I see that oject is no equal, example of some compared elements:
["title"]=> string(11) "Loopy Cubix"
["title"]=> string(24) "Looped Cubix Pre Release"
The issue had to do with whitespace while parsing the XML data. I ended up fixing it by replacing
if($remote->build == $local->build) {
}
else {
//Output Update Information
}
with
$trimmed_local = trim($local->build);
$trimmed_remote = trim($remote->build);
And using the new variables as my comparison operators
if($trimmed_local == $trimmed_remote) {
}
else {
//Output Update Information
}

json Problems to echo, arrays

I am working on a script, that will use steam api, and i selected to use json for the response format.
So i have used var_dump with and without jason_decode() and it appears to be ok.
But can't manage to print it out, or echo it.
Script that gets the json data
<?php
$id = $_GET['SteamId'];
$get = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=API_KEY_REMOVED_FOR_SECURITY&steamids=$id",true);
$data = json_decode($get);
//var_dump($data);
echo $data->realname;
?>
So, that i get using the var_dump with json_decode is this.
object(stdClass)#1 (1) { ["response"]=> object(stdClass)#2 (1) { ["players"]=> array(1) { [0]=> object(stdClass)#3 (15) { ["steamid"]=> string(17) "76561198053511970" ["communityvisibilitystate"]=> int(3) ["profilestate"]=> int(1) ["personaname"]=> string(9) "Undefined" ["lastlogoff"]=> int(1340978067) ["profileurl"]=> string(41) "http://steamcommunity.com/id/Heisteknikk/" ["avatar"]=> string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd.jpg" ["avatarmedium"]=> string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd_medium.jpg" ["avatarfull"]=> string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/5c/5c75278da69102d9c8290bccd1becbb4081954cd_full.jpg" ["personastate"]=> int(1) ["realname"]=> string(7) "Andreas" ["primaryclanid"]=> string(18) "103582791430704052" ["timecreated"]=> int(1322427688) ["loccountrycode"]=> string(2) "NO" ["locstatecode"]=> string(2) "09" } } } }
And the raw data from the json.
{
"response": {
"players": [
{
"steamid": "76561198053511970",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "Undefined",
"lastlogoff": 1340978067,
"profileurl": "http:\/\/steamcommunity.com\/id\/Heisteknikk\/",
"avatar": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd.jpg",
"avatarmedium": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd_medium.jpg",
"avatarfull": "http:\/\/media.steampowered.com\/steamcommunity\/public\/images\/avatars\/5c\/5c75278da69102d9c8290bccd1becbb4081954cd_full.jpg",
"personastate": 1,
"realname": "Andreas",
"primaryclanid": "103582791430704052",
"timecreated": 1322427688,
"loccountrycode": "NO",
"locstatecode": "09"
}
]
}
}
I've been searching around on google about printing the json using "echo $data->realname;".
So i don't know what i did wrong, so it canno't echo the data.
Intro
Hey there, this is a REALLY old question, but since there's no answer here.
You do have it right when it comes to decoding the JSON PHP has the built-in function json_decode which can easily decode the function for you:
$json = '{...}';
$obj = json_decode($json, [ASSOC_ARRAY = FALSE]);
The second Param you can set to bool for an associative array which would change mentions from:
$obj->response;
To
$obj['response'];
Response
Now you say you need the realname from the players, that would be as simple as getting it via the player's position in the array, or just looping through the array:
echo $obj->response->players[0]->realname; // Andreas
Or if you have multiple users:
for( $i = 0; $i < count($obj->response->players); $i++ ) {
echo $obj->response->players[$i]->realname;
}
Which would display all of the realname(s)

Categories