PHP Proxy How ?

Discussion in 'PHP' started by SNaRe, Dec 23, 2007.

  1. #1
    For example i have ten proxy ip in arrays.
    I want to use proxy while entering websites with php(for example grabbing data curl, file_get_contents)
    Can you show me an example ?
    Or a source about it . I won't make a proxy script. A website blocks me when i make too many queries so i want to use proxy and pass this restrtiction
     
    SNaRe, Dec 23, 2007 IP
  2. Vio82

    Vio82 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There is my personal grabbing class, writen a long time ago =)
    Maybe it will be usefull for you:

    
    <?php
    class Grab {
    	
    	/**
    	 * Enter description here...
    	 *
    	 * @param $url
    	 * @param $stopwords
    	 * @param $minlen
    	 * @param $display_url_in_errors
    	 * @param $method
    	 * @param $postfields
    	 * @param $timeout
    	 * @param $header
    	 * @param $referer
    	 * @param $useragent
    	 * @return unknown
    	 */
    	function get(
    		$url, 
    		$stopwords = array(), 		// array of stopwords which mean that proxy id banned
    		$minlen = '', 				// minimum lenght of result
    		$display_url_in_errors = 1,
    		$display_content_in_errors = 1,
    		$method = '0', 				// 0 - GET, 1 - POST
    		$postfields = false, 	
    		$timeout = 20, 
    		$header = 0, 
    		$referer = '', 
    		$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'
    	){
    		$ch = curl_init();
    		curl_setopt ($ch,CURLOPT_URL,$url);
    		curl_setopt ($ch,CURLOPT_USERAGENT,$useragent);
    		curl_setopt ($ch,CURLOPT_HEADER,$header);
    		curl_setopt ($ch,CURLOPT_REFERER,$referer);
    		curl_setopt ($ch,CURLOPT_FOLLOWLOCATION,1);
    		curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1);
    		curl_setopt ($ch,CURLOPT_TIMEOUT,$timeout);
    		curl_setopt ($ch,CURLOPT_POST,$method);
    		
    		// If want POST put some postfields
    		if($method == 1 && $postfields){
    			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
    		}
    		$err_url = $display_url_in_errors?' =='.$url.'== ':'';
    		
    			$response = curl_exec($ch);
    			$error = curl_error($ch);
    			curl_close($ch);
    			
    			if($error){	return array("", "GRAB_ERROR: cURL says [".curl_error($ch)."] (nonproxy)".$err_url."
    ");}
    				
    			if(count($stopwords)){
    				foreach ($stopwords as $word){
    					if(strpos($response,$word)){
    						return array($response, "GRAB_ERROR: match stopword [".$word."] (nonproxy)".$err_url."
    ");					
    					}
    				}
    			}
    			
    			if($minlen && ($minlen > strlen($response))){return array($response, "GRAB_ERROR: small lenght [".round((strlen($response)/1000), 2)." Kb] (nonproxy)".$err_url."
    ");}
    			return array($response, false);
    			
    		
    	}
    		
    	
    	function proxy_get(
    		$url, 
    		$proxy = '', 				// path/to/file which consist list of proxyes OR leave to get without proxy
    		$stopwords = array(), 		// array of stopwords which mean that proxy id banned
    		$minlen = '', 				// minimum lenght of result
    		$delete_dead = 1,
    		$delete_banned = 1,
    		$get_next_if_stopword = 1,  // Get next proxy if stopword matching
    		$get_next_if_error = 1,  	// Get next proxy if curl error
    		$display_url_in_errors = 1,
    		$display_content_in_errors = 1,
    		$method = '0', 				// 0 - GET, 1 - POST
    		$postfields = false, 	
    		$timeout = 20, 
    		$header = 0, 
    		$referer = '', 
    		$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
    		$recall_err = array(),
    		$start_from = 0 // If proxy baned or dead and $delete_dead = 0 -> get number from thouse proxy starting 
    	){	
    		
    		
    		$ch = curl_init();
    		curl_setopt ($ch,CURLOPT_URL,$url);
    		curl_setopt ($ch,CURLOPT_USERAGENT,$useragent);
    		curl_setopt ($ch,CURLOPT_HEADER,$header);
    		curl_setopt ($ch,CURLOPT_REFERER,$referer);
    		curl_setopt ($ch,CURLOPT_FOLLOWLOCATION,1);
    		curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1);
    		curl_setopt ($ch,CURLOPT_TIMEOUT,$timeout);
    		curl_setopt ($ch,CURLOPT_POST,$method);
    		
    		// If want POST put some postfields
    		if($method == 1 && $postfields){
    			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
    		}
    		$err_url = $display_url_in_errors?' =='.$url.'== ':'';
    		$size = filesize($proxy);
    		
    		if($size < 7){return array("", "GRAB_ERROR: file [".$proxy."] is empty
    ");}
    		
    		$fp = fopen($proxy, 'r');
    		$cont = trim(fread($fp, $size));
    		fclose($fp);
    		$proxy_array = explode('
    ', $cont);
    		
    		//pr($proxy_array);
    		$count = count($proxy_array);
    		if($count < 1){return array("", "GRAB_ERROR: not finding proxyes in [".$proxy."]
    ");}
    		for($i=$start_from;$i!=$count;$i++){
    		
    			$p=trim($proxy_array[$i]);
    			
    			if(strlen($p) < 7)	continue;
    			
    			
    			curl_setopt($ch, CURLOPT_PROXY, $p);
    			$response = curl_exec($ch);
    			$error = curl_error($ch);
    			curl_close($ch);
    			
    			if($error){
    				// Get recursive
    				$t_error = "GRAB_ERROR: cURL says [".curl_error($ch)."], seems $p is dead,".($delete_dead?' delete it and':'')." get next
    ";
    				echo $t_error;
    				if($get_next_if_error){
    					$recall_err[] = $t_error;
    						if($delete_dead){
    							$this->delete_proxy($proxy, $p);
    						}
    					
    					return $this->proxy_get($url, $proxy, $stopwords, $minlen, $delete_dead, $delete_banned, $get_next_if_stopword, $get_next_if_error, $display_url_in_errors, $display_content_in_errors, $method, $postfields, $timeout, $header, $referer, $useragent, $recall_err, ($i+2));
    				}
    				return array('res'=>$response, 'errors'=>$t_error, 'start'=>$i);
    			}
    
    			if(count($stopwords)){
    				foreach ($stopwords as $word){
    					if(strpos($response,$word)){
    						$t_error = "GRAB_ERROR: match stopword [".$word."] seems $p is banned ".($delete_banned?' delete it and':'')." get next 
    ".$err_url.$err_content;
    						echo $t_error;
    						if($get_next_if_stopword){
    							$recall_err[] = $t_error;
    								if($delete_banned){
    									$this->delete_proxy($proxy, $p);
    								}
    					return $this->proxy_get($url, $proxy, $stopwords, $minlen, $delete_dead, $delete_banned, $get_next_if_stopword, $get_next_if_error, $display_url_in_errors, $display_content_in_errors, $method, $postfields, $timeout, $header, $referer, $useragent, $recall_err, ($i+2));
    						}
    						return array('res'=>$response, 'errors'=>$t_error, 'start'=>$i);
    						
    					}
    					
    				}
    			}
    				
    				if($minlen && ($minlen > strlen($response))){return array('res'=>$response, 'errors'=>"GRAB_ERROR: small lenght [".round((strlen($response)/1000), 2)." Kb] ".$err_url.$err_content."
    ", 'start'=>$i);}
    				
    				return array('res'=>$response, 'errors'=>$t_error, 'start'=>$i);
    		}
    		return array('res'=>$response, 'errors'=>$t_error, 'start'=>$i);
    	}
    	
    	function delete_proxy($proxy, $p){
    		
    		$fp = fopen($proxy, 'r+');
    		$cont = fread($fp, filesize($proxy));
    		$cont = str_replace($p, '');
    		fwrite($fp, $cont);
    		fclose($fp);
    	}
    } ?>
    
    PHP:
     
    Vio82, Dec 23, 2007 IP
  3. Vio82

    Vio82 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There is an exemple:
    
    <?php
    
    require_once 'class.Grab.php';
    $Grab = new Grab();
    $res = $Grab->proxy_get(
    				$url, // url requested page
    				'proxy.txt', // full path to proxy file OR proxy IP like '127.0.0.1:81'
    				array('bad word 1', 'bad word 2', 'Error 403'), // stop words means that proxy is baned
    				'100', // mimimum leight of content getted
    				'0',  // delete dead proxy from list 1-yes 0-no
    				'0',  // delete banned proxy from list 1-yes 0-no
                                    '0',  // Get next proxy if stopword matching
    				'0',  // Get next proxy if curl error
    				'1',  // display url in errors 1-yes 0-no
    				'1',  // display grabbed content in errors 1-yes 0-no
    				'0',  // 0 - GET, 1 - POST
    				'',   // post fields (var1=1&var2=2 etc.)
    				'30',// curl timeout
    				'1', // return HTTP header or not 1-yes 0-no
    				'',   // referer
    				'',   // useragent
    				'',   // recall_err - system var, use to tranfer errors, do not enter any value
    				''   // start_from - system var, use as count, do not enter any value
    				);
    echo $res;
    ?>
    
    PHP:
     
    Vio82, Dec 23, 2007 IP