Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Guzzle中使用'sink'选项下载文件会导致空文件。为什么?如何修复它?_Php_Download_Guzzle_Guzzle6 - Fatal编程技术网

Php 在Guzzle中使用'sink'选项下载文件会导致空文件。为什么?如何修复它?

Php 在Guzzle中使用'sink'选项下载文件会导致空文件。为什么?如何修复它?,php,download,guzzle,guzzle6,Php,Download,Guzzle,Guzzle6,我需要用Guzzle下载一个文件。目前我使用的是6.3.3版 我将该选项传递给我的请求,但尽管我请求的API响应“200OK”,其中包含一些正文内容,但目标文件始终为空 以下是我目前掌握的代码: // sidenote: // $this->importFile is the absolute path to the file the contents have to be downloaded to // $this->api is a GuzzleHttp\Client, bas

我需要用Guzzle下载一个文件。目前我使用的是6.3.3版

我将该选项传递给我的请求,但尽管我请求的API响应“200OK”,其中包含一些正文内容,但目标文件始终为空

以下是我目前掌握的代码:

// sidenote:
// $this->importFile is the absolute path to the file the contents have to be downloaded to
// $this->api is a GuzzleHttp\Client, base URL has been set previously
// $uri is the API endpoint's URI I am requesting (like "get/data")
// $this->getQueryParams() returns an array with a few needed parameters

$downloadDestination = fopen($this->importFile, 'w');

$response = $this->api->get($uri, [
    'query' => $this->getQueryParams(),
    'sink' => $downloadDestination,
]);

var_dump(file_get_contents($this->importFile));
var_dump($response->getBody()->getContents());
die;
顺便说一句,我是在命令(
bin/console blah:custom command
)中的Symfony(3.4)应用程序上下文中调用它的。上面的代码片段是我的一个服务类的一部分

这将在我的终端中生成一个新创建但为空的文件和以下输出:

string(0) ""
string(2065) "{"id":"123", … }"
# the latter one is actually a large JSON string, I just shortened it here
有人知道我做错了什么吗?事实上,这不是火箭科学。现在我越是困惑,我下载的目标文件已经创建,但它的内容不会被写入

Guzzle或类似的东西缺少某种配置吗?

该死!这完全是我自己的错。我也应该发布Guzzle客户端的初始化。那我就可以早点发现我的错误了

在我添加
sink
选项(将响应主体作为文件下载)之前,我的服务类必须逐行处理响应(因为我使用的API响应的数据大小高达1GB)。因此,我先前也添加了该选项。这一个正在与
接收器发生碰撞

因此,我的解决方案是从客户端的初始化中删除
选项等等。它起作用了

$this->api = new Client([
    'base_uri' => $apiBaseUrl,
    'stream' => true,
]);