Guzzle curl请求获取空响应体但状态为200且Content-Length标头正确

时间:2015-02-12 19:12:34

标签: php http curl guzzle

我正在通过Guzzle 3.8.1向Jasper报告(通过Jasper Server API)发出超过2MB的请求,并且我收到了具有正确Content-Length标头但没有响应主体的响应。 / p>

Guzzle请求:

GET /jasperserver/rest_v2/reports/projects/i3app_suite/Resource/BulkShiftExport.csv?ACCOUNT_ID=2&START_DATETIME=2015-01-01&END_DATETIME=2015-01-31 HTTP/1.1
Host: jasper.i3app:8080
User-Agent: Guzzle/3.8.1 curl/7.19.7 PHP/5.5.8
Authorization: Basic ***=

响应:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: private
Expires: Wed, 31 Dec 1969 17:00:00 MST
P3P: CP="ALL"
Set-Cookie: JSESSIONID=F0B0F72B65A8145B45DA9DB2BACE53D8; Path=/jasperserver/; HttpOnly, userLocale=en_US;Expires=Fri, 13-Feb-2015 18:56:44 GMT;HttpOnly
Content-Disposition: attachment; filename="BulkShiftExport.csv"
output-final: true
Content-Type: application/vnd.ms-excel
Content-Length: 2173897
Date: Thu, 12 Feb 2015 18:57:02 GMT

如果我通过命令行上的curl发出此请求(或在浏览器中请求它),我会按预期获得报告

GET /jasperserver/rest_v2/reports/projects/i3app_suite/Resource/BulkShiftExport.csv?ACCOUNT_ID=2&START_DATETIME=2015-01-01&END_DATETIME=2015-01-30 HTTP/1.1
Authorization: Basic ***=
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: jasper.i3app:8080
Accept: */*


< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Cache-Control: private
< Expires: Wed, 31 Dec 1969 17:00:00 MST
< P3P: CP="ALL"
< Set-Cookie: JSESSIONID=AF1BF885354AF3E352DD9E18FA044A4B; Path=/jasperserver/; HttpOnly
< Set-Cookie: userLocale=en_US;Expires=Fri, 13-Feb-2015 19:03:42 GMT;HttpOnly
< Content-Disposition: attachment; filename="BulkShiftExport.csv"
< output-final: true
< Content-Type: application/vnd.ms-excel
< Content-Length: 2113902
< Date: Thu, 12 Feb 2015 19:03:49 GMT
< 
{ [data not shown]

我能看到的唯一区别是curl请求中的Accept: */*。我尝试将该标题添加到guzzle请求并获得相同的结果。

当通过Guzzle客户端发出请求时,它似乎花费相同的时间(5-6秒)来接收响应,并设置Content-Length标头,但响应正文为空。为什么我得到一个空的响应体,虽然Guzzle使用curl而不是在命令行上使用curl时?我需要设置一个选项才能使其工作吗?

    $request = $this->getGuzzleClient()->createRequest('GET');

    $config = $this->getConfig();
    $url = new Url(
        $config['scheme'],
        $config['host'],
        $config['user'],
        $config['pass'],
        $config['port'],
        $config['path'] . $reportPath . '.' . $format,
        new QueryString($parameters)
    );
    $request->setUrl($url);

    $response = $request->send();

...

public function getGuzzleClient()
{
    if (!$this->restClient) {
        $client = new GuzzleClient();
        $this->setRestClient($client);
    }
    return $this->restClient;
}

1 个答案:

答案 0 :(得分:0)

就我而言,我错误地使用了MockHandler