Codesleeve\Stapler\Factories\File::createFromUrl PHP Метод

createFromUrl() защищенный статический Метод

Fetch a remote file using a string URL and convert it into an instance of Codesleeve\Stapler\File\File.
protected static createFromUrl ( string $file ) : Codesleeve\Stapler\File\File
$file string
Результат Codesleeve\Stapler\File\File
    protected static function createFromUrl($file)
    {
        $ch = curl_init($file);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $rawFile = curl_exec($ch);
        curl_close($ch);
        // Remove the query string and hash if they exist
        $file = preg_replace('/[&#\\?].*/', '', $file);
        // Get the original name of the file
        $pathinfo = pathinfo($file);
        $name = $pathinfo['basename'];
        $extension = isset($pathinfo['extension']) ? '.' . $pathinfo['extension'] : '';
        // Create a temporary file with a unique name.
        $tempFile = tempnam(sys_get_temp_dir(), 'stapler-');
        if ($extension) {
            $filePath = $tempFile . "{$extension}";
        } else {
            // Since we don't have an extension for the file, we'll have to go ahead and write
            // the contents of the rawfile to disk (using the tempFile path) in order to use
            // symfony's mime type guesser to generate an extension for the file.
            file_put_contents($tempFile, $rawFile);
            $mimeType = MimeTypeGuesser::getInstance()->guess($tempFile);
            $extension = static::getMimeTypeExtensionGuesserInstance()->guess($mimeType);
            $filePath = $tempFile . '.' . $extension;
        }
        file_put_contents($filePath, $rawFile);
        unlink($tempFile);
        return new StaplerFile($filePath);
    }