PHP cURL$_POST数组未传输


PHP cURL $_POST array not transmitted

我看过其他答案,但我相信我的问题可能有所不同。我不是一个cURL高手,我希望有人能告诉我应该在哪里努力。

本地PHP脚本传输包含文本值和文件的$_POST数组。数据由服务器上的PHP脚本接收。公式中没有浏览器。我从网上查到了密码。

可在本地开发HTTP服务器(切罗基)上完美工作。在远程实时HTTP服务器(Apache)上失败。

发送代码:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    post_array=array("my_file"=>"@".$File,"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
if(!curl_errno($ch))
{
 $info = curl_getinfo($ch);
 echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

发送脚本将$_POST内容显示为:

Array
(
    [my_file] => @<path to file>
    [upload] => Upload
    [var1] => P1
    [var2] => P2
    [var3] => P3
)

在远程服务器上,$_POST数组由脚本接收为:

Array
(
    [upload] => Upload
)

我使用CURLOPT_VERBOSE,得到以下屏幕输出:

* About to connect() to <domain name> port 80 (#0)
*   Trying 82.71.205.4... * connected
> POST <receiving file> HTTP/1.1
User-Agent: Mozilla/4.0 (compatible;)
Host: <domain name>
Accept: */*
Content-Length: 22091
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------734a2c311f30
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Thu, 21 Nov 2013 14:09:13 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.17
< Content-Length: 199
< Content-Type: text/html
< 
* Connection #0 to host <domain name> left intact
Took 0.718261 seconds to send a request to <receiving file>

所以,我知道以下内容:

1/ Script works perfectly on dev server, but on live server...
2/ curl_errno says no errors.
3/ CURLOPT_VERBOSE reports expected array size (22091).
4/ Receiving file receives data, but not $_POST array content.
5/ No entries in server error log.

有人能告诉我应该在哪里调查吗?

这两行需要反转:

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
post_array=array("my_file"=>"@".$File,"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);

另外,post_array = ...无效。您正在为一个常量赋值。

应该是:

$post = array( blah blah blah );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

由于application/x-www-form-urlencoded(默认情况下),请尝试http_build_query():

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array));

这不是一个正确的答案,但我发现,如果我只上传文件,而不在$_POST数组中包含任何其他值,那么它在Apache上就可以工作。

我一直不知道为什么这在切罗基而不是Apache上有效,但至少我有一个变通办法。转移到一个使用切诺基语的网络主机将是一个比它的价值更多的麻烦。

我认为这是一个变通办法。也许这对将来的某个人有帮助。

我认为这个问题与POST数据字段开头的"at"符号(@)有关。我的剧本也有同样的问题。我认为对数据进行url编码应该可以解决这个问题。

post_array=array("my_file"=>urlencode("@".$File),"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);