如何在php中使用CURL解析xml响应并分配给变量
我在浏览器中点击了一个URL/API,从服务器得到了低于xml的响应如何在php中使用CURL解析xml响应并分配给变量,php,xml,curl,Php,Xml,Curl,我在浏览器中点击了一个URL/API,从服务器得到了低于xml的响应 <test xmlns:taxInfoDto="com.message.TaxInformationDto"> <response> <code>0000</code> <description>SUCCESS</description> </response> <accounts> <account currency=
<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>
现在我想解析这个xml响应并将其分配给一个变量,这样我就可以在任何地方使用这个变量值
<?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;
?>
任何人都可以帮助我如何解析每个值并将其分配给变量。可能的解决方案是添加以下选项: 从手册中: 如果为TRUE,则将传输作为返回值的字符串返回 curl_exec()而不是直接输出 例如,您可以使用加载返回的字符串并访问其属性:
<?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 ..
响应->描述;
$username=(字符串)$simplexmlement->transaction->username;
//等等。。
我不清楚您的观点。请您澄清一下……您要么必须使用curlopt添加另一个选项以获取curl以返回响应正文,要么使用输出缓冲来捕获输出。我仍然无法解决此问题。有人能帮忙吗。。。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
<?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 ..