我正在本地机器上设置客户端/服务器测试场景 - 我有一个测试客户端 PHP 脚本,旨在通过 cURL 将 XML-RPC 发送到服务器 PHP 脚本。
$cnxn = curl_init();
$log = fopen("/var/www/mobile-client.localhost/www/curl.log", "w");
curl_setopt_array($cnxn,
array(
CURLOPT_FRESH_CONNECT => false,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml",
),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "m=boomshakalaka",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_STDERR => $log,
CURLOPT_URL => "http://mobile-server.localhost/rpc/index.php",
CURLOPT_VERBOSE => true,
)
);
$response = curl_exec($cnxn);
echo "<pre>";
var_dump(curl_errno($cnxn) . ':' . curl_error($cnxn), "<hr>", $response);
fclose($log);
curl_close($cnxn);
但是,当服务器通过返回 POST 数组的内容(使用 var_dump($_POST)
)进行响应时,响应为空。
我在/var/www/mobile-client.localhost/www/curl.log
创建的日志文件内容如下:
* About to connect() to mobile-server.localhost port 80 (#0)
* Trying 127.0.0.1... * connected
* Connected to mobile-server.localhost (127.0.0.1) port 80 (#0)
> POST /rpc/index.php HTTP/1.1
Host: mobile-server.localhost
Accept: */*
Content-Type: text/xml
Content-Length: 15
< HTTP/1.1 200 OK
< Date: Tue, 25 Jan 2011 04:57:15 GMT
< Server: Apache/2.2.14 (Ubuntu)
< X-Powered-By: PHP/5.3.2-1ubuntu4.7
< Vary: Accept-Encoding
< Content-Length: 1337
< Content-Type: text/html
<
* Connection #0 to host mobile-server.localhost left intact
* Closing connection #0
我还使用 Wireshark 监控了我的环回地址,并在捕获日志中看到以下内容:
POST /rpc/index.php HTTP/1.1
Host: mobile-server.localhost
Accept: */*
Content-Type: text/xml
Content-Length: 15
m=boomshakalaka
我的 POST 数据消失在哪里?
对于那些对实际服务器响应感兴趣的人 - index.php 的内容是:
<?php
var_dump(":)");
var_dump("POST: ", $_POST);
var_dump("GET: ", $_GET);
var_dump("SERVER: ", $_SERVER);
最佳答案
注释掉这部分应该可以解决问题:
/*
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml",
),
*/
编辑----
您不需要明确指定内容类型。 cURL 将根据 CURLOPT_POSTFIELDS
是字符串 (application/x-www-form-urlencoded
) 还是数组 (multipart/form-data
)。 Reference .
关于PHP/cURL - 服务器没有收到 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4790413/