USPS api - using curl and codeigniter

↘锁芯ラ 提交于 2019-12-11 02:45:54

问题


I am trying to develop a USPS rate calculator for a cart that I am building in codeigniter. I am having issues getting getting success responses from the USPS test server and their api. I have called in with my issues and apparently, they don't want to provide any support to anyone trying to create a custom solution. To make it more confusing, they told me that all I need to know can be found in their Development Guide v3.pdf, but in it they state:

Step 3: Run “Canned” Test from Test Server

Applicable APIs: All

The next step is to test your API interfaces. As a registered API user you have been granted access to the Shipping API test server. An important note: the test server is set up to only accept a subset of pre-defined XML transactions and return the pre-defined XML responses provided in this document.

If you have already successfully tested your API and have access to the production server, skip this step and proceed to Step 5: Run “Samples” from Production Server.

The problem here is that they do not provide any of the "subset of pre-defined XML transactions" as stated. They do provide test samples, but they seem to be designed for the production server which you only get access to whey you successfully test on the test server (in my script below, I have one of these samples which I tried unsuccessfully). I therefore refer to the USPS Rate Calculators v1-5-1.pdf where they give you specific test transaction samples. At best, I would get an error response stating:

<Error>
<Number>80040b1a</Number>
<Description>Authorization failure. You are not authorized to connect to this server.    </Description>
<Source>UspsCom:: DoAuth</Source>
</Error>

I know people have solved this, there are a few closed questions similar to this here, but none of them show a solution. My code follows:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Usps_curl extends CI_Controller {

    public function index()
    {

        $devurl = "testing.shippingapis.com/ShippingAPITest.dll";
        $puburl = "https://secure.shippingapis.com/ShippingAPITest.dll";

        $service = "RateV4";

//      ALTHOUGH THE USPS DEV GUIDE V3 SAYS 

//      $xml = '<RateV4Request USERID="225XXXXX5943">
//      <Revision>2</Revision>
//      <Package ID="1">
//          <Service>PRIORITY</Service>
//          <Container>FLAT RATE BOX</Container>
//          <ZipOrigination>20012</ZipOrigination>
//          <ZipDestination>20007</ZipDestination>
//          <Pounds>1</Pounds>
//          <Ounces>0.0</Ounces> 
//          <Size>REGULAR</Size> 
//      </Package>
//      </RateV4Request>';

//  THIS IS AN EXACT EXAMPLE PROVIDED IN THE USPS DEV GUIDE V3 - BUT I BELIEVE IT IS ONE OF THE FOUR "CANNED" TEST FOR THE PRODUCTION SERVER

//$xml = '<AddressValidateRequest USERID="225XXXXX5943">
//          <Address ID="0">
//              <Address1></Address1>
//              <Address2>6406 Ivy Lane</Address2>
//              <City>Greenbelt</City>
//              <State>MD</State>
//              <Zip5></Zip5>
//              <Zip4></Zip4>
//              </Address>
//          </AddressValidateRequest>';


//      THIS IS THE SAMPLE CODE FROM USPS RATE CALCULATORS V1-5-1 PDF

        $xml = rawurlencode('<RateV4Request USERID="225XXXXX5943">
        <Revision>2</Revision>
        <Package ID="1ST">
        <Service>FIRST CLASS</Service>
        <FirstClassMailType>LETTER</FirstClassMailType>
        <ZipOrigination>44106</ZipOrigination>
        <ZipDestination>20770</ZipDestination>
        <Pounds>0</Pounds>
        <Ounces>3.5</Ounces>
        <Container/>
        <Size>REGULAR</Size>
        <Machinable>true</Machinable>
        </Package>
        <Package ID="2ND">
        <Service>PRIORITY</Service>
        <ZipOrigination>44106</ZipOrigination>
        <ZipDestination>20770</ZipDestination>
        <Pounds>1</Pounds>
        <Ounces>8</Ounces>
        <Container>NONRECTANGULAR</Container>
        <Size>LARGE</Size>
        <Width>15</Width>
        <Length>30</Length>
        <Height>15</Height>
        <Girth>55</Girth>
        <Value>1000</Value>
        <SpecialServices>
        <SpecialService>1</SpecialService>
        </SpecialServices>
        </Package>
        <Package ID="3RD">
        <Service>ALL</Service>
        <ZipOrigination>90210</ZipOrigination>
        <ZipDestination>96698</ZipDestination>
        <Pounds>8</Pounds>
        <Ounces>32</Ounces>
        <Container/>
        <Size>REGULAR</Size>
        <Machinable>true</Machinable>
        <ShipDate>13-Dec-2010</ShipDate>
        </Package>
        </RateV4Request>');


        $request = $devurl . "?API=" . $service . "&xml=" . $xml;

//      echo "<pre>";
//      print_r($request);
//      die;


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);



        $response = curl_exec($ch);
        curl_close($ch);

//      echo "<pre>";
//      print_r($response);
//      echo "</pre>";

    }
}

/* End of file usps_curl.php */
/* Location: ./application/controllers/usps_curl.php */

As you can see, I have the various strings commented and commented out. I have tried each of these with rawurlencode, urlencode & not encoded, but none work. Any help/suggestions would be greatly appreciated!

I know that I must be close, because I do at times get a proper error message, but I must be missing something here!


回答1:


That error response is just stating you aren't auth'd for that API.

I think you need to test using the test examples they provide. Once those are working, your API is working and you can get access to production API's.

curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false );

will also be important in order to ignore cert errors (USPS does essentially the same thing in their example code--they set the ActiveX object to ignore cert errors).

The PDF also says to use your browser if the client isn't working. You can do this to see what you can expect to be returned.

I just registered to test this... It seems at least one example is broken. But this example works in my browser:

http://testing.shippingapis.com/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest> 

(replace user id)

<CityStateLookupResponse>
<ZipCode ID="0">
<Zip5>90210</Zip5>
<City>BEVERLY HILLS</City>
<State>CA</State>
</ZipCode>
</CityStateLookupResponse>

Thing is with the examples: you have to use the appropriate testing server depending on whether or not it is an SSL API. Anything with an address in it is required to use SSL. So you have to meld the examples with the testing URL's provided by USPS in the registration email.

Anyway, so check out the URL above and get your code to match the output that you get in the browser. Once this works, tell them you're LIVE and you will then get access to the production URL's and price lookup API.



来源:https://stackoverflow.com/questions/10391110/usps-api-using-curl-and-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!