1

今天我一直在处理一些代码,想知道是否有人可以为我指出比现在更好的方向。

我有 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>

谢谢!!!

4

2 回答 2

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

于 2012-10-13T01:52:22.300 回答
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>";
}

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

于 2012-10-13T02:43:59.730 回答