[nycphp-talk] making a manual http POST using a PHP client (USPS webservices)
Adam Maccabee Trachtenberg
adam at trachtenberg.com
Mon Jul 12 12:48:31 EDT 2004
On Mon, 12 Jul 2004, Hans Zaunere wrote:
> Pseudo code (meaning it probably won't work exactly, but it's a start);
>
> $fp = fsockopen('webserver.usps.com',80);
>
> $buffer = "POST /ShippingAPI.dll\r\n"
> .= "Content-Type: application/x-www-form-urlencoded\r\n"
> .= "X-Other-Headers: blah blah\r\n\r\n"
> .= "API=Rate&XML=somethingelse\r\n";
>
> fwrite($fp,$buffer);
Barf.
> Aside from CURL, as previously mentioned, there are no native functions
> to do this. Then again, in Perl, there aren't either (it's CPAN).
Actually, this is false. Check out my last message about streams. You
need to use a stream context to override the default method from GET
to POST and to set the body, but it'll work.
It's just not well documented. Here's a code snippet from "Upgrading
to PHP 5"
> $post_variables = array('API' => Rate', 'XML' => 'somethingelse');
> $content = http_build_query($post_variables);
> $options = array(
> 'http'=>array(
> 'method' => 'POST',
> 'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
'content' => $content
> )
> );
> $context = stream_context_create($options);
> $index = file_get_contents('http://www.example.com/index.php',
> false, $context);
This should make a POST request for you and it's a little nicer than
having to do all that fsockopen stuff because you don't need to handle
all the details of negotiating the connection. It'll also work with
https requests if you're installed OpenSSL.
-adam
--
adam at trachtenberg.com
author of o'reilly's "upgrading to php 5" and "php cookbook"
avoid the holiday rush, buy your copies today!
>From hans not junk at nyphp.com Mon Jul 12 13:54:47 2004
Return-Path: <hans not junk at nyphp.com>
Received: from smtp11.intermedia.net (smtp11.intermedia.net [64.78.21.10])
by virtu.nyphp.org (Postfix) with ESMTP id D9BA8A85EA
for <talk at lists.nyphp.org>; Mon, 12 Jul 2004 13:54:46 -0400 (EDT)
Received: from ehost011-1.exch011.intermedia.net ([64.78.21.3]) by
smtp11.intermedia.net with Microsoft SMTPSVC(6.0.3790.0);
Mon, 12 Jul 2004 10:46:11 -0700
X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Subject: RE: [nycphp-talk] DB resultset
Date: Mon, 12 Jul 2004 10:54:42 -0700
Message-ID: <41EE526EC2D3C74286415780D3BA9F870306C673 at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Thread-Topic: [nycphp-talk] DB resultset
Thread-Index: AcRoLRfXnk3SOrGZSVSHfC0fOZ6cjQACwAEg
From: "Hans Zaunere" <hans not junk at nyphp.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
X-OriginalArrivalTime: 12 Jul 2004 17:46:11.0118 (UTC)
FILETIME=[1E6DB4E0:01C46838]
X-BeenThere: talk at lists.nyphp.org
X-Mailman-Version: 2.1.4
Precedence: list
Reply-To: NYPHP Talk <talk at lists.nyphp.org>
List-Id: NYPHP Talk <talk.lists.nyphp.org>
List-Unsubscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
<mailto:talk-request at lists.nyphp.org?subject=unsubscribe>
List-Archive: <http://lists.nyphp.org/pipermail/talk>
List-Post: <mailto:talk at lists.nyphp.org>
List-Help: <mailto:talk-request at lists.nyphp.org?subject=help>
List-Subscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
<mailto:talk-request at lists.nyphp.org?subject=subscribe>
X-List-Received-Date: Mon, 12 Jul 2004 17:54:47 -0000
> > There lies the issue - the resultset isn't stored by
> > the database. What
> > you really want is a cursor. However, I assume
> > we're talking about
> > MySQL, in which case there are no cursors (they're
> > coming in MySQL 5.0).
>=20
> But what's wrong with using "mysql_data_seek()"
Nothing, but it isn't a true cursor.
A database cursor (at least in common terms) is where the result set is
stored on the server and client side API calls can operate on it
completely - generally known as scrollable cursors. mysql_data_seek()
works on the results after they've been stored on the client side as a
result of using mysql_query().
mysql_unbuffered_query() approaches cursors, but isn't really a cursor,
and doesn't provide much of the niceness of cursors (scrolling, multiple
results, lower network usage, etc). Thus, in the documentation, it says
mysql_data_seek(), among other functions, can't be used after an
unbuffered query (because nothing is stored on the client side
initially).
H
>From hans not junk at nyphp.com Mon Jul 12 14:09:36 2004
Return-Path: <hans not junk at nyphp.com>
Received: from smtp11.intermedia.net (smtp11.intermedia.net [64.78.21.10])
by virtu.nyphp.org (Postfix) with ESMTP id 593D4A85EA
for <talk at lists.nyphp.org>; Mon, 12 Jul 2004 14:09:36 -0400 (EDT)
Received: from ehost011-1.exch011.intermedia.net ([64.78.21.3]) by
smtp11.intermedia.net with Microsoft SMTPSVC(6.0.3790.0);
Mon, 12 Jul 2004 11:01:01 -0700
X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Subject: RE: [nycphp-talk] making a manual http POST using a PHP client
(USPSwebservices)
Date: Mon, 12 Jul 2004 11:09:35 -0700
Message-ID: <41EE526EC2D3C74286415780D3BA9F870306C69C at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Thread-Topic: [nycphp-talk] making a manual http POST using a PHP client
(USPSwebservices)
Thread-Index: AcRoMBQi1I9f25R1SpiLGTAsFLJvQAACTycg
From: "Hans Zaunere" <hans not junk at nyphp.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
X-OriginalArrivalTime: 12 Jul 2004 18:01:01.0759 (UTC)
FILETIME=[314ABCF0:01C4683A]
X-BeenThere: talk at lists.nyphp.org
X-Mailman-Version: 2.1.4
Precedence: list
Reply-To: NYPHP Talk <talk at lists.nyphp.org>
List-Id: NYPHP Talk <talk.lists.nyphp.org>
List-Unsubscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
<mailto:talk-request at lists.nyphp.org?subject=unsubscribe>
List-Archive: <http://lists.nyphp.org/pipermail/talk>
List-Post: <mailto:talk at lists.nyphp.org>
List-Help: <mailto:talk-request at lists.nyphp.org?subject=help>
List-Subscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
<mailto:talk-request at lists.nyphp.org?subject=subscribe>
X-List-Received-Date: Mon, 12 Jul 2004 18:09:36 -0000
> > Pseudo code (meaning it probably won't work exactly, but it's a
start);
> >
> > $fp =3D fsockopen('webserver.usps.com',80);
> >
> > $buffer =3D "POST /ShippingAPI.dll\r\n"
> > .=3D "Content-Type: application/x-www-form-urlencoded\r\n"
> > .=3D "X-Other-Headers: blah blah\r\n\r\n"
> > .=3D "API=3DRate&XML=3Dsomethingelse\r\n";
> >
> > fwrite($fp,$buffer);
>=20
> Barf.
I know, I know...
> > Aside from CURL, as previously mentioned, there are no native
functions
> > to do this. Then again, in Perl, there aren't either (it's CPAN).
>=20
> Actually, this is false. Check out my last message about streams. You
> need to use a stream context to override the default method from GET
> to POST and to set the body, but it'll work.
Depends on what version of PHP... in PHP 4, which is the version I'm
sure he's using, it's correct.
> It's just not well documented. Here's a code snippet from "Upgrading
> to PHP 5"
>=20
> > $post_variables =3D array('API' =3D> Rate', 'XML' =3D> =
'somethingelse');
> > $content =3D http_build_query($post_variables);
> > $options =3D array(
> > 'http'=3D>array(
> > 'method' =3D> 'POST',
> > 'header' =3D> "Content-type:
application/x-www-form-urlencoded\r\n" .
> 'content' =3D> $content
> > )
> > );
> > $context =3D stream_context_create($options);
> > $index =3D file_get_contents('http://www.example.com/index.php',
> > false, $context);
>=20
> This should make a POST request for you and it's a little nicer than
> having to do all that fsockopen stuff because you don't need to handle
> all the details of negotiating the connection. It'll also work with
> https requests if you're installed OpenSSL.
Yeah, but sockets are cool. And yes, streams are very cool, but unless
I'm writing in PHP 5, I wouldn't make extensive use of them in my code.
Especially since file_get_contents() doesn't support contexts until 5,
per the above example.
H
More information about the talk
mailing list