No results when using a web API

后端 未结 1 1398
不思量自难忘°
不思量自难忘° 2021-01-16 00:27

I\'m attempting to pull an XML page from the U.S. Postal Service (USPS) rate calculator, using PHP. Here is the code I am using (with my API login and password replaced of

相关标签:
1条回答
  • 2021-01-16 01:26

    One thing is that you need to URL encode the XML that you are sending to the service. The browser will do that for you automatically, but file_get_contents won't.

    Try this:

     $param = urlencode("<RateV4Request ".
       "USERID=\"MYUSERID\" PASSWORD=\"MYPASSWORD\"><Revision/><Package ID=\"1ST\">".
       "<Service>FIRST CLASS</Service><FirstClassMailType>PARCEL</FirstClassMailType>".
       "<ZipOrigination>12345</ZipOrigination><ZipDestination>54321</ZipDestination>".
       "<Pounds>0</Pounds><Ounces>9</Ounces><Container/><Size>REGULAR</Size></Package></RateV4Request>");
    
     $api = "http://production.shippingapis.com/ShippingAPI.dll?API=RateV4&XML="
            .$param;
    
     ... then the rest of the code
    

    If that doesn't help, make sure you have error reporting activated so you get a response if file_get_contents has an error.

    0 讨论(0)
提交回复
热议问题