使用Guzzle复制远程文件 [英] Copy remote file using Guzzle

查看:383
本文介绍了使用Guzzle复制远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将远程文件(图像PNG,GIF,JPG ...)复制到我的服务器.我使用 Guzzle ,因为有时我会使用

I'm trying to copy a remote file (image PNG, GIF, JPG ...) to my server. I use Guzzle since I sometimes get 404 with copy() even if the file exists and I also need to do a basic auth. This script is within a long script launched in command triggered by a cron job. I'm pretty new to Guzzle and I successfully copy the image but my files have wrong mime type. I must be doing something wrong here. Please suggest me a good way to do this (including checking success/failure of copy and mime type check). If file has no mime type I would pop an error with details informations.

这是代码:

$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

When I do this my mime type is set to application/x-empty

当状态不同于200时,Guzzle也会自动引发异常.如何停止这种行为并自己检查状态,以便可以自定义错误消息?

Also it looks like when status is different from 200 Guzzle will automatically throw an exception. How can I stop this behaviour and check status myself so I can custom error message?

编辑:这是针对Guzzle 3.X的 现在,您可以使用Guzzle v 4.X(与Guzzle 6兼容)来做到这一点

This was for Guzzle 3.X Now this is how you can do it using Guzzle v 4.X (works as well with 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流:

Or using Guzzle stream:

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时是否有更好/合适的解决方案?

This looks great. Is there better/proper solution when using Guzzle 4?

推荐答案

您的代码可以大大简化.我下面的示例代码会将响应主体直接流式传输到文件系统.

Your code can be simplified a great deal. My example code below will stream the body of the response directly to the filesystem.

<?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

When I do this my mime type is set to application/x-empty

文件系统模仿类型?

当状态不同于200时,Guzzle也会自动引发异常.如何停止这种行为并自己检查状态,以便可以自定义错误消息?

Also it looks like when status is different from 200 Guzzle will automatically throw an exception. How can I stop this behaviour and check status myself so I can custom error message?

Guzzle对于诸如4xx和5xx之类的不良响应将抛出异常.无需禁用此功能.只需捕获一个异常并在那里处理错误即可.

Guzzle will throw an exception for bad responses like 4xx and 5xx. No need to disable this. Just catch an exception and deal with the error there.

这篇关于使用Guzzle复制远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆