3

Hello I am trying to do API call to USPS API using PHP Curl.

I get the following response:

[Number] => 80040B19
[Description] => XML Syntax Error: Please check the XML request to see if it can be parsed.
[Source] => USPSCOM::DoAuth

I put together my code for the API call from some sample code on here and also the sample on the USPS site; but cannot get it to work (getting error above); here is my code:

$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>');

I am hoping someone can help with what I am doing wrong...

4
  • 4
    ... that is disturbingly close to my address as a child. And you're missing the opening < in front of City Commented Jul 5, 2016 at 20:24
  • check <City>Greenbelt</City> it is missing the opening Commented Jul 5, 2016 at 20:25
  • 3
    @SeanBright you should be careful posting that info, someone may travel back in time and kidnap you to prevent you from commenting to this post in the future Commented Jul 5, 2016 at 20:32
  • Hello Guys: thanks for responding but actually my code had that opening tag in it and when i put it on here i mistakenly deleted it; so sorry about wasting your time on finding that error; i edited it and it still gets that error so if you can find anything else wrong please let me know.... Commented Jul 5, 2016 at 20:32

1 Answer 1

8

According to the documentation, you're supposed to pass the XML in a field named XML, not xmlRequest. Try something like this instead:

<?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>');
?>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks so much Sean...that did work although i had to edit a litte; it did not like the way you did the "$fields" array and got an error; but that was something I knew how to handle so i basically redid your exact array a little differently - but could not have done this without your help so thanks so much (I will edit your answer for the array part)
Looks like you're running an older PHP version that doesn't support the […] array syntax. I've updated to show how to do it with the older array(…) syntax.
yeah - i am sure you are right; i actually tried to do it with parens as I thought it could be done that way; but could not remember exactly how it went; so fastest way to do it was the way i knew like the back of my hand...thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.