Php curl在HTTP GET请求中自动设置body

时间:2014-12-16 09:57:39

标签: php curl

这是我的问题。我有三个环境(apache + mysql + mongoDB):持续集成,UAT和Prod。

所有三个环境都位于同一个proxmox上托管的不同VM上,并且可以通过nginx服务器访问它们。

我使用Php curl下载CSV文件。对于特定的CSV文件提供程序,php curl无法在UAT和Prod上实现在Continuous Intergration环境中下载文件。经过几个小时的调查,我发现最接近问题的是输出请求标题。

如下所示,在Continuous Integration环境中,php curl设置了“Content-Length”标题,表示它正在GET请求中发送正文。我暂时无法调试此请求正文。

我真的不明白为什么php curl会在CI环境中添加这个请求主体,特别是当它在UAT和Prod env中没有这样做时。

我也加入了php curl设置代码。

任何帮助将不胜感激:)

(Request Header in Continuous Integration Env)
------------------------------------------
HTTP/1.1 413 Request Entity Too Large
Date: Mon, 15 Dec 2014 21:01:22 GMT
Server: Apache
Content-Length: 341
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>413 Request Entity Too Large</title>
</head><body>
<h1>Request Entity Too Large</h1>
The requested resource<br />/export/export<br />
does not allow request data with GET requests, or the amount of data provided in
the request exceeds the capacity limit.
</body></html>
-----------------------------------------

(Request Header UAT Env)
------------------------------------------
HTTP/1.1 200 OK
Date: Mon, 15 Dec 2014 21:05:35 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/plain; charset=UTF-8

+ CSV data
----------------------------------------------

Php curl设置代码:

public function getResource($url, $outputHeaders = false, $timeout = false)
{
    $ch = curl_init($url);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36');

    // Follow redirections
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    // Use navigation cookies
    $cookieFilePath = APP . 'tmp' . DS . 'cookies' . DS . 'curl_cookie.txt';
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilePath);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilePath);

    // Trust any certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    if ($timeout) {
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    }

    if($outputHeaders)
        curl_setopt($ch, CURLOPT_HEADER, 1);

    $data = curl_exec($ch);

    if(curl_errno($ch))
    {
        throw new Exception('Erreur Curl : ' . curl_error($ch));
    }

    if(empty($data))
        throw new Exception('Curl returned an empty html page.');

    $encoding = self::getEncoding($ch, $data);

    $html['data'] = $data;
    $html['encoding'] = $encoding;
    $html['effective_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

    curl_close($ch);

    return $html;
}

1 个答案:

答案 0 :(得分:0)

首先,我没有输出请求标题,而是输出响应。

我安装了MITM代理(http://mitmproxy.org/ - 易于安装,易于使用),允许从终端进行流量监控,我可以看到curl在从Continuous Integration环境请求时发送cookie但是它不是从UAT环境发送任何内容。

在Continuous Integration环境中删除已发送的cookie解决了该问题。