如何使用HttpSocket在cakephp中执行xml请求

时间:2015-07-09 11:20:57

标签: cakephp

我有以下xml网址:

 http://production.shippingapis.com/ShippingAPI.dll?API= CityStateLookup&XML=<CityStateLookupRequest USERID="138TOTAL1122"><ZipCode ID="0">
<Zip5>90210</Zip5>
</ZipCode></CityStateLookupRequest>

我如何使用HttpSocket来获取此网址的响应。 我尝试如下:

$HttpSocket = new HttpSocket();
            $results = $HttpSocket->post('http://production.shippingapis.com/ShippingAPI.dll', array('API' => 'CityStateLookup',
                'XML' => '<CityStateLookupRequest USERID="138TOTAL1122"><ZipCode ID="0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>'
                    )
            );

它显示响应数据($ results)如下:

HttpSocketResponse Object
(
    [body] => 
90210BEVERLY HILLSCA
    [headers] => Array
        (
            [X-Backside-Transport] => OK OK
            [Cache-Control] => private
            [Content-Type] => text/xml
            [Server] => Microsoft-IIS/7.5
            [X-AspNet-Version] => 2.0.50727
            [X-Powered-By] => ASP.NET
            [Date] => Thu, 09 Jul 2015 11:46:33 GMT
            [X-Client-IP] => 56.0.70.6
            [Access-Control-Allow-Origin] => *
            [Connection] => Keep-Alive
            [Content-Length] => 177
        )

    [cookies] => 
    [httpVersion] => HTTP/1.0
    [code] => 200
    [reasonPhrase] => OK
    [raw] => HTTP/1.0 200 OK
X-Backside-Transport: OK OK
Cache-Control: private
Content-Type: text/xml
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Thu, 09 Jul 2015 11:46:33 GMT
X-Client-IP: 56.0.70.6
Access-Control-Allow-Origin: *
Connection: Keep-Alive
Content-Length: 177


90210BEVERLY HILLSCA
    [context] => Array
        (
        )

)

我怎么能看到这个响应如下(因为我可以在手动浏览上面的url时看到这个响应):

<CityStateLookupResponse>
<ZipCode ID="0">
<Zip5>90210</Zip5>
<City>BEVERLY HILLS</City>
<State>CA</State>
</ZipCode>
</CityStateLookupResponse>

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

Try:
$HttpSocket = new HttpSocket();
$results = $HttpSocket->get('http://production.shippingapis.com/ShippingAPI.dll', array('API' => 'CityStateLookup', 'XML' => '<CityStateLookupRequest USERID="138TOTAL1122"><ZipCode ID="0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>'));
echo $results->body; // xml response data in body
//exit();
$xmlArray = Xml::toArray(Xml::build($results->body)); // xml response data in array format
print_r($xmlArray);
exit();