Page 1 of 1

[SOLVED]...cURL help please...

Posted: Thu Sep 16, 2004 11:00 am
by lostboy
Hi All,

Trying to get a file from a remote server. fopen and fsockopen were troublesome so I thought I'd try cURL. Have never used it though :)

So I built this function

Code: Select all

function get_file1($file, $local_path, $newfilename)
    {
            $err_msg = '';
           echo "<br>Attempting message download<br>";
           echo "File: fopen($file, 'r')<br>";
           
           if (!$handle = fopen($newfilename, 'w')){
             $err_msg = "File not opened<br>";
           }
           
            list($site, $uri) = explode("/",$file);
            $ch = curl_init($site);
            $handle2 = fopen($uri, "r");
        
            curl_setopt($ch, CURLOPT_FILE, $handle2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            
            $data = curl_exec($ch);
            
            if ($handle){
            if (fwrite($handle,$data) === FALSE){
               $err_msg .= "Unable to write to file<br>";
            }
          }
            
            curl_close($ch);
            fclose($handle2);
            
            if ($handle){ fclose($handle); }
         if ($handle2){ fclose($handle2);}
}//end function
and am getting this error. I am sure I've done something silly...:(

error:
Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in C:\Apache2\WWW\localhost\email\custom_pop3.php on line 383
Any advice would be appreciated.

TIA

Posted: Thu Sep 16, 2004 11:28 am
by feyd
maybe the fopen call failed?

Although judging by this:
CURLOPT_FILE The file that the transfer should be written to. The default is STDOUT (the browser window).
your permissions on the file are wrong.

Posted: Thu Sep 16, 2004 12:08 pm
by lostboy
ok, made some changes and now the error is not there, but I don't have a file.

Code: Select all

list($site, $uri) = explode("/",$file);
$ch = curl_init($site);
$handle2 = fopen($uri, "r");
   	 
curl_setopt($ch, CURLOPT_FILE, $handle);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
			
curl_exec($ch);
curl_close($ch);
Just a little debugging help and I'll have this puppy nailed.

TIA

Posted: Thu Sep 16, 2004 12:30 pm
by feyd
you could lose the CURLOPT_FILE line and write to the file yourself since you are returning the transfer.....

Posted: Thu Sep 16, 2004 12:47 pm
by lostboy
So how would that look?

like this?:

Code: Select all

if (!$handle = fopen($local_path.$newfilename, 'w')){
	   	  $err_msg = "File not opened<br>";
	   	}
	   	
			list($site, $uri) = explode("/",$file);
			$ch = curl_init($site);
			$handle2 = fopen($uri, "r");
   	 
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			
			$data = curl_exec($ch);
			
			if($handle){
			  if (!fwrite($handle, $data)){
			    $err_msg="can't write to file";
			  }//end if
			}//end if
			
			curl_close($ch);

Posted: Thu Sep 16, 2004 3:56 pm
by lostboy
Fixed with this code

Code: Select all

function get_file1($file, $local_path)
    {
$err_msg = '';
echo "<br>Attempting message download for $file<br>";
$time = time();
$newfile = $local_path.'my'.$time.'.wav';
$out = fopen($newfile, 'wb');
if ($out == FALSE){
  print "File not opened<br>";
  exit;
}
           
$ch = curl_init();
			       
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
			            
curl_exec($ch);
echo "<br>Error is : ".curl_error ( $ch);
           
curl_close($ch);
//fclose($handle);

}//end function