Copy link to clipboard
Copied
I need to get rates for shipping from USPS, but I can not get the API to work. Below is the code and links to the page. Does anyone know of a tutorial or can help me figure out what the problem is. I only get HTTP Error 501 - Not Implemented
USPS instructions: http://www.usps.com/webtools/htm/Rate-Calculators-v2-3.htm#_Toc220743995
Page where the below code runs: http://www.wrathclothingstore.com/cfhttp.cfm
CODE:
<cfset strURL = (
"http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV3&XML="
) />
<!--- Create the XML data to post. --->
<cfsavecontent variable="strXML">
<RateV3Request USERID="xxxxUserNameGoesHerexxxx">
<Package ID="1ST">
<Service>FIRST CLASS</Service>
<FirstClassMailType>LETTER</FirstClassMailType>
<ZipOrigination>44106</ZipOrigination>
<ZipDestination>20770</ZipDestination>
<Pounds>0</Pounds>
<Ounces>3.5</Ounces>
<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>
</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>
</Package>
</RateV3Request>
</cfsavecontent>
<!---
Post the XML data to catch page. We are going
to post this value as an XML object will actually
just post it as an XML body.
--->
<cfhttp
url="#strURL#"
method="POST"
useragent="#CGI.http_user_agent#"
result="objGet">
<!---
When posting the xml data, remember to trim
the value so that it is valid XML.
--->
<cfhttpparam
type="XML"
value="#strXML.Trim()#"
/>
</cfhttp>
<!--- Output the return message. --->
<cfoutput>
CharSet: #objGet.ErrorDetail#<BR>
Filecontent: #objGet.Filecontent#<BR>
Header: #objGet.Header#<BR>
Mimetype: #objGet.Mimetype#<BR>
Statuscode: #objGet.Statuscode#<BR>
Text: #objGet.Text#<BR>
<!--- <cfdump var="#objGet#"> --->
</cfoutput>
Any help would be greatly appreciated. Been banging my head agaist the wall!!
Thanks
CJ
Copy link to clipboard
Copied
Try upgrading your account to the production server.
http://www.ubercart.org/forum/support/1756/usps_webtools
http://www.marksanborn.net/php/calculating-usps-shipping-rates-with-php/
Copy link to clipboard
Copied
Thanks, but I upgraded and moved to the production server and it still does not work. I checked out your second link and it is PHP and I not know PHP so I am having a hard time trying to figure it out so I could make it work with CF
If I put this in the the URL (Address Bar) It works fine:
http://production.shippingapis.com/ShippingAPI.dll?API=RateV3&XML=%20%20%20%3CRateV3Request%20USERID=%22xxxUSERNAMExxx%22%3E%20%20%3CPackage%20ID=%221%22%3E%20%20%3CService%3EPRIORITY%3C/Service%3E%20%20%3CZipOrigination%3E44656%3C/ZipOrigination%3E%20%20%3CZipDestination%3E44663%3C/ZipDestination%3E%20%20%3CPounds%3E0%3C/Pounds%3E%20%20%3COunces%3E12%3C/Ounces%3E%20%20%3CSize%3EREGULAR%3C/Size%3E%20%20%3C/Package%3E%20%3C/RateV3Request%3E
It brings back the shipping rate. But when I do it in the my application, nothing, I get the NOT IMPLEMENTED (HTTP Error 501) Is there an error in my CF code above, what am I doing wrong?
I changed the strXML line of code to this for production:
<cfset strURL = (
"http://production.shippingapis.com/ShippingAPI.dll?API=RateV3&XML="
) />
Copy link to clipboard
Copied
<cfset strURL = ( "http://production.shippingapis.com/ShippingAPI.dll?API=RateV3&XML="
) />
Let us remove the &XML= from our strURL as below:
<cfset strURL = "http://production.shippingapis.com/ShippingAPI.dll?API=RateV3" />
Copy link to clipboard
Copied
Not sure if I am following you on why we would do that and the difference it would make?
Copy link to clipboard
Copied
The cfhttpparam passes in our xml parameter i.e. XML=#strXML#. In other words by including the &XML= in the URL we are duplicating the &XML= in the final URL+its parameters.
We could actually have passed our API value (i.e. API=RateV3) with another cfhttparam as well, leaving our URL as
http://production.shippingapis.com/ShippingAPI.dll
Copy link to clipboard
Copied
Please note that we have also removed the parentheses ( ).
Copy link to clipboard
Copied
I have tried this and it worked.
<cfset strURL = "http://production.shippingapis.com/ShippingAPI.dll" />
<cfset api = "RateV3" />
<!--- Create the XML data to post. --->
<cfsavecontent variable="strXML">
<RateV3Request USERID="UserName">
<Package ID="1ST">
<Service>FIRST CLASS</Service>
<FirstClassMailType>LETTER</FirstClassMailType>
<ZipOrigination>44106</ZipOrigination>
<ZipDestination>20770</ZipDestination>
<Pounds>0</Pounds>
<Ounces>3.5</Ounces>
<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>
</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>
</Package>
</RateV3Request>
</cfsavecontent>
<cffunction name="processRequest" access="private" returntype="xml" output="false" hint="Makes the HTTP request to the USPS API.">
<cfargument name="url" type="String" required="true" />
<cfargument name="api" type="String" required="true" />
<cfargument name="xml" type="String" required="true" />
<cfhttp url="#arguments.url#" method="post">
<cfhttpparam type="formfield" name="api" value="#arguments.api#" />
<cfhttpparam type="formfield" name="xml" value="#arguments.xml#" />
</cfhttp>
<cfreturn XmlParse(cfhttp.fileContent) />
</cffunction>
<!--- Dump results --->
<cfdump var=#processRequest(strURL, api, strXML)# />
<!--- Output details --->
<cfoutput>CharSet: #cfhttp.ErrorDetail#<br />
Filecontent: #cfhttp.Filecontent#<br />
Header: #cfhttp.Header#<br />
Mimetype: #cfhttp.Mimetype#<br />
Statuscode: #cfhttp.Statuscode#<br />
Text: #cfhttp.Text#<br />
</cfoutput>
Copy link to clipboard
Copied
(Disclaimer, I have only skimmed this thread) Before you re-invent the wheel, have you checked riaforge.org? I have not used it myself, but it looks like there is at least one cfc:
http://uspscfc.riaforge.org/
Copy link to clipboard
Copied
Sorry for the delay, I did not work on it again till tonight.
A***, Thank you very much for your help, I really appreciate it. I did get it to work with your code and it works great. Thanks
-==cfSearching==- - Thanks for the link, I checked it out and it also does what I need it to do. So both are very helpful.
I also found this one from CFUnite
Mary Jo Sminkey "USPS tracker"
http://wiki.cfunited.com/index.cfm/Open_Source_Contest_Entries#Mary_Jo_Sminkey__USPS_tracker_
All were very helpful in fixing and learning how to handle it.
thank you very much for your time, knowledge and willingness to help me out
CJ