Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要一个使用PHP和CURL将XML发布到USPS地址验证web服务的示例吗_Php_Curl - Fatal编程技术网

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

需要一个使用PHP和CURL将XML发布到USPS地址验证web服务的示例吗,php,curl,Php,Curl,我尝试了以下方法: <?php $user = 'myusername'; $xml_data = "<AddressValidateRequest USERID='$user'>" . "<IncludeOptionalElements>true</IncludeOptionalElements>" . "<ReturnCarrierRoute>true</ReturnCarrierRoute>" . "<Addre

我尝试了以下方法:

<?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;
?>
”。
“$address2”。
“$城市”。
“$state”。
"" .
"" .
"" .
"";
$url=”http://production.shippingapis.com/ShippingAPI.dll?API=Verify";
//设置旋度参数。
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
//必须按原样添加以下行:
curl_setopt($ch,CURLOPT_POSTFIELDS,
“XML=”。$XML_数据);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,300);
$output=curl\u exec($ch);
回波旋度误差($ch);
卷曲关闭($ch);
$array_data=json_decode(json_encode(simplexml_load_string($output)),true);
印刷品(“”);
打印(数组数据);
印刷品(“”);
echo PHP_EOL;

?>

这是工作代码。请注意,XML是在对curl_setopt的调用中传递的。除了原始URL中的输入错误之外,我在$URL变量中包含了“&XML=”,但这不起作用



echo curl\u error()在curl\u close()之前,并转储$output以了解问题所在。此外,您可能需要打开错误报告,这是一个帮助。我的第一个错误是主机名输入错误。然后web服务返回一个错误,说我的XML无效。我用验证器进行了检查,结果没有问题,但经过几次黑客攻击后,它开始工作。正在运行的代码e在原始问题中。我的答案被删除,因为我没有提供问题的答案。因为我提供了工作代码作为答案,这有什么错?复制工作代码并将其作为答案发布,而不是将其放在我猜的问题中。(我没有调整答案)谢谢。我很难把代码放到评论中,但在anwer中,它可以工作
<?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;
<?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;
?>