tags:

views:

235

answers:

2
<?php
$data = array('name' => 'Ross', 'php_master' => true);


$url="http://localhost/test.php";
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
?>

The $_POST variable on the server side is empty. I also tried using Java servlet; request.getParameterNames() is also empty. Why are the post parameters lost?

A: 

you need to convert your $data array to a url-encoded string of key/value pairs:

$data = 'name=Ross&php_master=true';
Harold1983-
According to the manual it can be either an URL-encoded string or an array: http://www.php.net/manual/en/function.curl-setopt.php The only difference is that if you pass an array, the form will be encoded as `multipart/form-data` I don't think this is it, but nevertheless it's surely worth trying that way.
Pekka
Ah, good point - I've always just been in the habit of converting it to a string.
Harold1983-
Nope, tried that, the $_POST on server side/request.getParameterNames() are still empty.
nevermind, it works now on localhost! however it doesn't work on remote server, which goes through a series of redirect(ajp) from tomcat to http server, not sure why it would make a difference...
the redirects on the remote server seems to be another cause of not getting $_POST parameters, my domain.com is redirected to www.mydomain.com, if I use www.mydomain.com, the post parameters are passed correctly.
See also http://serverfault.com/questions/147547/apache-http-redirects-not-keeping-post-parameters
bmb
A: 

If you send an array in POSTFIELDS, it will be posted as multipart/form-data. I just tried your script and I can get the parameters in $_POST.

Looks like either your server is too old or not configured to support this encoding.

Here is the wire trace,

POST /test HTTP/1.1^M
Host: localhost^M
Accept: */*^M
Content-Length: 243^M
Expect: 100-continue^M
Content-Type: multipart/form-data; boundary=----------------------------b05745ba31db^M
^M
HTTP/1.1 100 Continue^M
^M
------------------------------b05745ba31db^M
Content-Disposition: form-data; name="name"^M
^M
Ross^M
------------------------------b05745ba31db^M
Content-Disposition: form-data; name="php_master"^M
^M
1^M
------------------------------b05745ba31db--^M
HTTP/1.1 200 OK^M
Date: Wed, 02 Jun 2010 20:37:57 GMT^M
Server: Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.7l DAV/2 PHP/5.2.12^M
X-Powered-By: PHP/5.2.12^M
Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/^M
Transfer-Encoding: chunked^M
Content-Type: text/html^M
ZZ Coder