Пример #1
0
 /**
  * Download the file at $url and store locally.
  *
  * @param string $url The URL to fetch.
  * @param string $local_path The path to store the file, should be a full path, and include the desired filename
  * @param boolean $append_ext_based_on_mime If true, append an appropriate extension to $local_path.
  * @return array Array of The $local_path (with appended extension, if $append_ext_based_on_mime is true), and the response object
  */
 public function download($url, $local_path, $append_ext_based_on_mime = false)
 {
     $fp = fopen($local_path, 'w+');
     $additional_opts = [CURLOPT_FILE => &$fp, CURLOPT_TIMEOUT => 1800, CURLOPT_CONNECTTIMEOUT => 15];
     $response = $this->request($url, 'get', array(), $additional_opts);
     fclose($fp);
     $responseerrors = $response->errors();
     if (!empty($responseerrors)) {
         if (!empty($this->logger)) {
             $this->logger->debug('httpclient download failed: ' . $response->get_debug_string());
         }
         return [false, $response];
     }
     if ($append_ext_based_on_mime === true) {
         $mime = $response->mime_type();
         $ext = \pdyn\filesystem\Mimetype::mime2ext($mime);
         if (empty($ext) || $ext === 'unknown') {
             $ext = \pdyn\filesystem\FilesystemUtils::get_ext($url);
             $mime = \pdyn\filesystem\Mimetype::ext2mime($ext);
         }
         rename($local_path, $local_path . '.' . $ext);
         $local_path = $local_path . '.' . $ext;
     }
     return [$local_path, $response];
 }