使用CURL调用XML的结果

时间:2012-10-13 01:43:19

标签: php xml api parsing

我今天已经处理了一些代码了好几个小时,想知道是否有人能指出我比现在更好的方向。

我有PHP代码通过send_request_via_curl($ host,$ path,$ content)发送XML来获取数据数组。

我的功能:

function send_request_via_curl($host,$path,$content)
{
$posturl = "https://" . $host . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml;charset=utf-8"));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    return $response;
}

$headers = array(
    "Content-type: text/xml;charset=utf-8",
    "Accept: application/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "Content-length: " . strlen($content),
    );


$content = '<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Body>
                <GetLocations xmlns="http://tempuri.org/">
                    <AuthLogin>ClientName</AuthLogin>
                    <UserName>MyUser</UserName>
                    <Password>SomePassword</Password>
                </GetLocations>
            </soap:Body>
            </soap:Envelope>';

$response = send_request_via_curl($host,$path,$content);

编辑:

我越来越近了,我实施了Baba的代码,现在得到了一个不同的错误:

警告:未知:......中不再存在节点

我有相同的代码,现在处理响应:

if ($response)
{

$xml = new SimpleXMLElement($response);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->GetLocationsResponse->GetLocationsResult->SearchLocations->children() as $table)
{
echo $table->Table->LocationName . "<br>";
}

这是真正的XML返回 - 我注意到Firebug使所有的XML都是小写的,而源代码的大写字母是混合的,我觉得这可能很重要。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetLocationsResponse xmlns="http://tempuri.org/">
<GetLocationsResult>
<SearchLocations>
<Table>
<LocationName>
</Table>
<Table>
<LocationId>103501</LocationId>
<LocationName>Albuquerque, New Mexico, USA</LocationName>
</Table>
<Table>
<LocationId>101600</LocationId>
<LocationName>Atlanta, Georgia, USA</LocationName>
</Table>
</SearchLocations>
</GetLocationsResponse>
</GetLocationsResult>
</soap:Body>
</soap:Envelope>

感谢!!!

2 个答案:

答案 0 :(得分:3)

您获得的回复不是HTML,而是XML,但看起来您的XML错误,标签无效或者您必须犯错误...请参阅

  <LocationName>Hawaii</Location>
                             ^--- it should be LocationName

应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <getlocationsresponse xmlns="http://tempuri.org">
            <getlocationsresult>
                <SearchLocations>
                    <Table>
                        <LocationID>10322</LocationID>
                        <LocationName>Hawaii</LocationName>
                    </Table>
                </SearchLocations>
            </getlocationsresult>
        </getlocationsresponse>
    </soap:Body>
</soap:Envelope>

阅读此XML

$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->getlocationsresponse->getlocationsresult->SearchLocations->children() as $table)
{
     echo $table->LocationName . "<br>";
}

输出

 Hawaii

------编辑--------

您的新XML再次出错......这应该是http://codepad.org/JgJfqnrA

答案 1 :(得分:1)

Baba是正确的,用于解析XML的内置函数是执行此操作的最佳方法,但如果XML实际上是使用不匹配的标记发回的,那么您也可以使用正则表达式。

function regex_all( $capture, $haystack, $return=1 ) {
  preg_match_all( "#$capture#", $haystack, $match );
  return $match[ $return ];
}

foreach( regex_all('<LocationName>(.*?)<\/', $data) as $locationName ) {
  echo "$locationName<br>";
}

这不是首选方法,因为正则表达式不可靠。