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;

?>

Community
  • 1
  • 1
Dennis M. Gray
  • 332
  • 1
  • 3
  • 17
  • 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-http://stackoverflow.com/questions/8032391/php-error-reporting-even-when-display-error-is-on/8032471#8032471 – Salman Oct 30 '15 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. – Dennis M. Gray Oct 30 '15 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? – Dennis M. Gray Nov 02 '15 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) – Salman Nov 02 '15 at 19:11
  • Thanks. I had trouble putting code into comments but in an anwer, it works – Dennis M. Gray Nov 03 '15 at 01:05

1 Answers1

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;
?>
Dennis M. Gray
  • 332
  • 1
  • 3
  • 17