我正在尝试使用 PHP 从美国邮政服务 (USPS) 费率计算器中提取 XML 页面。这是我正在使用的代码(当然替换了我的 API 登录名和密码):
<?
$api = "http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=<RateV4Request ".
"USERID=\"MYUSERID\" PASSWORD=\"MYPASSWORD\"><Revision/><Package ID=\"1ST\">".
"<Service>FIRST CLASS</Service><FirstClassMailType>PARCEL</FirstClassMailType>".
"<ZipOrigination>12345</ZipOrigination><ZipDestination>54321</ZipDestination>".
"<Pounds>0</Pounds><Ounces>9</Ounces><Container/><Size>REGULAR</Size></Package></RateV4Request>";
$xml_string = file_get_contents($api);
$xml = simplexml_load_string($xml_string);
?>
非常简单。然而它永远不会返回任何东西。我可以将 URL 直接粘贴到浏览器的地址栏中:
http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=<RateV4RequestUSERID="MYUSERID" PASSWORD="MYPASSWORD"><Revision/><Package ID="1ST"><Service>FIRST CLASS</Service><FirstClassMailType>PARCEL</FirstClassMailType><ZipOrigination>12345</ZipOrigination><ZipDestination>54321</ZipDestination><Pounds>0</Pounds><Ounces>9</Ounces><Container/><Size>REGULAR</Size></Package></RateV4Request>
它返回我需要的 XML,因此我知道该 URL 是有效的。但我似乎无法使用 PHP 捕获它。任何帮助将不胜感激。提前致谢。
一件事是您需要对发送到服务的 XML 进行 URL 编码。浏览器会自动为您执行此操作,但是file_get_contents
won't.
尝试这个:
$param = urlencode("<RateV4Request ".
"USERID=\"MYUSERID\" PASSWORD=\"MYPASSWORD\"><Revision/><Package ID=\"1ST\">".
"<Service>FIRST CLASS</Service><FirstClassMailType>PARCEL</FirstClassMailType>".
"<ZipOrigination>12345</ZipOrigination><ZipDestination>54321</ZipDestination>".
"<Pounds>0</Pounds><Ounces>9</Ounces><Container/><Size>REGULAR</Size></Package></RateV4Request>");
$api = "http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML="
.$param;
... then the rest of the code
如果这没有帮助,请确保您已激活错误报告,以便您在以下情况下得到响应file_get_contents
有错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)