2013-06-05 34 views
16

我试图将远程文件(图像PNG,GIF,JPG ...)复制到我的服务器。我使用Guzzle,因为即使文件存在,我有时也会得到404和copy(),我还需要进行基本身份验证。此脚本位于由cron作业触发的长命令中。 我是非常新的Guzzle,我成功复制图像,但我的文件有错误的MIME类型。我一定在这里做错了什么。请建议我一个很好的方法来做到这一点(包括检查复制和MIME类型检查的成功/失败)。如果文件没有MIME类型,我会弹出一个包含详细信息的错误。使用Guzzle复制远程文件

下面是代码:

$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类型设置为应用程序/ x-空

而且看起来当状态是从200狂饮会自动不同的像抛出异常。我怎样才能停止这种行为,并自己检查状态,以便我可以自定义错误消息?

编辑:这对狂饮3.X 现在这是如何使用狂饮v 4.X(有狂饮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', 
    ]); 

或者使用狂饮流做到这一点:

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

回答

21

您的代码可以简化很多。我下面的示例代码将直接将响应正文传递给文件系统。

<?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类型设置为应用程序/ x-空

一个文件系统的MIME类型?

此外它看起来像状态不同于200 Guzzle会自动抛出异常。我怎样才能停止这种行为,并自己检查状态,以便我可以自定义错误消息?

Guzzle会为4xx和5xx等不良响应抛出异常。无需禁用此功能。只要发现异常并处理那里的错误。

+0

非常感谢!我会试试这个。我希望它保持propre MIME类型(原始MIME类型)。 – Spir

+0

你需要做一些事情来更新磁盘上的文件以使用某种类型的MIME类型。我从来没有听说过这个。 –

+0

不,我的意思是我打破它/打破了mime类型。你的解决方案工作正常谢谢您的帮助。 – Spir

10

起来看看这款帖子:

$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); 

在这里,你必须把你的“邮报”

,并请求得到

$myFile = fopen('path/to/file', 'w') or die('Problems'); 
$client = new \GuzzleHttp\Client(); 
$request = $client->get('https://www.yourdocumentpage.com', ['save_to' => $myFile]); 

,在这里你不需要发送请求 和here你会发现很多文档,你必须使用guzzle 6才能做到这一点,如果你同时使用GOUTTE,你需要3.1版本的更新,更新您在composer.json中的要求

+0

一起工作是现在更好吗? –

+1

这与Guzzle 5.3.x一起工作,使用'save_to'选项完成了这项工作。谢谢! –

+4

只要注意 - 在Guzzle 6中,“save_to请求选项已被弃用,以支持sink请求选项。提供save_to选项现在是sink的别名”。 – TeeJay