[PHP] Sending multipart/form-data request with PECL.

2009-03-04 Thread Jason Cipriani
I am trying to submit a request to an HTTP server with
multipart/form-data encoded data. I'm using PECL's HttpRequest
(although I'm open to alternatives). I am using PHP5.

I noticed that if you call addPostFile to add a file, PECL will send
the file, and all other post parameters, with multipart/form-data
encoding, so I know PECL has the capability to do it. If you simply
call addPostFields but never call addPostFile, it uses standard post
encoding.

Is there a way to force PECL to use multipart/form-data encoding for
all post fields added with addPostFields, even when you are not
calling addPostFile to add a file?

If not, is there another good way to encode multipart data with PHP?

Thanks!
Jason

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sending multipart/form-data request with PECL.

2009-03-04 Thread Jason Cipriani
On Wed, Mar 4, 2009 at 2:10 PM, Shawn McKenzie nos...@mckenzies.net wrote:
 Jason Cipriani wrote:
 Is there a way to force PECL to use multipart/form-data encoding for
 all post fields added with addPostFields, even when you are not
 calling addPostFile to add a file?

 Try: setContentType()

Thanks! But, I tried that, and according to my packet sniffer, calling
setContentType() actually seems to have no effect whatsoever on the
request! Is there something I have to enable? Here's an example, it's
just a fake request, used to see what HttpRequest outputs:

$fields = array(field=value,other=something)
$http_req = new HttpRequest('http://localhost:/resource');
$http_req-setMethod(HTTP_METH_POST);
$http_req-setContentType('multipart/form-data');
$http_req-addPostFields($fields);
$http_req-send();

Here is what it produces, it's still application/x-www-form-urlencoded:

=== BEGIN REQUEST ===
POST /resource HTTP/1.1
User-Agent: PECL::HTTP/1.6.1-dev (PHP/5.2.6)
Host: localhost:
Accept: */*
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

field=valueother=something
=== END REQUEST ===

Even if I call setContentType with some made up content type, it
doesn't affect the output; am I doing something wrong there?

Thanks!
Jason

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sending multipart/form-data request with PECL.

2009-03-04 Thread Jason Cipriani
On Wed, Mar 4, 2009 at 7:12 PM, Shawn McKenzie nos...@mckenzies.net wrote:
 Jason Cipriani wrote:
 On Wed, Mar 4, 2009 at 2:10 PM, Shawn McKenzie nos...@mckenzies.net wrote:
 Jason Cipriani wrote:
 Is there a way to force PECL to use multipart/form-data encoding for
 all post fields added with addPostFields, even when you are not
 calling addPostFile to add a file?
 Try: setContentType()

 Thanks! But, I tried that, and according to my packet sniffer, calling
 setContentType() actually seems to have no effect whatsoever on the
 request! Is there something I have to enable? Here's an example, it's
 just a fake request, used to see what HttpRequest outputs:

 $fields = array(field=value,other=something)
 $http_req = new HttpRequest('http://localhost:/resource');
 $http_req-setMethod(HTTP_METH_POST);
 $http_req-setContentType('multipart/form-data');
 $http_req-addPostFields($fields);
 $http_req-send();

 Here is what it produces, it's still application/x-www-form-urlencoded:

 === BEGIN REQUEST ===
 POST /resource HTTP/1.1
 User-Agent: PECL::HTTP/1.6.1-dev (PHP/5.2.6)
 Host: localhost:
 Accept: */*
 Content-Length: 27
 Content-Type: application/x-www-form-urlencoded

 field=valueother=something
 === END REQUEST ===

 Even if I call setContentType with some made up content type, it
 doesn't affect the output; am I doing something wrong there?

 Thanks!
 Jason

 I don't know.  I just looked it up in the manual.  If it doesn't work
 then it may be a bug.  Hard to tell because the documentation for
 httprequest is very light.


Thanks. I actually had a look at the HttpRequest source code, and I
can see the logic where it switches to multipart encoding if files are
present but it actually appears that it's not possible to force it to
do that. It's sort of annoying that it's right at my finger tips but
there's no way to do it, and for sort of a silly reason (there's
arbitrarily no way to pick which encoding to use, it decides for you
even though it has the capability of doing anything). A custom PECL
build is not an option, unfortunately.

I'll have to check out the httpclient class that Manuel Lemos mentioned.

Thanks,
Jason

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sending multipart/form-data request with PECL.

2009-03-04 Thread Jason Cipriani
On Thu, Mar 5, 2009 at 12:48 AM, Manuel Lemos mle...@acm.org wrote:
 Hello,

 on 03/05/2009 02:31 AM Jason Cipriani said the following:
 Thanks. I actually had a look at the HttpRequest source code, and I
 can see the logic where it switches to multipart encoding if files are
 present but it actually appears that it's not possible to force it to
 do that. It's sort of annoying that it's right at my finger tips but
 there's no way to do it, and for sort of a silly reason (there's
 arbitrarily no way to pick which encoding to use, it decides for you
 even though it has the capability of doing anything). A custom PECL
 build is not an option, unfortunately.

 I'll have to check out the httpclient class that Manuel Lemos mentioned.

 Yes, the HTTP client class follows the same logic but has a variable
 named force_multipart_form_post that you can set to make it send
 multipart form posts as if you were submitting a form with an empty file
 input.


Thanks; is there documentation for this anywhere?

Jason

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Sending multipart/form-data request with PECL.

2009-03-04 Thread Jason Cipriani
On Thu, Mar 5, 2009 at 1:48 AM, Manuel Lemos mle...@acm.org wrote:
 Hello Jason,

 on 03/05/2009 03:17 AM Jason Cipriani said the following:
 Thanks. I actually had a look at the HttpRequest source code, and I
 can see the logic where it switches to multipart encoding if files are
 present but it actually appears that it's not possible to force it to
 do that. It's sort of annoying that it's right at my finger tips but
 there's no way to do it, and for sort of a silly reason (there's
 arbitrarily no way to pick which encoding to use, it decides for you
 even though it has the capability of doing anything). A custom PECL
 build is not an option, unfortunately.

 I'll have to check out the httpclient class that Manuel Lemos mentioned.
 Yes, the HTTP client class follows the same logic but has a variable
 named force_multipart_form_post that you can set to make it send
 multipart form posts as if you were submitting a form with an empty file
 input.


 Thanks; is there documentation for this anywhere?

 Sorry, not yet, but the example scripts have plenty of commented code
 that pretty much explains how to do what you need. For sending POST
 requests, take a look at the test_http_post.php example script. For
 further support requests about this class, try the support forum:

 http://www.phpclasses.org/discuss/package/3/

Awesome, thanks. The code was clear enough that I was able to make it
work pretty easily. It seems to be doing everything that I want. Nice
work!

Thanks again!
Jason

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] [PHP 5.2.8] Retrieve cookies from remote host and pass them back to client.

2009-02-13 Thread Jason Cipriani
I have a PHP 5.2.8 web application that needs to retrieve a resource
from a remote URL. However, the remote host also sends a cookie back
(in a Set-Cookie header), and I need to grab that cookie and pass it
on to the client (so that it's stored by the client browser). I do
know the name of the cookie beforehand.

What is the best way to do this? Passing a cookie from PHP back to the
client is easy, it's grabbing the cookie from the remote URL that is
the problem.

There is no support for cookies in file_get_contents(), so that won't
work. I can use curl to retrieve the remote URL with headers, but curl
does not seem to provide an easy way to get the value of the cookie
beyond retrieving the HTTP headers and parsing them manually. I could
use and parse curl's cookie file (another hack) but the cookie
contains a value that is randomly generated, and the cookies in curl's
cookie jar are global to all PHP requests (so if multiple users made
the same request, their cookie value could be shared).

How can I retrieve the contents of a remote resource as well as the
value of a cookie sent with that resource?

Thanks,
Jason

P.S.: Also, as a minor side question; how, if at all, would the method
for doing this be different in PHP 4.4.9?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] [PHP 5.2.8] Retrieve cookies from remote host and pass them back to client.

2009-02-13 Thread Jason Cipriani
On Fri, Feb 13, 2009 at 1:03 PM, Andrew Ballard aball...@gmail.com wrote:
 On Fri, Feb 13, 2009 at 12:42 PM, Jason Cipriani jason.cipri...@gmail.com
 wrote:

 I have a PHP 5.2.8 web application that needs to retrieve a resource
 from a remote URL. However, the remote host also sends a cookie back
 (in a Set-Cookie header), and I need to grab that cookie and pass it
 on to the client (so that it's stored by the client browser). I do
 know the name of the cookie beforehand.

 What is the best way to do this? Passing a cookie from PHP back to the
 client is easy, it's grabbing the cookie from the remote URL that is
 the problem.

 There is no support for cookies in file_get_contents(), so that won't
 work. I can use curl to retrieve the remote URL with headers, but curl
 does not seem to provide an easy way to get the value of the cookie
 beyond retrieving the HTTP headers and parsing them manually. I could
 use and parse curl's cookie file (another hack) but the cookie
 contains a value that is randomly generated, and the cookies in curl's
 cookie jar are global to all PHP requests (so if multiple users made
 the same request, their cookie value could be shared).

 How can I retrieve the contents of a remote resource as well as the
 value of a cookie sent with that resource?

 Thanks,
 Jason

 P.S.: Also, as a minor side question; how, if at all, would the method
 for doing this be different in PHP 4.4.9?


 I usually use the HttpRequest for this type of stuff, but it only works with
 PHP 5.0/5.1 and greater. Otherwise, CURL is the next easiest, followed by
 direct socket connections.

Thanks! This is exactly what I was looking for. I've managed to get
everything working well with HttpRequest.

Cheers,
Jason

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php