PHP curl post参数未传递给服务器

时间:2010-06-02 17:20:24

标签: php curl

<?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);
?>

服务器端的$_POST变量为空。我也尝试过使用Java servlet; request.getParameterNames()也是空的。为什么帖子参数会丢失?

2 个答案:

答案 0 :(得分:0)

您需要将$ data数组转换为url编码的键/值对字符串:

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

答案 1 :(得分:0)

如果您在POSTFIELDS中发送数组,它将作为multipart / form-data发布。我刚试过你的脚本,我可以在$ _POST中获取参数。

看起来您的服务器太旧或未配置为支持此编码。

这是线迹,

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