首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php curl在HTTP GET请求中自动设置正文

问Php curl在HTTP GET请求中自动设置正文
EN

Stack Overflow用户
提问于 2014-12-16 17:57:39
回答 1查看 544关注 0票数 1

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

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

我使用Php curl下载CSV文件。对于特定的CSV文件提供商,php curl没有实现在持续集成环境下下载文件,而在UAT和Prod上实现了下载。经过几个小时的调查,我想最接近问题的方法是输出请求头。

正如您在下面看到的,在持续集成环境中,php curl设置了"Content-Length“头,这表明它正在GET请求中发送正文。我现在还不能调试这个请求体。

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

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

如有任何帮助,我们将不胜感激:)

代码语言:javascript
复制
(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设置代码:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-22 01:32:09

首先,我输出的不是请求头,而是响应头。

我安装了MITM代理(http://mitmproxy.org/ -易于安装,易于使用),它允许从终端监控流量,我可以看到当从持续集成环境请求时,curl正在发送cookie,但它没有从UAT环境发送任何cookie。

在持续集成环境中删除已发送的cookies解决了此问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27501894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档