使用PHP协助USPS API Curl
您好,我正在尝试使用PHP Curl对USPS API进行API调用。使用PHP协助USPS API Curl
我得到如下回应:
[Number] => 80040B19
[Description] => XML Syntax Error: Please check the XML request to see if it can be parsed.
[Source] => USPSCOM::DoAuth
我把我的代码从一些示例代码在这里的API调用,并在样品上的USPS网站;但不能得到它的工作(上面得到错误);这里是我的代码:
$input_xml = '<AddressValidateRequest USERID="xxxxxxx">
<Address ID="0">
<Address1></Address1>
<Address2>6406 Ivy Lane</Address2><City>Greenbelt</City>
<State>MD</State>
<Zip5></Zip5>
<Zip4></Zip4>
</Address>
</AddressValidateRequest>';
$url = "http://production.shippingapis.com/ShippingAPITest.dll?API=Verify";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"xmlRequest=" . $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);
//convert the XML result into array
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
我希望有人能与我做错了帮助...
据the documentation,你应该通过在一个名为XML
领域的XML,而不是xmlRequest
。尝试这样的代替:
<?php
$input_xml = <<<EOXML
<AddressValidateRequest USERID="xxxxxxx">
<Address ID="0">
<Address1></Address1>
<Address2>6406 Ivy Lane</Address2>
<City>Greenbelt</City>
<State>MD</State>
<Zip5></Zip5>
<Zip4></Zip4>
</Address>
</AddressValidateRequest>
EOXML;
$fields = array(
'API' => 'Verify',
'XML' => $input_xml
);
$url = 'http://production.shippingapis.com/ShippingAPITest.dll?' . http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);
// Convert the XML result into array
$array_data = json_decode(json_encode(simplexml_load_string($data)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
?>
非常感谢肖恩......虽然我不得不编辑一个litte;它不喜欢你做“$ fields”数组的方式并出错;但这是我知道如何处理,所以我基本上redid你的确切数组有点不同 - 但不能没有你的帮助做到这一点,所以非常感谢(我将编辑你的答案阵列部分) –
看起来像你运行不支持'''数组语法的旧版PHP版本。我已经更新了如何使用旧的'array(...)'语法来实现它。 –
是的 - 我相信你是对的;我实际上试图用parens来做,因为我认为它可以这样做;但不记得究竟是怎么回事;所以最快的方式就是像我手背一样知道的方式......非常感谢! –
...这令人不安地接近我的孩子地址。并且你错过了开头'''前面的'城市' –
检查'绿带 '它缺少开幕 –
@SeanBright你应该小心发布信息,有人可能会返回及时回来并绑架你,以防止你从未来评论到这个职位 – Ray