• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 43
  • Last Modified:

How to get second layer output from XML and convert to php variable ?

Hi,

I have the following scripts :


<?

$xml="<whmcsapi>
<action>getinvoice</action>
<result>success</result>
<invoiceid>242347</invoiceid>
<invoicenum></invoicenum>
<userid>25209</userid>
<date>2017-12-21</date>
<duedate>2018-01-11</duedate>
<datepaid>0000-00-00 00:00:00</datepaid>
<lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
<subtotal>50.00</subtotal>
<credit>0.00</credit>
<tax>3.00</tax>
<tax2>0.00</tax2>
<total>53.00</total>
<balance>53.00</balance>
<taxrate>6.00</taxrate>
<taxrate2>0.00</taxrate2>
<status>Unpaid</status>
<paymentmethod>banktransfer</paymentmethod>
<notes></notes>
<ccgateway></ccgateway>
     <items>
                   <item>
                                <id>304494</id>
                                <type>Domain</type>
                                <relid>52025</relid>
                                <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
                                <amount>50.00</amount>
                                <taxed>1</taxed>
                  </item>
     </items>
<transactions></transactions>
</whmcsapi>";


$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

$content .= '<tr><th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
foreach ($xml['items']['item'] as $key => $value) {

  $content .= '<tr><td><b>Description</b></td><td>'.$value['description'].'</td></tr>';
  $content .= '<tr><td><b>Amount</b></td><td>'.$value['amount'].'</td></tr>';
}
	

	$content .= '</table>';

return $content;
?>

Open in new window



But i received the following error :

Warning: Illegal string offset 'items' in /home/internet/public_html/test/teststring4.php on line 42

Warning: Illegal string offset 'item' in /home/internet/public_html/test/teststring4.php on line 42

Warning: Invalid argument supplied for foreach() in /home/internet/public_html/test/teststring4.php on line 42


May i know how to get the variable inside the "items" and "item" ?

I have tried "foreach ($xml['items']['item'] as $key => $value)" but it produce the above error.

Hope anybody can help me by providing the correct script.
0
smksa
Asked:
smksa
2 Solutions
 
gr8gonzoConsultantCommented:
Right now, $xml is just a string that contains raw, unparsed XML. You can't loop through it any more than you could loop through the phrase "Hello world!". You can use the SimpleXML extension to parse the XML string into an object that you can then loop through.

Here's the documentation page on how to do it:
http://php.net/manual/en/simplexml.examples-basic.php
0
 
Ryan ChongCommented:
try:

<?php

$xml="<whmcsapi>
  <action>getinvoice</action>
  <result>success</result>
  <invoiceid>242347</invoiceid>
  <invoicenum></invoicenum>
  <userid>25209</userid>
  <date>2017-12-21</date>
  <duedate>2018-01-11</duedate>
  <datepaid>0000-00-00 00:00:00</datepaid>
  <lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
  <subtotal>50.00</subtotal>
  <credit>0.00</credit>
  <tax>3.00</tax>
  <tax2>0.00</tax2>
  <total>53.00</total>
  <balance>53.00</balance>
  <taxrate>6.00</taxrate>
  <taxrate2>0.00</taxrate2>
  <status>Unpaid</status>
  <paymentmethod>banktransfer</paymentmethod>
  <notes></notes>
  <ccgateway></ccgateway>
  <items>
    <item>
      <id>304494</id>
      <type>Domain</type>
      <relid>52025</relid>
      <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
      <amount>50.00</amount>
      <taxed>1</taxed>
    </item>
  </items>
  <transactions></transactions>
</whmcsapi>";

$content = '';

$doc = new SimpleXMLElement($xml);

$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

  //$content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
  $content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS</th></tr>';
  
  foreach ($doc->items->item as $node) {
    $content .= '<tr><td><b>Description</b></td><td>'.$node->description.'</td></tr>';
    $content .= '<tr><td><b>Amount</b></td><td>'.$node->amount.'</td></tr>';
  }
  
  $content .= '</table>';

  echo $content;
  //return $content;

?>

Open in new window


or:

<?php

$xml="<whmcsapi>
  <action>getinvoice</action>
  <result>success</result>
  <invoiceid>242347</invoiceid>
  <invoicenum></invoicenum>
  <userid>25209</userid>
  <date>2017-12-21</date>
  <duedate>2018-01-11</duedate>
  <datepaid>0000-00-00 00:00:00</datepaid>
  <lastcaptureattempt>0000-00-00 00:00:00</lastcaptureattempt>
  <subtotal>50.00</subtotal>
  <credit>0.00</credit>
  <tax>3.00</tax>
  <tax2>0.00</tax2>
  <total>53.00</total>
  <balance>53.00</balance>
  <taxrate>6.00</taxrate>
  <taxrate2>0.00</taxrate2>
  <status>Unpaid</status>
  <paymentmethod>banktransfer</paymentmethod>
  <notes></notes>
  <ccgateway></ccgateway>
  <items>
    <item>
      <id>304494</id>
      <type>Domain</type>
      <relid>52025</relid>
      <description>Domain Renewal - xxx.com - 1 Year/s (11/01/2018 - 10/01/2019)</description>
      <amount>50.00</amount>
      <taxed>1</taxed>
    </item>
  </items>
  <transactions></transactions>
</whmcsapi>";

$content = '';


$doc = new DOMDocument();
$doc->loadXML($xml);

$content .= '<table border="1" cellpadding="5" style="border-collapse: collapse;border: 1px solid #cacaca">';

  //$content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS '.print_r($xml).'</th></tr>';
  $content .= '<tr> <th colspan="2" bgcolor="thistle">ITEMS</th></tr>';
  
  foreach ($doc->getElementsByTagName('items') as $node) {
    $itms = preg_split("/\\r\\n|\\r|\\n/", $node->nodeValue);
    $content .= '<tr><td><b>Description</b></td><td>'.$itms[5].'</td></tr>';
    $content .= '<tr><td><b>Amount</b></td><td>'.$itms[6].'</td></tr>';
  }
  
  $content .= '</table>';

  echo $content;
  //return $content;

?>

Open in new window

0
 
smksaAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now