为每个属性单独打印xml响应

时间:2015-05-04 12:11:44

标签: php arrays xml api curl

我正在尝试将此XML转换为数组这是我从API获得的响应,我想获取statuscode的值,但我无法做到。

<?xml version='1.0' encoding='utf-8' standalone='no'?>
<REGISTRATIONRESPONSE>
<STATUSCODE>20</STATUSCODE>
<STATUS>1234</STATUS> 
</REGISTRATIONRESPONSE>

但是当我使用以下代码时

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'RequestData='.$post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);
$resultres=xml2ary($result);
echo '<pre>';
print_r($resultres);

输出

Array
 (
    [string] => Array
    (
            [_a] => Array
            (
                [xmlns] => http://tempuri.org/
            )
            [_v] => 201234
    )
)

结果如何变化?我还使用了另外一种技术但是我也没有得到所需的结果

2 个答案:

答案 0 :(得分:1)

查看SimpleXML

<?php

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,'RequestData='.$post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);

/** @var SimpleXMLElement $xml */
$xml = simplexml_load_string($result);

// nice, use SimpleXMLElement object
var_dump($xml);

// not nice, use array instead
var_dump((array) $xml);

只需确保您从API获取的XML响应与您上面发布的相同。这里再次作为参考:

<?xml version='1.0' encoding='utf-8' standalone='no'?>
<REGISTRATIONRESPONSE>
    <STATUSCODE>20</STATUSCODE>
    <STATUS>1234</STATUS>
</REGISTRATIONRESPONSE>

这是一个没有cURL的示例,只需将其添加到文件中并从终端运行它:

<?php

$xmlString = <<<XML
<?xml version='1.0' encoding='utf-8' standalone='no'?>
    <REGISTRATIONRESPONSE>
    <STATUSCODE>20</STATUSCODE>
    <STATUS>1234</STATUS>
</REGISTRATIONRESPONSE>
XML;

$xml = simplexml_load_string($xmlString);

// everything
var_dump($xml);

// independent values
var_dump((string) $xml->STATUSCODE);
var_dump((string) $xml->STATUS);

// now with arrays
$array = (array) $xml;

// everything
var_dump($array);

// independent values
var_dump($array['STATUSCODE']);
var_dump($array['STATUS']);

// want to print it?

echo sprintf('Registration responded with status code %d and status %d', $xml->STATUSCODE, $xml->STATUS) . PHP_EOL;
echo sprintf('Registration responded with status code %d and status %d', $array['STATUSCODE'], $array['STATUS']) . PHP_EOL;

答案 1 :(得分:0)

如果XML正在通过,例如:

<class>
    <type>
        <input>Text</input>
        <input>Text</input>
    </type>
    <type>
        <input>Text</input>
        <input>Text</input>
    </type>
    <type>
        <input>Text</input>
        <input>Text</input>
    </type>
</class>

你能做的是:

$xml = simplexml_load_file("filepath");
$classes = $xml->$class;
foreach ($classes as $keyClass => $class) {
    echo "<ul><li>Class</li><ul>";
    $types = $class->type;
    foreach ($types as $keyType => $type) {
        echo "<li>Type</li><ul>";
        $inputs = $type->input;
        foreach ($inputs as $keyInput => $input) {
            echo "<li>Input</li>";
        }
        echo "</ul>";
    }
    echo "</ul></ul>";
}