【问题标题】:PHP cURL $_POST array not transmittedPHP cURL $_POST 数组未传输
【发布时间】:2013-11-21 14:50:28
【问题描述】:

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

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

在本地开发 HTTP 服务器 (Cherokee) 上完美运行。 在远程实时 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.

谁能告诉我应该在哪里调查?

【问题讨论】:

    标签: php post curl


    【解决方案1】:

    这两行需要颠倒:

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

    【讨论】:

    • 交换了订单。没有不同。此外, post_array = 是一个错字。我的错。
    【解决方案2】:

    因为application/x-www-form-urlencoded(默认)尝试http_build_query()

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

    【讨论】:

    • 为什么? cURL 可以直接接受一个数组并自行构建查询。所以http_build_query() 不是必需的。
    • 有趣的是,这会将@file 恢复到数组中,但没有恢复到 vars。我会期望它工作与否。多么奇怪。
    【解决方案3】:

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

    我无法找出为什么这适用于 Cherokee 而不是 Apache,但至少我有一个解决方法。迁移到使用 Cherokee 的网络主机会带来更多麻烦。

    我认为这是一种解决方法。也许它可以帮助将来的人。

    【讨论】:

      【解决方案4】:

      我认为该问题与在 POST 数据字段的开头使用“at”符号 (@) 有关。我的脚本中有同样的问题。我相信对数据进行 urlencoding 应该可以解决问题。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        相关资源
        最近更新 更多