Return to the Forum Home
 Post New TopicNote: Polls are considered new topics.  If you post a poll, it will be created as a new subject in this forum, not as a reply within this topic.Topic Replies: PopIt!Post A Reply
MyPop™ | register | search | faq | forum home


PreviousGo to the next oldest topic in this forum Go to the next newest topic in this forumNext
 friends of ED » friends of ED forums » Books and Information » PHP and USPS Web Tools (XML)

Rating:   Not yet rated
Author Topic:   PHP and USPS Web Tools (XML)
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Hi, need some help-

I'm trying to use the United States Postal Service (USPS) Web Tools API to do a City State Lookup. The API specifies that you need to send a XML request which should look something like the following.

http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLooku
pRequest%20USERID="xxxxxxx"><ZipCode ID= "0">
<Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

The service will return a response as follows:

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

Then I would need to parse it.

What I'm trying to do is to create a form for user registration. When the user hits the Submit/Register button, I want it send the request to USPS and parse the response. If the response is good, then I'll write user info to the database. I'm using PHP to process the user registration form (i.e. - check for valid inputs from users, and write to database if everything is ok.

I don't think their tool supports SOAP. So I was think of using XML-RPC.

Although David Power's book does go into PHP and XML for RSS feeds, I don't think it applies here.

Can someone point me in the right direction? Perhaps some sample code or suggestion about how to go about doing this would be greatly appreciated. Thanks in advance.

-bui

Posts: 28 | Registered: 04-22-06
David Powers
Author
Describes the mood or content of the topic posted Click Here to See the Profile for David Powers   Reply With QuoteEdit or Delete MessageReport This Post
If your server supports PHP 5, use SimpleXML.

David Powers
Author: PHP Solutions
Author: Foundation PHP for Dreamweaver 8
http://foundationphp.com/

Posts: 4331 | From: London, UK | Registered: 12-18-03
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Dave -
Thanks for responding. I'll try your suggestion.

Posts: 28 | Registered: 04-22-06
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Dave -

I am running php 5.

Also, I looked into using SimpleXML and it seems that it would be great for working with XML. However, it seems to me that the USPS Web Tool is basically looking for a request using a query string. This means that the user registration form would have to use method=GET.

So it seems to me that i could just build the query string and and hit the submit button. Does this seem to be the correct approach?

Posts: 28 | Registered: 04-22-06
David Powers
Author
Describes the mood or content of the topic posted Click Here to See the Profile for David Powers   Reply With QuoteEdit or Delete MessageReport This Post
You should be able to do it by calling simplexml_load_file().

David Powers
Author: PHP Solutions
Author: Foundation PHP for Dreamweaver 8
http://foundationphp.com/

Posts: 4331 | From: London, UK | Registered: 12-18-03
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Dave - I was finally able to send a request to USPS using CURL. And, I do get a response but the response only contains the data values in one string. USPS says that I should be getting the following response verbatim:

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

But I'm getting this when I execute the request:
90210BEVERLY HILLSCA

What am I doing wrong??? I don't know why I'm not getting the XML elements but only the data values. Am I not making the request properly?

I'm uploading my simple one page test file just in case you feel like looking at it. It's a form with just one field and a button. I've XXXX out the username.

Thanks you very much for your help.

curlziptest.php (4 Kb, 196 downloads) curl test file

Posts: 28 | Registered: 04-22-06
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Dave -
OK, I also tried using file_get_contents(). That produces the same results. However, in looking at the page source, it does indeed show the XML elements. I guess the browser just doesn't display them. Is there a way to have the browser display them automatically?

If I manually input the request into the URL field for IE7, it shows the XML elements.

Posts: 28 | Registered: 04-22-06
bui
friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for bui   Reply With QuoteEdit or Delete MessageReport This Post
Dave - I think I figured out why the elements do not display. I changed my set up to the following:

A form that posts to a php file containing only the php code with either the curl or file_get_contents method for submitting a request; both works. The php file does not contain any html or text. In IE 7, I get the expected results. In FireFox, I only get the data. This is a bit odd, because both browsers show the identical source code. Any ideas?

Posts: 28 | Registered: 04-22-06
David Powers
Author
Describes the mood or content of the topic posted Click Here to See the Profile for David Powers   Reply With QuoteEdit or Delete MessageReport This Post
If you want to display the XML in a browser, you need to add this to your PHP code before anything is output to the browser:
  header('Content-Type: text/xml');
Normally, though, you would process the XML without outputting it in raw form to the browser.

David Powers
Author: PHP Solutions
Author: Foundation PHP for Dreamweaver 8
http://foundationphp.com/

Posts: 4331 | From: London, UK | Registered: 12-18-03
thephpguru
new friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for thephpguru   Reply With QuoteEdit or Delete MessageReport This Post
All of your pains will go away with PHP USPS scripts on freshsoftware.net/scripts_for_sale/usps/
Posts: 1 | Registered: 01-02-09
jriggs
new friend of ED
Describes the mood or content of the topic posted Click Here to See the Profile for jriggs   Reply With QuoteEdit or Delete MessageReport This Post
Hi I put together a short script to validate addresses using USPS webtools and php. You can see it here:
http://joe-riggs.com/blog/2009/10/address-standardization-verification-with-usps-web-tools-and-php/

Posts: 1 | Registered: 11-05-09

All times are
Post New TopicNote: Polls are considered new topics.  If you post a poll, it will be created as a new subject in this forum, not as a reply within this topic.Post A Reply
Print Page
Printable
Email a Friend
Email a Friend
Topic Replies: PopIt!
PopIt!
MyPop™
MyPop™
Contacts
Contacts
Administrative Links:
Close Topic
Close
Manage Topic
Manage
Delete Topic
Delete
Admin Options
PreviousGo to the next oldest topic in this forum Go to the next newest topic in this forumNext

Rate Topic:

Hop To:

All content copyright 2001-2008 friends of ED. No reuse without permission.

OpenTopic 3.1.0