NYCPHP Meetup

NYPHP.org

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

Brian Kaney brian at vermonster.com
Mon Jul 12 12:14:09 EDT 2004


We use CURL to talk to UPS and FedEx API's on a regular basis.  Our
FedEx library is open source and available for download:

http://freshmeat.net/redir/fedexdc/42837/url_tgz/fedexdc.tar.gz  


It looks like same technique could be used for USPS:
http://www.usps.com/webtools/welcome.htm


BTW: If you don't like the CURL library, I think you could try compiling
PHP with OpenSSL (so you can go over https) and use the fopen() function
(http://us4.php.net/function.fopen) or PEAR::HTTP module 
(http://pear.php.net/manual/en/package.http.php).

- Brian


On Mon, 2004-07-12 at 11:22, Jayesh Sheth wrote:
> 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
> 
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list