Say I have a xml file like the one below:
<objectlist>
<objectcode>OP#0003</objectcode>
<objectid>0001</objectid>
<objecttype>Test object</objecttype>
<object>
<info>
<id>001</id>
<name>Some name</name>
<value>5</value>
</info>
<properties>
<shopdetails>
<desciption>
<header>Test</header>
<text>This is some text about the object</text>
</desciption>
<price>4</price>
<currency>Dollar</currency>
<weight>500</weight>
<gramSymbol>mg</gramSymbol>
</shopdetails>
</properties>
</object>
</objectlist>
How do I get all the information out of this XML file using curl_setopt()
? I tried it with a foreach loop, but it failed. It only gets the information from: objectcode, object id and objecttype.
My script is only able to get information from childnodes of the root but not children of child nodes. I used this script for my project:
<?php
Class xmlObject{
public function xml_From_URL() {
require_once 'dbconnect.php';
$config[CURLOPT_URL] = "http://localhost/example.xml";
$config[CURLOPT_VERBOSE] = 0;
$config[CURLOPT_SSLVERSION] = 3;
$config[CURLOPT_SSL_VERIFYPEER] = FALSE;
$config[CURLOPT_SSL_VERIFYHOST] = 2;
$config[CURLOPT_FOLLOWLOCATION] = 0;
$config[CURLOPT_HEADER] = 0;
$config[CURLOPT_RETURNTRANSFER] = 1;
//-- config section --//
$tuCurl = curl_init();
curl_setopt_array($tuCurl, $config);
$data = curl_exec($tuCurl);
$xml = simplexml_load_string($data);
//-- Loops --//
//-- 1 --//
foreach($xml -> object as $row){
$id = $row -> id;
$name = $row -> name;
$value = $row -> value;
echo("<b>Objects</b></br>");
echo($id."<br>");
echo($name."<br>");
echo($value."<br>");
}
//-- 2 --//
foreach ($xml -> description as $row) {
$header = $row -> header;
$text = $row -> text;
echo("<b>description</b><br>");
echo($header);
echo($text);
}
//-- 3 --//
foreach ($xml -> shopdetails as $row) {
$header = $row -> price;
$text = $row -> currency;
$weight = $row -> weight;
$gramsymbol = $row -> gramsymbol;
echo("<b>description</b><br>");
echo($header);
echo($text);
}
curl_close($tuCurl);
}
}
?>