NYCPHP Meetup

NYPHP.org

[nycphp-talk] making a manual http POST using a PHP client (USPS web services)

Jayesh Sheth jayeshsh at ceruleansky.com
Mon Jul 12 11:22:49 EDT 2004


Hello all,

I am trying to build a PHP script that requests information from the 
United States Postal Service (USPS) using their web services interface. 
More specifically, I want to look up 9 digit zip codes for a given 
street address.

The USPS does not use a standard web services protocol such as XML-RPC 
or SOAP. If they were using XML-RPC, then I could just use the PEAR 
XML-RPC library to automatically POST the XML request to their server 
(and also to automatically decode the XML response that I receive from 
them).

The USPS has some documents on this matter, but their (less than 
abundant) code samples are restricted to Perl, ASP, and Visual Basic.

Their Perl code sample looks like this:

--------------------------------------
print "content-type: text/html\n\n";

print $htmlBegin;

$ua = new LWP::UserAgent;

$req = new HTTP::Request 'POST', 'http://SERVERNAME/ShippingAPI.dll';

$req->content_type('application/x-www-form-urlencoded');

$req->content('API=Rate&XML=' . $rateReqDoc->toString);

$response = $ua->request($req);

if ($response->is_success) {

     $resp = $response->content;

} else {

     print "<p>There was an error processing your request</p>\n";

     print $htmlEnd;

     exit 0;

}
---------------------------------------------

I have not really used Perl before, but the first time I looked at this 
code, I thought it looked a lot like PHP. Here's what I think is happening:

The Perl script manually opening an HTTP connection and posting a chunk 
of XML encoded text to server using the "LWP::UserAgent" and 
"HTTP::Request" things.

So my question is: which function (or objects) should I use in PHP to do 
something. I know, this is kind of a stupid question - I should probably 
know how to do this, but until now I just let the web services libraries 
do this "dirty work" for me.

On a related note, I was wondering if anyone knows of open source PHP 
code that does what I would like to do.

Thanks in advance.

Best Regards,

- Jay





More information about the talk mailing list