2

I've tried the following:

<?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;
?>

I get no response nor any kind of an error. Can anyone lead me in the right direction?

I have tried to follow several examples that I found on the web but I can't seem to hit upon the solution.

My code is based on the example in this post:

Send XML data to webservice using php curl

Working Code follows

<?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;

?>

5
  • echo curl_error() before curl_close(), and also dump the $output to get an idea what's wrong. In addition, you might need to turn on error reporting-stackoverflow.com/questions/8032391/… Commented Oct 30, 2015 at 18:15
  • That is a help. My first error was a typo in the host name. The web service was then returning an error saying my XML was invalid. I checked it with a validator and it was okay but, after a few more hacks, got it to work. The working code is in the original question. Commented Oct 30, 2015 at 19:39
  • My answer was deleted as not providing an answer to the question. Since I provided the working code as the answer, what was wrong with that? Commented Nov 2, 2015 at 15:29
  • Copy the working code and post it as answer, instead of putting it inside the question I guess. (I didn't moderate the answer) Commented Nov 2, 2015 at 19:11
  • Thanks. I had trouble putting code into comments but in an anwer, it works Commented Nov 3, 2015 at 1:05

1 Answer 1

3

Here is the working code. Note the XML is passed in the call to curl_setopt. In addition to the typo in my original URL, I included '&XML=' in the $url variable, which did not work.

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

Comments

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.