如何从CDN网址下载?

时间:2016-11-27 11:23:18

标签: php download cdn

它无法下载以CDN开头的表单网址。 这个链接下载('// yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg');没关系,但链接下载('// cdn.yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg');不行。 怎么了?

    $ret = '';

    if ( $url != '' ) {

        $parse_url = explode('/', $url);
        $patterns = array();
        $patterns[0] = '/[\x{06F0}-\x{06F9}]/u';
        $patterns[1] = '/[\\s,\x{06A9}\x{06AF}\x{06C0}\x{06CC}\x{060C}\x{062A}\x{062B}\x{062C}\x{062D}\x{062E}\x{062F}\x{063A}\x{064A}\x{064B}\x{064C}\x{064D}\x{064E}\x{064F}\x{067E}\x{0670}\x{0686}\x{0698}\x{200C}\x{0621}-\x{0629}\x{0630}-\x{0639}\x{0641}-\x{0654}]/u';          
        $patterns[2] = '/(،|؟|«|»|؛|٬)/u';

        if ( count($parse_url) ) {

            foreach ( $parse_url as $key => $value ) {

                for ($i = 0; $i < count($patterns); $i++ ) {

                    if (preg_match($patterns[$i], $value) ){

                        $parse_url[$key] = urlencode($value);

                    };

                };

            };

            $ret = implode('/', $parse_url);

        };

    };

    return $ret;    

}


function getFinalUrl( $url, $timeout = 75 ) {

    $url = urlEncode_($url);

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // tried true/false
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // tried true/false
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302) {

        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
        $headers = get_headers(urlEncode_($response['url']));

        $location = "";
        foreach( $headers as $value ) {

            if ( substr( strtolower($value), 0, 9 ) == "location:" ) {

                return getFinalUrl( trim( substr( $value, 9, strlen($value) ) ) );

            }

        }

    }

    if ( preg_match("/window.location.replace('(.*)')/i", $content, $value) ||
        preg_match("/window.location=[\"'](.*)[\"']/i", $content, $value) ||
        preg_match("/location.href=[\"'](.*)[\"']/i", $content, $value) ) { 

        return getFinalUrl ( $value[1] );

    } else {

        return $response['url'];

    }
}


function download ($url = '') {

    if ($url == '') return false;

    $url = getFinalUrl($url);

    $parsUrl = parse_url($url) ;
    $scheme = $parsUrl['scheme'];
    $file_name = basename($url);
    $fp = fopen($file_name, "w+") or die("Error File ...");

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 75);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);


    if ($scheme == 'https') {

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    }

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

    curl_setopt($ch, CURLOPT_FILE, $fp);

    $exec = curl_exec($ch);
    curl_close($ch);

    fflush($fp);
    fclose($fp);    
}

//好的 下载( 'http://yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg');

//不是 下载( 'http://cdn.yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg');

1 个答案:

答案 0 :(得分:1)

使用此处的代码:https://stackoverflow.com/a/724449/7215875

我能够下载这两张图片,但是,在尝试使用curl时,它无法为我下载这两张图片。