开发者

PHP curl post parameters not passed to server

<?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 e开发者_高级运维mpty. Why are the post parameters lost?


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

$data = 'name=Ross&php_master=true';


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜