FOF30\Download\Adapter\Fopen::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())
    {
        if (empty($from)) {
            $from = 0;
        }
        if (empty($to)) {
            $to = 0;
        }
        if ($to < $from) {
            $temp = $to;
            $to = $from;
            $from = $temp;
            unset($temp);
        }
        if (!(empty($from) && empty($to))) {
            $options = array('http' => array('method' => 'GET', 'header' => "Range: bytes={$from}-{$to}\r\n"), 'ssl' => array('verify_peer' => true, 'cafile' => __DIR__ . '/cacert.pem', 'verify_depth' => 5));
            $options = array_merge($options, $params);
            $context = stream_context_create($options);
            $result = @file_get_contents($url, false, $context, $from - $to + 1);
        } else {
            $options = array('http' => array('method' => 'GET'), 'ssl' => array('verify_peer' => true, 'cafile' => __DIR__ . '/cacert.pem', 'verify_depth' => 5));
            $options = array_merge($options, $params);
            $context = stream_context_create($options);
            $result = @file_get_contents($url, false, $context);
        }
        global $http_response_header_test;
        if (!isset($http_response_header) && empty($http_response_header_test)) {
            $error = JText::_('LIB_FOF_DOWNLOAD_ERR_FOPEN_ERROR');
            throw new DownloadError($error, 404);
        } else {
            // Used for testing
            if (!isset($http_response_header) && !empty($http_response_header_test)) {
                $http_response_header = $http_response_header_test;
            }
            $http_code = 200;
            $nLines = count($http_response_header);
            for ($i = $nLines - 1; $i >= 0; $i--) {
                $line = $http_response_header[$i];
                if (strncasecmp("HTTP", $line, 4) == 0) {
                    $response = explode(' ', $line);
                    $http_code = $response[1];
                    break;
                }
            }
            if ($http_code >= 299) {
                $error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_HTTPERROR', $http_code);
                throw new DownloadError($error, $http_code);
            }
        }
        if ($result === false) {
            $error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_FOPEN_ERROR');
            throw new DownloadError($error, 1);
        } else {
            return $result;
        }
    }

Usage Example

Пример #1
0
 /**
  * @covers  FOF30\Download\Adapter\Fopen::downloadAndReturn
  *
  * @dataProvider    FOF30\Tests\Download\Adapter\FopenDataprovider::getTestDownloadAndReturn
  *
  * @param array $config
  * @param array $test
  */
 public function testDownloadAndReturn(array $config, array $test)
 {
     FakeFopen::setUp($config);
     $adapter = new Fopen();
     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']);
 }