-1

i am hitting a URL/API in browser and getting below xml response from the server.

<test xmlns:taxInfoDto="com.message.TaxInformationDto">
<response>
 <code>0000</code>
 <description>SUCCESS</description>
</response>
<accounts>
 <account currency="BDT" accAlias="6553720">
 <currentBalance>856.13</currentBalance>
 <availableBalance>856.13</availableBalance>
 </account>
</accounts>
<transaction>
 <principalAmount>0</principalAmount>
 <feeAmount>0.00</feeAmount>
 <transactionRef>2570277672</transactionRef>
 <externalRef/>
 <dateTime>09/03/2016</dateTime>
 <userName>01823074838</userName>
 <taxInformation totalAmount="0.00"/>
 <additionalData/>
 </transaction>
</test>

Now i want to parse this xml response and assign it to a variable so that i can use this variable value in anywhere.i am using below PHP code.

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://x.x.x.x:/ussd/process?     destination=BANGLA&userName=&secondarySource=01");
curl_setopt($ch, CURLOPT_HEADER, 0);
$retValue = curl_exec($ch);
return $retValue;
?>

and i am getting below output.

0000SUCCESS856.13 856.13 00.00257027770913/03/201601823074838

Can anyone please help me how can i parse each value and assign it to a variable.

bKashOST
  • 149
  • 4
  • 11
  • i am not clear about your point..can you clarify please... – bKashOST Mar 13 '16 at 08:46
  • You either have to add another option with curlopt to get curl to return the response body, or use output buffering to capture the output. – GordonM Mar 13 '16 at 08:50
  • Possible duplicate of [How to get response using cURL in PHP](http://stackoverflow.com/questions/6516902/how-to-get-response-using-curl-in-php) – GordonM Mar 13 '16 at 08:50
  • still i am stuck on this issue. can anyone please help... – bKashOST Mar 13 '16 at 09:38

1 Answers1

1

A possible solution could be to add the CURLOPT_RETURNTRANSFER option:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

From the manual:

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

You can use for example simplexml_load_string to load the returned string and access its properties:

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://x.x.x.x:/ussd/process?     destination=BANGLA&userName=&secondarySource=01");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$retValue = curl_exec($ch);

$simpleXMLElement = simplexml_load_string($retValue);
$description = (string)$simpleXMLElement->response->description;
$username = (string)$simpleXMLElement->transaction->userName;
// etc ..
The fourth bird
  • 154,723
  • 16
  • 55
  • 70