你确定你正确地转义了参数吗?只需为此目的使用 urlencode()。这是一个例子:
<?php
$url = 'http://localhost/';
$fields = array (
'param1' => 'val1',
'param2' => 'val2'
);
$qry = '';
foreach ($fields as $key => $value) {
$qry .= $key . '=' . urlencode($value) . '&';
}
$qry = rtrim($qry, '&');
// Alternatively, you can also use $qry = http_build_query($fields, '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
如果您想验证请求是否正确发送,我会推荐 netcat。只需将 URL 设置为http://localhost:3333/,然后使用以下命令执行 netcat:$ nc -l -p 3333
不出所料,请求如下所示: POST / HTTP/1.1 Host: localhost:3333 Accept: /
Content-Length: 23 Content-Type: application/x-www-form-urlencoded
param1=val1¶m2=val2