-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.

4
  • i am not clear about your point..can you clarify please... Commented Mar 13, 2016 at 8: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. Commented Mar 13, 2016 at 8:50
  • Possible duplicate of How to get response using cURL in PHP Commented Mar 13, 2016 at 8:50
  • still i am stuck on this issue. can anyone please help... Commented Mar 13, 2016 at 9:38

1 Answer 1

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 ..
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.