-7
<?xml version="1.0"?>
<ABCD xmlns="http://abcd.com">
<PRODUCT xmlns="">
 <CNHEADER>
  <CNTRACK>true</CNTRACK>
  <FIELD name="PRODUCTNO" value="BK201122"/>
  <FIELD name="ProductType" value="DP"/>
  <FIELD name="strProdCode" value="NL1754"/>

Here i want the value of PRODUCTNO and so on.

i tried this but still not getting any output

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_URL, $url); // get the url contents 

$data = curl_exec($ch); // execute curl request 

curl_close($ch); 

$xml = simplexml_load_string($data); 

echo $value = (string) $xml->FIELD[0]->attributes()->name; 
8
  • Hello Ravi, you should take a look at XML Parser utilities (like SimpleXML) : php.net/manual/fr/simplexml.examples-basic.php Commented Sep 20, 2018 at 7:32
  • 4
    Possible duplicate of PHP How to read XML Child Node Value in a String using PHP? Commented Sep 20, 2018 at 7:33
  • 1
    Please do some proper research and make some attempts before posting. There are plenty of guides about reading XML with PHP out there (and here on SO). Try any of those first. If you still can't get it working, you're welcome to come back to show us what you've tried, explain where you're stuck and we can help you from there but we won't write it all for you. Commented Sep 20, 2018 at 7:37
  • 1
    You're just asking for code and SO isn't a free coding service. You're expected to write your own code. We can help you if you get stuck on something specific with your existing code. Please visit the help center and read the guidelines. Commented Sep 20, 2018 at 7:39
  • 1
    Have you read the comment posted by @Tom Udding? So try that solution and then ask if you have some errors Commented Sep 20, 2018 at 7:46

1 Answer 1

1

Using SimpleXMLElement() you can get your product data from xml string; ref

// Your xml string
$xmlString = '<?xml version="1.0"?>
<ABCD xmlns="http://abcd.com">
<PRODUCT xmlns="">
 <CNHEADER>
  <CNTRACK>true</CNTRACK>
  <FIELD name="PRODUCTNO" value="Z41346020"/>
  <FIELD name="ProductType" value="DP"/>
  <FIELD name="strProdCode" value="NL1754"/>
 </CNHEADER>
</PRODUCT>
</ABCD>';

$xmlData = new SimpleXMLElement($xmlString);

$productData = [];
foreach ($xmlData->PRODUCT->CNHEADER->FIELD as $productField) {
    $productData[(string)$productField{'name'}] = (string)$productField{'value'};
}

echo "<pre>";
print_r($productData);
exit;

You will get following output

// Output
Array
(
    [PRODUCTNO] => Z41346020
    [ProductType] => DP
    [strProdCode] => NL1754
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.