【发布时间】:2015-04-13 16:23:22
【问题描述】:
我正在通过 Guzzle 3.8.1 发出超过 2MB 的 Jasper 报告(通过 Jasper 服务器 API)的请求,我收到了包含正确 Content-Length 标头但没有响应正文的响应。
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;
}
【问题讨论】:
-
您是否尝试过设置用户代理:
$client->setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36'); -
@halfer 这就是 curl 命令行工具在我使用 -v(详细)标志时输出它的方式
-
@halfer 我正在使用 jasper 的 REST api 来检索报告。该报告使用 jasperserver ui 工作正常,当我通过浏览器点击 url 或在命令行上 curl 时。它似乎只是通过 php 使用 guzzle 给出一个空的响应
-
@pdizz - 你确定 curl 在命令行运行时没有设置默认的
User-Agent? -
@Cyclone 我错了,看起来 guzzle 正在发送
Guzzle/3.8.1 curl/7.19.7 PHP/5.5.8而 curl 正在为 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