Page 1 of 1

[SOLVED] proxy server, CURL & images

Posted: Wed Apr 07, 2004 4:18 pm
by sakaveli
hi,

this problem is driving me insane!

i am behind proxie server and wish to download images from the internet so i can store them into a mysql database...im using Curl and have it setup fine, the function below is about to initialise the get LoadImage function:

Code: Select all

<?php

function storeImageURL($url,$id,$table = "item") {
	$type = getImageType($url);
	if ($type === FALSE) return FALSE;
	$data = loadImage($url);
	if ($data === FALSE) return FALSE;
	$rtn = saveImage($id,$data,$type,$table);
}
?>
after the type is verified the loadimage function is called with the valid url being passed to it:

Code: Select all

<?php
function loadImage($url) 
{

 //useful function  
 //@param  resource  $cx     connection 
 //@param  array       $opts  array of options: array(option_name => option_value) 
 //@return  void 


	$cx = curl_init($url); // create the connection 
	curl_set_options($cx, array('returntransfer' => true, 'followlocation' => true)); // set connection options 
	$data = curl_exec($cx); // execute the request
	curl_close($cx);	
	return $data;
	

// now $result holding the response of the remote server: image, html error-page, nothing - whatever. 
// check the PHP manual, cURL chapter for more options, such as proxies, authentication etc. 
}

?>
can ANYONE please help me? whats going wrong above?? i placed an echo just before the "return $data" and that echo comes up. but no image is saved?

just incase, the save function is below...

Code: Select all

<?php

function saveImage($id,$data,$type,$table = "item") {
	//clean up the data so SQL can understand it.
	$data = str_replace("","\",$data);
  	$data = str_replace("'","\''",$data);
  	$result = query("UPDATE $table set imagetype = '$type', image = '$data' where item_id = '$id'");
}

?>

Posted: Thu Apr 08, 2004 10:29 am
by sakaveli
here's how i solved it:

(no thanks to anyone) :evil:

Code: Select all

<?php

function loadImage($url) 
{
	$ch = curl_init($url); // create the connection
 	
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//allow redirects	
	curl_setopt($ch, CURLOPT_PROXY, "*******:3128"); //this should be your proxy server name and the port connection
	curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
	$data = curl_exec($ch); // execute the request
	curl_close($ch);	
	return $data;
	
}

?>

Posted: Fri Apr 09, 2004 4:27 am
by Weirdan
Originally there was the curl_set_options function definition in code I posted. You have omitted it in your code. Who's guilty, I'd ask you. :twisted:

Posted: Fri Apr 09, 2004 5:29 am
by patrikG
sakaveli wrote:here's how i solved it:

(no thanks to anyone) :evil:
Not every question can find an answer in one day - especially when christian holidays are coming up (someone give me an Easter egg, please).

I am glad you've found a solution - and posted it here :)

Now let me go find that Easter egg... oh, damn, have to wait until Sunday...:(

Posted: Fri Apr 09, 2004 7:16 am
by Weirdan
Image
:D

Posted: Fri Apr 09, 2004 7:32 am
by patrikG
Yum! :)

Posted: Fri Apr 09, 2004 11:32 am
by sakaveli
wierdan, thanks for your post, it was a bit hard to make sense of since you used a function which handled the Curl options. but you gave me a shove in the right direction! :D