Copy Images at URL onto local

Discussion in 'PHP' started by protocol96, Aug 19, 2008.

  1. #1
    Hi,

    I am trying to copy static images from the Google Maps by passing latitude and longitude with the following code, but when I try to write it to text file, it said --> invalid or illegal request and writing it to a gif gives to no image.

    function cURLcheckBasicFunctions()
    {
      if( !function_exists("curl_init") &&
          !function_exists("curl_setopt") &&
          !function_exists("curl_exec") &&
          !function_exists("curl_close") ) return false;
      else return true;
    }
    
    function cURLdownload($url)
    {
    $file = array();
      if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
      $ch = curl_init();
      if($ch)
      {
        $fp = fopen("/var/www/html/maps/x.gif", "w");
        if($fp)
        {
          if( !curl_setopt($ch, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)";
          if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
          if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
          if( !curl_exec($ch) ) return "FAIL: curl_exec()";
          curl_close($ch);
          fclose($fp);
          return "SUCCESS: $file [$url]";
        }
        else return "FAIL: fopen()";
      }
      else return "FAIL: curl_init()";
    }
    
    echo cURLdownload("URL");
    HTML:

     
    protocol96, Aug 19, 2008 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    Try the following code.

    function OpenURL($url,$ref=null) {
    
    	//Open with cURL
    	$header = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";  
    
    	$ch = curl_init($url); 
    	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_HEADER, 0);  
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
    	curl_setopt($ch, CURLOPT_REFERER, $ref);
    	curl_setopt($ch, CURLOPT_USERAGENT, $header); 
    	$data = curl_exec ($ch); 
    	curl_close ($ch);
    
    	return $data;
    }
    Code (markup):
     
    Alex Roxon, Aug 20, 2008 IP