[SOLVED] proxy server, CURL & images

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
User avatar
sakaveli
Forum Commoner
Posts: 60
Joined: Tue Apr 06, 2004 9:42 am

[SOLVED] proxy server, CURL & images

Post 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'");
}

?>
Last edited by sakaveli on Thu Apr 08, 2004 10:26 am, edited 2 times in total.
User avatar
sakaveli
Forum Commoner
Posts: 60
Joined: Tue Apr 06, 2004 9:42 am

Post 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;
	
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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:
Last edited by Weirdan on Fri Apr 09, 2004 7:49 am, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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...:(
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Image
:D
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Yum! :)
User avatar
sakaveli
Forum Commoner
Posts: 60
Joined: Tue Apr 06, 2004 9:42 am

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