Need little help converting fopen to curl please

Discussion in 'PHP' started by pilotcap232, May 2, 2008.

  1. #1
    Hello, my current host has disabled fopen for security purposes, I have a search escript which retrieves outside server results, wich now is not working anymore, I will appreciate if someone can help me convert this file or point me to the right direction to convert it to curl instead of fopen which is currently is.

    Thank you in advanced, any help will be much appreciated.

    
    
    
    <?
    	//DMOZ
    	$DELAY = 5;
    	$DMOZ_URL = "http://search.dmoz.org/cgi-bin/search?";
    	$TRY_CONNECTIONS = 20;
    	$SEARCHFEED_URL = "http://www.searchfeed.com/rd/feed/XMLFeed.jsp?";
    	$RAGING_URL = "http://www.altavista.com/web/res_text?avkw=xytx&amb=txt&kls=0&kgs=0";
    	//microseconds
    	$TRY_PAUSE = 5;
    	
    	function Pause($delay = 10){
    		$ts = explode(" ", microtime());
    		$start_t = $ts[0] + $ts[1]*1000;
    		do{
    			$ts = explode(" ", microtime());
    			$c_t = $ts[0] + $ts[1]*1000;
    		}while(($c_t - $start_t)<=$delay);
    	}	
    	
    	
    	function GetDMOZResults(&$results, $ss, $start = 1, $num = 1){
    		global $DMOZ_URL;
    		global $TRY_CONNECTIONS, $TRY_PAUSE;
    		$results = array(
    								"0" => array(
    								"title"=>"",
    								"link"=>"",
    								"summary"=>"",
    								"category"=>""
    							 )
    		);
    		$link = $DMOZ_URL."search=".urlencode($ss)."&start=".$start;
                    
    		$cc = 1;
    		while(!($fh = @fopen($link, 'r'))){
    			if($cc==$TRY_CONNECTIONS) break;
    			$cc++;
    			Pause($TRY_PAUSE);
    		}
    		if($fh)
    		{
    			$buff = "";
    			$tmp = "";
    			$tmp_arr = array();
    			while($buff = fread($fh, 256)){
    				$tmp.=$buff;
    			}
    			fclose($fh);
                           	$sc = 0;
    			if(preg_match("/Open Directory Sites.*?(\d+?)\)/", $tmp, $m1)){
    				$sc = $m1[1];
                            }        
    			$tmp = preg_replace("/^.*Open Directory Sites/", "", $tmp);
    			$tmp = preg_replace("/<img.*?>/", "", $tmp);
    			$count = 1;
    			if(preg_match_all("/<li>(<a href=\"http:.*?)(?:<li>|<\/ol>)/s", $tmp, $matches)){
    				foreach($matches[1] as $st){
    					if($count<=$num&&$count<=$sc){
                                            if(preg_match("/^<a href=\"(.*?)\">(.*?)<\/a>(.*?)<br>.*?<a href=\".*?\">(.*?)<\/a>/s", $st, $m2)){
    							$linkurl = $m2[1];
    							$title = $m2[2];
    							$summary = $m2[3];
    							$category = $m2[4];
    						}
    //						print $count." - ".$title."<hr>";
    						$results[$count] = array(
    							"title"=>$title,
    							"link"=>$linkurl,
    							"summary"=>$summary,
    							"category"=>$category
    						);
    						$count++;
    					}else{
    						break;
    					}
    				}
    				return $sc;
    			}else return "";
    		}else{
    			return "";
    		}
    	}
    	
    	
    	function PPCParseRecord_External($type, $record, $counter, $page, $rpp, $hs, $ss, $rest){
    		global $PPC_RESULTS_PART, $CLICK_SCRIPT, $CLICK_TARGET, $MAIN_URL;
    		
    			$tmp = ReadTemplate($PPC_RESULTS_PART);
    			$shl = "";
    			if($rest=="dmoz"||$rest=="msn"){
    				if(preg_match("/(http:\/\/(?:[-\w]\.?)+\/??)/", $record["link"], $mm)){
    					$shl = $mm[1];
    					$cat = $record["category"];
    				}
    			}elseif($rest=="revenue"){
    				$shl = $record["category"];
    				$cat = "n/a";
    			}
    			
    			$vars = array(
    					"logo"=>"",
    					"counter"=>$counter,
    					"id"=>"",
    					"title"=>"<a href=\"".$record["link"]."\" target=\"_blank\">".$record["title"]."</a>",
    					"summary"=>($hs==0?stripslashes($record["summary"]):"").($record["bid"]!=""?"<br><i>Bid: ".$record["bid"]."</i>":""),
    					"link"=>$shl,
    					"category"=>$cat
    			);
    			return ParseTemplate($tmp, $vars);
    			
    	}
    	
    //END OF DMOZ
    
     
    
     
    
    //SEARCHFEED XML FEEDS
    	function GetSearchFeedResults(&$results, $ss, $start = 1, $num = 1){
    		global $SEARCHFEED_URL, $TRY_CONNECTIONS, $TRY_PAUSE;
    		global $SEARCHFEED_CID;
    		$results = array(
    								"0" => array(
    								"title"=>"",
    								"link"=>"",
    								"summary"=>"",
    								"category"=>"",
    								"bid"=>""
    							 )
    		);
    		//$SEARCHFEED_CID = GetSettings("searchfeed_id");
    		$link = $SEARCHFEED_URL."cat=".urlencode($ss)."&pID=".$SEARCHFEED_CID."&IP=".getenv("REMOTE_ADDR")."&page=".($start)."&nl=".$num;
    		$cc = 1;
    		while(!($fh = @fopen($link, 'r'))){
    			if($cc==$TRY_CONNECTIONS) break;
    			$cc++;
    			Pause($TRY_PAUSE);
    		}
    		if($fh)
    		{
    			$buff = "";
    			$tmp = "";
    			$tmp_arr = array();
    			while($buff = fread($fh, 256)){
    				$tmp.=$buff;
    			}
    			fclose($fh);
    /*
    							print "SF working...<hr>";
    							$ft = fopen("temp2.txt","a");
    							fwrite($ft, "\n\n$link\n\n");
    							fwrite($ft, $tmp);
    							fclose($ft);
    */
    			$sc = $num;
    			$count = 1;
    			if(preg_match("/<Listings>(.*)<\/Listings>/is", $tmp, $matches)){
    				$recs = $matches[1];
    					if(preg_match("/<Count>(.*?)<\/Count>/is", $recs, $m1)){
    						$sc = $m1[1];
    					}
    					if(preg_match_all("/<Listing>(.*?)<\/Listing>/is", $recs, $m1)){
    						foreach($m1[1] as $tt){
    							if(preg_match("/<URI>\s*(.*)\s*<\/URI>/is", $tt, $m2)){
    								$linkurl = $m2[1];
    							}
    							if(preg_match("/<URL>\s*(.*)\s*<\/URL>/is", $tt, $m2)){
    								$category = $m2[1];
    							}
    							if(preg_match("/<Title>\s*<!\[CDATA\[(.*)\]\]>\s*<\/Title>/is", $tt, $m2)){
    								$title = $m2[1];
    							}
    							if(preg_match("/<Description>\s*<!\[CDATA\[(.*)\]\]>\s*<\/Description>/is", $tt, $m2)){
    								$summary = $m2[1];
    							}
    							if(preg_match("/<Bid>\s*(.*)\s*<\/Bid>/is", $tt, $m2)){
    								$bid = $m2[1];
    							}
    /*
    							$ft = fopen("temp.txt","a");
    							fwrite($ft, "$title\n$summary\n$linkurl\n$category\n$bid\n---------------------------\n");
    							fclose($ft);
    */
    							$results[$count] = array(
    								"title"=>$title,
    								"link"=>$linkurl,
    								"summary"=>$summary,
    								"category"=>$category,
    								"bid"=>$bid
    							);
    							$count++;
    						}
    					}
    				return $sc;
    			}else return "";
    		}else{
    			return "";
    		}
    	}
    //END OF SEARCHFEEDS XML FEEDS
    
    //RAGING FEEDS
    
    	function GetRAGINGResults(&$results, $ss, $start = 1, $num = 1){
    		global $RAGING_URL;
    		global $TRY_CONNECTIONS, $TRY_PAUSE;
    		$results = array(
    								"0" => array(
    								"title"=>"",
    								"link"=>"",
    								"summary"=>"",
    								"category"=>""
    							 )
    		);
          //		$link = $RAGING_URL."&q=".urlencode($ss)."&stq=".$start;
     		$link = "http://www.altavista.com/web/results?q=".urlencode($ss)."&stq=".$start;
     		
    		$cc = 1;
    		while(!($fh = @fopen($link, 'r'))){
    			if($cc==$TRY_CONNECTIONS) break;
    			$cc++;
    			Pause($TRY_PAUSE);
    		}
    		if($fh){
    			$buff = "";
    			$tmp = "";
    			$tmp_arr = array();
    			while($buff = fread($fh, 256)){
    				$tmp.=$buff;
    			}
    			fclose($fh);
    		}
    		$sc = 0;
    /*
    		if(preg_match("/found ([\d\,]+) results\s+<\/a>/i", $tmp, $m1)){
    			$sc = $m1[1];
    		}
    */
    //new		
    			if(preg_match("/found (.*?) results/i", $tmp, $m1)){
    			$sc = $m1[1];
    			$sc = preg_replace("/,/","",$sc);
                     }
    //new                 
    		$str_pos = strpos($tmp,"found $sc results") + 15; 
    		$tmp = substr($tmp,$str_pos);
     		
    		
    //		if (preg_match_all("/<a\n*\s+href\s*=\"\/r\?ck_sm=([^\"]*)[^>]*>([^<]*)<\/a><br><\/b><span class=s>(.*)<br>/",$tmp,$array))
    		if (preg_match_all("/<br class='lb'><a class='res' href=\'(.*?)\'>(.*?)<\/a><br><span class=s>(.*?)<br>/si",$tmp,$array))
    		{
    			$i=0;
    			while($i<count($array[1]))
    			{
     //				$linkurl="http://www.altavista.com/r?ck_sm=".$array[1][$i];
    				$linkurl=$array[1][$i];
    	             		$title=$array[2][$i]; 
    				$summary=$array[3][$i];
    				$category = "n/a"; 
    				$results[$i+1] = array(
    					"title"=>$title,
    					"link"=>$linkurl,
    					"summary"=>$summary,
    					"category"=>$category
    				);
    				$i++;  
    /* echo "$title <br>"; 
    echo "$linkurl <br>"; 
    echo "$summary <br>"; 
    */				
    			}
    		}
    		return $sc;
    	}
    
    ?>
    
    
    
    
    
    Code (markup):
     
    pilotcap232, May 2, 2008 IP