0

下面是我从 php 方法中的 REST webservice 调用响应中得到的 xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <products>
      <capacity>1</capacity>
      <id>ae123</id>
      <group>Per</group>
      <name>xxxx</name>
    </products>
    <records>1
    </records>
</response>

我调用 web 服务并获取 xml 数据的 php 方法是

function getAjaxProducts(){

    $path="my webservice url";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:     application/json'));
    $retValue = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if($httpCode>400) {
            $retValue="<error><errorcode>".$httpCode."</errorcode></error>";
            echo $retValue;
    }

    curl_close($ch);
    echo $retValue;
 }

$retValue包含 xml 文件。

我完全陌生php,不知道如何在 xml 的数据表中显示产品详细信息。对此有任何想法吗?

4

1 回答 1

0

simplexml_load_string()应该做的伎俩

$retValue = '<response>
  <products>
      <capacity>1</capacity>
      <id>ae123</id>
      <group>Per</group>
      <name>xxxx</name>
  </products>
  <records>1
  </records>
</response>';


$xml = simplexml_load_string($retValue);
echo $xml->products->capacity . "<br>\n";
echo $xml->products->group . "<br>\n";
echo $xml->products->name . "<br>\n";
echo $xml->records . "<br>\n";

看到它在行动

于 2013-02-08T13:52:52.707 回答