我正在尝试将远程文件(图像 PNG、GIF、JPG ...)复制到我的服务器。我使用 Guzzle 因为我有时会通过 copy() 得到 404,即使文件存在并且我还需要进行基本的身份验证。该脚本位于由 cron 作业触发的命令中启动的长脚本内。 我对 Guzzle 还很陌生,我成功复制了图像,但我的文件的 MIME 类型错误。我一定是在这里做错了什么。请建议我一个好方法来做到这一点(包括检查复制的成功/失败和 MIME 类型检查)。如果文件没有 mime 类型,我会弹出一个包含详细信息的错误。
这是代码:
$remoteFilePath = 'http://example.com/path/to/file.jpg';
$localFilePath = '/home/www/path/to/file.jpg';
try {
$client = new Guzzle\Http\Client();
$response = $client->send($client->get($remoteFilePath)->setAuth('login', 'password'));
if ($response->getBody()->isReadable()) {
if ($response->getStatusCode()==200) {
// is this the proper way to retrieve mime type?
//$mime = array_shift(array_values($response->getHeaders()->get('Content-Type')));
file_put_contents ($localFilePath , $response->getBody()->getStream());
return true;
}
}
} catch (Exception $e) {
return $e->getMessage();
}
当我这样做时,我的 mime 类型设置为 application/x-empty
而且看起来当状态不同于 200 时 Guzzle 会自动抛出异常。我怎样才能停止这种行为并自己检查状态以便自定义错误消息?
编辑: 这是针对 Guzzle 3.X 的 现在,您可以使用 Guzzle v 4.X 来完成此操作(也适用于 Guzzle 6)
$client = new \GuzzleHttp\Client();
$client->get(
'http://path.to/remote.file',
[
'headers' => ['key'=>'value'],
'query' => ['param'=>'value'],
'auth' => ['username', 'password'],
'save_to' => '/path/to/local.file',
]);
或者使用 Guzzle 流:
use GuzzleHttp\Stream;
$original = Stream\create(fopen('https://path.to/remote.file', 'r'));
$local = Stream\create(fopen('/path/to/local.file', 'w'));
$local->write($original->getContents());
这看起来很棒。使用 Guzzle 4 时有更好/合适的解决方案吗?
编辑(Guzzle 7) save_to 请求选项已被弃用,取而代之的是 sink 请求选项。提供 save_to 选项现在是 sink 的别名。
您的代码可以大大简化。我下面的示例代码会将响应正文直接流式传输到文件系统。
<?php
function copyRemote($fromUrl, $toFile) {
try {
$client = new Guzzle\Http\Client();
$response = $client->get($fromUrl)
->setAuth('login', 'password') // in case your resource is under protection
->setResponseBody($toFile)
->send();
return true;
} catch (Exception $e) {
// Log the error or something
return false;
}
}
当我这样做时,我的 mime 类型设置为 application/x-empty
文件系统 mimetype?
而且看起来当状态不同于 200 时 Guzzle 会自动抛出异常。我怎样才能停止这种行为并自己检查状态以便自定义错误消息?
Guzzle 会针对 4xx 和 5xx 等错误响应抛出异常。无需禁用此功能。只需捕获异常并处理那里的错误即可。
看这个帖子:
$myFile = fopen('path/to/file', 'w') or die('Problems');
$client = new \Guzzle\Service\Client();
$request = $client->post('https://www.yourdocumentpage.com', array(), ['pagePostField' => 'data'], ['save_to' => $myFile]);
$client->send($request);
fclose($myFile);
您必须在此处发送“帖子”的请求
并使用 get
$myFile = fopen('path/to/file', 'w') or die('Problems');
$client = new \GuzzleHttp\Client();
$request = $client->get('https://www.yourdocumentpage.com', ['save_to' => $myFile]);
在这里你不需要发送请求, 在这里你会发现很多文档,你必须有guzzle 6才能做到这一点,如果你同时使用GOUTTE,你将需要goutte 3.1,在你的composer.json中更新你的要求
使用 Guzzle 6 只需使用 SINK 选项。详细功能见下文
额外:
使用GuzzleHttp\Client;包含 Guzzle 命名空间
$access_token = 如果您需要身份验证,否则只需删除此选项
ReportFileDownloadException = 自定义异常
/**
* download report file and read data to database
* @param remote url
* @return N/A
* @throws ReportFileDownloadException
*/
protected function getReportFile($report_file_url)
{
$file = $this->tempDirectory . "/" . basename($report_file_url);
$fileHandle = fopen($file, "w+");
try {
$client = new Client();
$response = $client->get($report_file_url, [
RequestOptions::SINK => $fileHandle,
RequestOptions::HEADERS => [
"Authorization" => "Bearer $access_token"
]
]);
} catch (RequestException $e) {
throw new ReportFileDownloadException(
"Can't download report file $report_file_url"
);
} finally {
@fclose($fileHandle);
}
throw new ReportFileDownloadException(
"Can't download report file $report_file_url"
);
}