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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

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

Post 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
Last edited by lostboy on Thu Sep 16, 2004 3:57 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could lose the CURLOPT_FILE line and write to the file yourself since you are returning the transfer.....
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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);
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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
Post Reply