I am working with an xml file that I am trying to parse into json format and then decode to an array. I accomplished this mainly using the built in simplexml_load_string and then json_encode. The issue is when calling simplexml_load_string the xml isn’t fully preserved. It seems like the child nodes for video show as object(stdClass). How could I get all values of the xml file? Link to XML
Code:
$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home') );
$json = json_encode($xml);
Result:
["results"]=>
object(stdClass)#183 (4) {
["previousPage"]=>
object(stdClass)#184 (1) {
["#attributes"]=>
object(stdClass)#185 (1) {
["exists"]=>
string(5) "false"
}
}
["nextPage"]=>
string(1) "2"
["total"]=>
string(2) "40"
["resultList"]=>
object(stdClass)#186 (1) {
["video"]=>
array(20) {
[0]=>
object(stdClass)#187 (7) {
["#attributes"]=>
object(stdClass)#188 (2) {
["id"]=>
string(7) "2329124"
["type"]=>
string(3) "960"
}
["description"]=>
object(stdClass)#189 (0) {
}
["created"]=>
string(25) "2015-02-18 04:04:52 +0000"
["duration"]=>
string(2) "86"
["images"]=>
object(stdClass)#190 (2) {
["image"]=>
object(stdClass)#191 (1) {
["#attributes"]=>
object(stdClass)#192 (3) {
["id"]=>
string(8) "13503818"
["width"]=>
string(3) "100"
["height"]=>
string(3) "100"
}
}
["thumbnail"]=>
object(stdClass)#193 (1) {
["#attributes"]=>
object(stdClass)#194 (3) {
["id"]=>
string(8) "13503819"
["width"]=>
string(3) "372"
["height"]=>
string(3) "210"
}
}
}
["videoFiles"]=>
object(stdClass)#195 (1) {
["file"]=>
object(stdClass)#196 (1) {
["#attributes"]=>
object(stdClass)#197 (3) {
["id"]=>
string(8) "14704560"
["formatId"]=>
string(3) "400"
["uploaded"]=>
string(4) "true"
}
}
}
["categories"]=>
object(stdClass)#198 (1) {
["category"]=>
string(21) "UEFA Champions League"
}
}
I would suggest just try to parse those values using SimpleXML alone and stick with it. Just access those properties properly. As for those nodes which have been wrapped with character data in it, cast them as (string).
$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home'));
foreach($xml->results->resultList->video as $video) {
$description = (string) $video->description;
$created = $video->created;
$duration = $video->duration;
$image = $video->images->image;
$thumbnail = (string) $video->images->image;
$video_file = (string) $video->videoFiles->file;
$categories = (string) $video->categories->category;
echo "
Description: $description <br/>
Created: $created <br/>
Duration: $duration <br/>
Categories: $categories <br/>
<hr/>
";
}
Sample Output
Related
Im trying many days now to select data from this db
[1]: https://i.stack.imgur.com/QA34L.jpg
I want to print for example all comments
echo $comment['username']." | "; \ Alex | Alex
My php code so far:
[![$m= new MongoDB\Client ("mongodb://127.0.0.1/");
$db = $m->stores;
$collection = $db->storeinfo;][1]][1]
$storez = $collection->find(array("Products.pTHUMBNAIL" => $pThumb));
$o=1;
$afm=array();
foreach ($storez as $stor) {
$afm[$o] = $stor['AFM'];
$record = $collection->findOne(array(
"AFM" => $afm[$o],"Products.pTHUMBNAIL" => $pThumb));
foreach ($record['Products'] as $pro){
if($pThumb == $pro['pTHUMBNAIL']){
echo $temp = $pro['pID']." ";
foreach($pro as $pro1['pCOMMENTS']) {
foreach($pro1 as $com['Comment']) {
var_dump($com['Comment']);
/*
foreach($com as $comment) {
echo $comment['username'];
}
*/
}
}
}
}
$o += 1;
}
It seems that i just cannot find the correct foreach to loop through my Comment array
var_dump output:
099360111/1 object(MongoDB\BSON\ObjectId)#55 (1) { ["oid"]=> string(24) "6003403a695900000c002649" } string(11) "099360111/1" string(9) "Old Skool" string(2) "75" string(4) "Vans" string(25) "Leather and textile upper" string(2) "44" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "7" object(MongoDB\Model\BSONArray)#65 (1) { ["storage":"ArrayObject":private]=> array(1) { [0]=> object(MongoDB\Model\BSONDocument)#10 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#73 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#45 (1) { ["milliseconds"]=> string(13) "1611028053000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } } } 099360666/1 object(MongoDB\BSON\ObjectId)#44 (1) { ["oid"]=> string(24) "6006563a3f1c0000c80034a8" } string(11) "099360666/1" string(12) "old school 2" string(2) "50" string(4) "Vans" string(11) "black/white" string(8) "42,43,43" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "6" object(MongoDB\Model\BSONArray)#79 (1) { ["storage":"ArrayObject":private]=> array(2) { [0]=> object(MongoDB\Model\BSONDocument)#7 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#39 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#68 (1) { ["milliseconds"]=> string(13) "1611028089000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } [1]=> object(MongoDB\Model\BSONDocument)#78 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#77 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#76 (1) { ["milliseconds"]=> string(13) "1611030745000" } ["text"]=> string(8) "good!!!!" ["rating"]=> string(1) "5" } } } } } }
What about this?
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach ($comments['Comment'] as $comment) {
echo $comment['text'];
}
}
You used foreach() bad: the index should be added to the first parameter, not in the 'as' part.
Also it helps if you use more clear variable names. That doesn't take too much, but makes it more readable and easy to debug.
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach($comments as $comment) {
echo $comment['text'];
}
}
I'd appreciate if someone could help me to solve this issue.
I'm getting this XML by reading a XML file through de code:
$x = simplexml_load_file($link);
$g = $x->attributeGroup;
foreach($g->xpath("//PARAMETER") as $attr){
var_dump((string)$attr->attributes()->ID);
var_dump((string)$attr->attributes()->VALUE);
var_dump((string)$attr); // for text value
}
I need to get the VALUE inside ID=sitPag, but I'm not able to.
Could anybody help me to do that?
<consulta>
<PARAMETER>
<PARAM ID="CodEmp" VALUE="1234"/>
<PARAM ID="Pedido" VALUE="00010714"/>
<PARAM ID="Valor" VALUE="351,20"/>
<PARAM ID="tipPag" VALUE="02"/>
<PARAM ID="sitPag" VALUE="00"/>
<PARAM ID="ValorPago" VALUE="000000351,20"/>
<PARAM ID="dtPag" VALUE="15012020"/>
<PARAM ID="codAut" VALUE=""/>
<PARAM ID="numId" VALUE=""/>
<PARAM ID="compVend" VALUE=""/>
<PARAM ID="tipCart" VALUE=""/>
</PARAMETER>
</consulta>
After calling simplexml_load_file(), I'm gettin it:
object(SimpleXMLElement)#3 (1) { ["PARAMETER"]=> object(SimpleXMLElement)#5 (1) { ["PARAM"]=> array(11) { [0]=> object(SimpleXMLElement)#6 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(6) "CodEmp" ["VALUE"]=> string(26) "1234" } } [1]=> object(SimpleXMLElement)#7 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(6) "Pedido" ["VALUE"]=> string(8) "00010714" } } [2]=> object(SimpleXMLElement)#8 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(5) "Valor" ["VALUE"]=> string(6) "351,20" } } [3]=> object(SimpleXMLElement)#9 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(6) "tipPag" ["VALUE"]=> string(2) "02" } } [4]=> object(SimpleXMLElement)#10 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(6) "sitPag" ["VALUE"]=> string(2) "00" } } [5]=> object(SimpleXMLElement)#11 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(9) "ValorPago" ["VALUE"]=> string(12) "000000351,20" } } [6]=> object(SimpleXMLElement)#12 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(5) "dtPag" ["VALUE"]=> string(8) "15012020" } } [7]=> object(SimpleXMLElement)#13 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(6) "codAut" ["VALUE"]=> string(0) "" } } [8]=> object(SimpleXMLElement)#14 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(5) "numId" ["VALUE"]=> string(0) "" } } [9]=> object(SimpleXMLElement)#15 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(8) "compVend" ["VALUE"]=> string(0) "" } } [10]=> object(SimpleXMLElement)#16 (1) { ["#attributes"]=> array(2) { ["ID"]=> string(7) "tipCart" ["VALUE"]=> string(0) "" } } } } }
It might not be the most efficient (you have to go through each PARAM element until the desired one is found, which could be many), but this will get the job done.
$x = simplexml_load_string($xmlString);
foreach($x->PARAMETER->PARAM as $param){
if((string)$param->attributes()->ID == "sitPag")
{
echo (string)$param->attributes()->VALUE;
break;
}
}
DEMO
Use Xpath expression to fetch the nodes or values. SimpleXMLElement::xpath() returns an array of SimpleXMLElement instances.
$consulta = new SimpleXMLElement($xml);
$params = $consulta->xpath('PARAMETER/PARAM[#ID="sitPag"]');
$sitPag = isset($params[0]) ? (string)$params[0]['VALUE'] : '';
var_dump($sitPag);
In DOM you can use DOMXpath::evaluate() to fetch the attribute value directly:
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$sitPag = $xpath->evaluate('string(/consulta/PARAMETER/PARAM[#ID="sitPag"]/#VALUE)');
var_dump($sitPag);
In DOM you can use DOMXpath::evaluate() to fetch the attribute value directly:
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$sitPag = $xpath->evaluate('string(/consulta/PARAMETER/PARAM[#ID="sitPag"]/#VALUE)');
var_dump($sitPag);
Or
$x = simplexml_load_string($xmlString);
foreach($x->PARAMETER->PARAM as $param){
if((string)$param->attributes()->ID == "sitPag")
{
echo (string)$param->attributes()->VALUE;
break;
}
}
I want to parse JSON that I recieve from steam via api link http://steamcommunity.com/id/theorangeass/inventory/json/753/1/ , but when I try to print it with echo it show nothing.
Here is the code
$data = file_get_contents('http://steamcommunity.com/id/theorangeass/inventory/json/753/1/');
$json = json_decode($data, true);
echo $json->success;
var_dump:
array(6) { ["success"]=> bool(true) ["rgInventory"]=> array(1) { ["922506184369981039"]=> array(5) { ["id"]=> string(18) "922506184369981039" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["amount"]=> string(1) "1" ["pos"]=> int(1) } } ["rgCurrency"]=> array(0) { } ["rgDescriptions"]=> array(1) { ["1254492673_2070301907"]=> array(18) { ["appid"]=> string(3) "753" ["classid"]=> string(10) "1254492673" ["instanceid"]=> string(10) "2070301907" ["icon_url"]=> string(116) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiCJoFB3O541FNc9ZPYXYjjL7UqfFEwOtgZKcs0eWlClqzSJn6" ["icon_url_large"]=> string(106) "U8721VM9p9C2v1o6cKJ4qEnGqnE7IoTQgZI-VTdwyTBeimAcIowbqB-harb00cJ0fNdiA54UEGOnqGQPJ9hDZHA50feEo7RMyO_GQNzkkA" ["icon_drag_url"]=> string(0) "" ["name"]=> string(4) "BEEP" ["market_name"]=> string(0) "" ["name_color"]=> string(0) "" ["background_color"]=> string(0) "" ["type"]=> string(4) "Gift" ["tradable"]=> int(0) ["marketable"]=> int(0) ["commodity"]=> int(0) ["cache_expiration"]=> string(20) "2017-01-02T00:00:00Z" ["fraudwarnings"]=> array(1) { [0]=> string(223) "This is a restricted gift which can only be redeemed in these countries: Armenia, Azerbaijan, Belarus, Georgia, Kyrgyzstan, Kazakhstan, Moldova, Republic of, Tajikistan, Turkmenistan, Uzbekistan, Ukraine, Russian Federation" } ["descriptions"]=> array(1) { [0]=> array(1) { ["value"]=> string(216) "A combination of Yoshi’s Island-style platforming with a gravity gun right out of Half-Life, BEEP is an amazing physics-platformer. Despite its friendly art style, this is a hardcore platformer in the truest sense." } } ["actions"]=> array(1) { [0]=> array(2) { ["name"]=> string(13) "View in store" ["link"]=> string(41) "http://store.steampowered.com/app/104200/" } } } } ["more"]=> bool(false) ["more_start"]=> bool(false) }
You are trying to echo a boolean. If you are wanting to echo true you're going to need to do an if, or a switch statement. An if statement would probably be easiest.
echo $json->success == true ? 'TRUE' : 'FALSE';
To parse the array to retrieve the item id's in the rgInventory to must do a foreach.
foreach ($json['rgInventory'] as $item) {
echo $item['id'];
}
Where you should start learning about arrays is Here
I am obtaining a json object using the following:
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
var_dump($data);
This gives me something like this:
array(2) { ["ok"]=> bool(true) ["result"]=> array(1) { [0]=> array(2)
{ ["update_id"]=> int(44893465) ["message"]=> array(5) {
["message_id"]=> int(16) ["from"]=> array(3) { ["id"]=> int(29595794)
["first_name"]=> string(3) "Bob" ["username"]=> string(14) "Bobo" }
["chat"]=> array(3) { ["id"]=> int(29595794) ["first_name"]=>
string(3) "Bob" ["username"]=> string(14) "Bobo" } ["date"]=>
int(1435354253) ["text"]=> string(7) "/q 3.33" } } } }
I would then like to add certain values into variables. For example I would like to extract username, text, message_id, etc
But no matter what I try my variables are empty:
//let's test it
echo "Username: " . $data[1][0]["username"];
//another test
echo $data->username;
Neither of these are working and my research has not helped me find a solution. I am stumped on this one.
Any pointers in the right direction would really be appreciated.
array(2) {
["ok"]=> bool(true)
["result"]=> array(1)
{
[0]=> array(2)
{
["update_id"]=> int(44893465)
["message"]=> array(5)
{
["message_id"]=> int(16)
["from"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["chat"]=> array(3)
{
["id"]=> int(29595794)
["first_name"]=> string(3) "Bob"
["username"]=> string(14) "Bobo"
}
["date"]=> int(1435354253)
["text"]=> string(7) "/q 3.33"
}
}
}
}
You are using wrong array index. $data[1][0]["username"]; not exists.
$data["result"][0]["message"]["from"]["username"];
$data["result"][0]["message"]["chat"]["username"];
This will give you the proper username
$json = file_get_contents("url-here");
$data = json_decode($json, true);
//test
echo $data["result"][0]['message']['from']['username'];
output
Bobo
I have the following data that I parsed from an xml file and now I have a problem returning the data. How do i return the photo src data from the array within the photo array. Any ideas as to what I am doing wrong?
Code
$xml = simplexml_load_file($url);
$photo_url = $xml['photo']->src;
for ($i=0, $n=count($photo_url); $i<$n; ++$i) {
echo $photo_url[$i].'<br>';
}
Data
["photo"]=>
array(46) {
[0]=>
object(SimpleXMLElement)#2 (1) {
["#attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006416.jpg"
["thumb"]=>
string(42) "1006416_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "01.jpg"
["favorite"]=>
string(0) ""
}
}
[1]=>
object(SimpleXMLElement)#3 (1) {
["#attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006417.jpg"
["thumb"]=>
string(42) "1006417_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "02.jpg"
["favorite"]=>
string(0) ""
}
}
You should use the SimpleXMLElement attributes method, like so:
$xml = simplexml_load_file($url);
foreach( $xml as $xml_node)
{
$attributes = $xml_node->attributes();
echo 'Photo source: ' . $attributes['src'] . "\n";
}
Demo