FOF30\Download\Adapter\Curl::downloadAndReturn PHP Метод

downloadAndReturn() публичный Метод

If this class' supportsChunkDownload returns false you should assume that the $from and $to parameters will be ignored.
public downloadAndReturn ( string $url, integer $from = null, integer $to = null, array $params = [] ) : string
$url string The remote file's URL
$from integer Byte range to start downloading from. Use null for start of file.
$to integer Byte range to stop downloading. Use null to download the entire file ($from is ignored)
$params array Additional params that will be added before performing the download
Результат string The raw file data retrieved from the remote URL.
    public function downloadAndReturn($url, $from = null, $to = null, array $params = array())
    {
        $ch = curl_init();
        if (empty($from)) {
            $from = 0;
        }
        if (empty($to)) {
            $to = 0;
        }
        if ($to < $from) {
            $temp = $to;
            $to = $from;
            $from = $temp;
            unset($temp);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSLVERSION, 0);
        curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'reponseHeaderCallback'));
        if (!(empty($from) && empty($to))) {
            curl_setopt($ch, CURLOPT_RANGE, "{$from}-{$to}");
        }
        if (!empty($params)) {
            foreach ($params as $k => $v) {
                @curl_setopt($ch, $k, $v);
            }
        }
        $result = curl_exec($ch);
        $errno = curl_errno($ch);
        $errmsg = curl_error($ch);
        $error = '';
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($result === false) {
            $error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_CURL_ERROR', $errno, $errmsg);
        } elseif ($http_status >= 300 && $http_status <= 399 && isset($this->headers['Location']) && !empty($this->headers['Location'])) {
            return $this->downloadAndReturn($this->headers['Location'], $from, $to, $params);
        } elseif ($http_status > 399) {
            $result = false;
            $errno = $http_status;
            $error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_HTTPERROR', $http_status);
        }
        curl_close($ch);
        if ($result === false) {
            throw new DownloadError($error, $errno);
        } else {
            return $result;
        }
    }

Usage Example

Пример #1
0
 /**
  * @covers  FOF30\Download\Adapter\Curl::downloadAndReturn
  *
  * @dataProvider    FOF30\Tests\Download\Adapter\CurlDataprovider::getTestDownloadAndReturn
  *
  * @param array $config
  * @param array $test
  */
 public function testDownloadAndReturn(array $config, array $test)
 {
     FakeCurl::setUp($config);
     $adapter = new Curl();
     if ($test['exception'] !== false) {
         $this->setExpectedException($test['exception']['name'], $test['exception']['message'], $test['exception']['code']);
     }
     $ret = $adapter->downloadAndReturn($test['url'], $test['from'], $test['to']);
     $retSize = 0;
     if (is_string($ret)) {
         $retSize = strlen($ret);
     }
     $this->assertEquals($test['retSize'], $retSize, $test['message']);
 }