需要一个使用PHP和CURL将XML发布到USPS地址验证web服务的示例


Need an example using PHP and CURL to post XML to USPS address verification web service

我尝试了以下操作:

<?php
$user = 'myusername';

$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1 />" .
"<Address2>205 bagwell ave</Address2>" .
"<City>nutter fort</City>" .
"<State>wv</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";

$url = "http://production.shippingspis.com/ShippingAPI.dll";

    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
   // Following line is compulsary to add as it is:
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                "?API=Verify&XML=" . $xml_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $output = curl_exec($ch);
    curl_close($ch);

$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
?>

我没有得到任何回应,也没有任何错误。有人能把我带向正确的方向吗?

我试着遵循我在网上找到的几个例子,但我似乎无法找到解决方案。

我的代码是基于这篇文章中的例子:

使用php curl 将XML数据发送到Web服务

工作代码如下

<?php
$user = 'myusername';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>$address1></Address1>" .
"<Address2>$address2</Address2>" .
"<City>$city</City>" .
"<State>$state</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";

$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";

    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // Following line is compulsary to add as it is:
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                'XML=' . $xml_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $output = curl_exec($ch);
    echo curl_error($ch);
    curl_close($ch);

$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;

?>

这是工作代码。请注意,XML是在对curl_setopt的调用中传递的。除了在我的原始URL中的拼写错误外,我还添加了'&$url变量中的XML=',但不起作用。

<?php
$user = 'myusername';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>$address1></Address1>" .
"<Address2>$address2</Address2>" .
"<City>$city</City>" .
"<State>$state</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";

$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";

    //setting the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // Following line is compulsary to add as it is:
    curl_setopt($ch, CURLOPT_POSTFIELDS,
                'XML=' . $xml_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
    $output = curl_exec($ch);
    echo curl_error($ch);
    curl_close($ch);

$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
?>