Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A little cURL help please...getting an error 1

Status
Not open for further replies.

Bastien

Programmer
Joined
May 29, 2000
Messages
1,683
Location
CA
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:
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\ on line 383

Any advice would be appreciated.

TIA

Bastien

Cat, the other other white meat
 
Looks like you are trying to query a POP3 server. Why don't you use the build-in POP3 functions conatined with the IMAP function set:
 
Nope, the POP3 stuff works fine. Thanks for your help in that...

In this case I am taking the URL that is passed in the emails (which I can get sucessfully), and trying to open the file( a wav file to be specific) which is on a remote server.

I am able to link the the wav and play it within the app that this all goes to, but the goal here is to use that url to download the file, save it without user intervention (this page is runnning from a cron) and then store it in a mysql db. (Yeah, I know about saving the file in the db, but its the requirement...)

Thats the background. I can pass all the info to the function, but just having some problems in making it work right, just not famaliar with cURL and how it works with PHP.

If you know of a good tutorial, let me know.

Bastien

Cat, the other other white meat
 
Your error may be because CURLOPT_FILE tells cURL the file handle to which it will write the data, but you've given it a file handle opened as "r".


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Something more like this, then? No errors, but no file either...:( hmmmmmmmm.

Code:
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, $handle);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
			
curl_exec($ch);
			
curl_close($ch);

Bastien

Cat, the other other white meat
 
Just out of curiosity:
What was wrong with fsock()?
Did you take into consideration that the headers might need to be stripped - the file is not usable "as-is" since the server probably prepends the appropriate MIME headers.

 
Fsockopen gave these
Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in C:\Apache2\ on line 341

Warning: fsockopen(): unable to connect to in C:\Apache2\ on line 341

Warning: fclose(): supplied argument is not a valid stream resource in C:\Apache2\ on line 366

for this code
Code:
if(!$handle2 = fsockopen($file, 80, $errno, $errstr, 30)){
  $err_msg .= "Remote file not readable<br>";
}else{
   $data = file_get_contents($file);
}  
  	
$localfile = file_get_contents($handle2);
 	  
if ($localfile){
  if (!$handle = fopen($local_path.$newfilename, "w")){
     $err_msg = "File not opened<br>";
   }
   if($handle){
     if (!fwrite($handle, $localfile)){
       $err_msg = "can't write to file";
      }//end if
    }//end if
  	  
  
}// end if 

      fclose($handle2);
      fclose($handle);

Bastien

Cat, the other other white meat
 
Bastien:

Appending the port at the end of the URL will make your socket fail. If anything the port goes after the host:

I was able to hear your test file once the :80 was placed correctly. Also, did you state the port in the fsockopen() as second parameter? That should work.
 
Whooo....
I just saw that you use file_get_contents() instead of fread() which would be the method you should use. file_get_contents has it's own wrappers that might interfere. Just read the bytes from the socket file handle.
 
Can you post the code that you used? I am still getting errors regarding fsockopen.

I originally had only included the port in the fsockopen call and not in the url. Placing the port in the url, in the correct place, did not seem to help. Also changed to fread. No joy there either.

I was able to create the local files using the cURL method, but no data was being streamed to them. Is there something to be enabled in the ini file?

Basically I now have two functions, one using fsockopen and the other with cURL to attempt this...only need one to work :)...



Bastien

Cat, the other other white meat
 
This code:

Code:
<?php
$out = fopen('logo.gif', 'wb');
if ($out == FALSE){
  print "File not opened<br>";
  exit;
}
           
$ch = curl_init();
       
curl_setopt($ch, CURLOPT_FILE, $out);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, '[URL unfurl="true"]http://www.tek-tips.com/images/logo.gif');[/URL]
            
curl_exec($ch);
            
curl_close($ch);
fclose($handle);
?>

Works beautifully for me in fetching the new Pegasus TT logo (from the upper-left corner of every page in the site).


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No joy with

Code:
$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_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, '$file');
			            
curl_exec($ch);
			            
curl_close($ch);

The local files get created by have 0 bytes...

Bastien

Cat, the other other white meat
 
I put the curl_error into the code and discovered that its the '$file' portion of curl_setopt($ch, CURLOPT_URL, '$file'); that is part of the problem.

The error message is Couldn't resolve host '$file'


So I replace the single quotes with doubles and tested again, same problem, except the error is Couldn't resolve host ' ' with a blank...

Seriously confused at this point :(


Bastien

Cat, the other other white meat
 
sleipnir214

Yes, it does fetch the image.

Bastien

Cat, the other other white meat
 
The single quotes were seriously wrong. See the PHP online manual section on strings to see why. But you don't need any kind of quotes at all.


Where are you putting a value in $file? Have you verified the value is getting into $file correctly by later issuing a:

print $file;

before you try to use it in your invocation of curl_setopt()? If you hard-code the URL, does the script work?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Most strange, I reran the code after adding another echo $file; it works...shrug!

Thanks a ton, guys...

Bastien

Cat, the other other white meat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top