title: how to download from CDN url?
body: <p>it can not download form urls that begins with CDN.
  this Link download('//yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg'); is Ok , but that Link download('//cdn.yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg'); is Not ok.
  What's wrong?</p>
  
  <p>
  
  <pre><code>    $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 =&gt; $value ) {
  
                  for ($i = 0; $i &lt; 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);    
  }
  </code></pre>
  
  <p>// ok
  download('<a href="http://yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg" rel="nofollow noreferrer">http://yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg</a>');</p>
  
  <p>// not 
  download('<a href="http://cdn.yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg" rel="nofollow noreferrer">http://cdn.yjc.ir/files/fa/news/1395/9/7/5488272_157.jpg</a>');</p>