<?phperror_reporting(E_ALL);/* To be implemented: - sort google results into an array - randomise proxies from proxy list - captcha protection */// open the google classclass google { // a function very kindly "borrowed" from php.net/ereg to get the content // from between the two div markers function returnSubStrings($text, $openingMarker, $closingMarker) { $openingMarkerLength = strlen($openingMarker); $closingMarkerLength = strlen($closingMarker); $result = array(); $position = 0; while (($position = strpos($text, $openingMarker, $position)) !== false) { $position += $openingMarkerLength; if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) { $result[] = substr($text, $position, $closingMarkerPosition - $position); $position = $closingMarkerPosition + $closingMarkerLength; } } } // get content from google's servers function get_url($url, $proxy_ip=null, $proxy_port=null){ // initialise curl in the handle $ch $ch = curl_init(); // give it our google URL curl_setopt($ch, CURLOPT_URL, $url); // set the user agent so google thinks we're a visitor $browser = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.4)"; curl_setopt($ch, CURLOPT_USERAGENT, $browser); // make sure to remove the curl header curl_setopt($ch, CURLOPT_HEADER, 0); // we want it to return our data, not print it curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // make sure we timeout after a set amount of time curl_setopt($ch, CURLOPT_TIMEOUT, 15); // lets see if we have a proxy if(!empty($proxy_ip) && !empty($proxy_port)) { // set CURLOPT_PROXY to the proxy IP with the proxy port curl_setopt($ch, CURLOPT_PROXY, $proxy_ip.":".$proxy_port); } // get the content of the URL $output = curl_exec($ch); // close our $ch curl handle curl_close($ch); // return the output back to the previous function return $output; } // scrape a result set for our user function results($string, $amount=20){ // the goog limits search strings to 32 words, so lets just allow 31 spaces if(substr_count($string, " ") > 31) { die("Search string too long."); } // lets not be too cruel, stop them at 100 results if($amount > 100) { $amount = 100; } // create our search URL, first the server in http:// $url = 'http://www.google.com/'; // now start the query with the amount $url .= 'search?num='.$amount.'&'; // now lets add the search string (url-encoded) $url .= 'q=%22'.urlencode($string).'%22&'; // and finally google's identifier $url .= 'btnG=Search'; // echo it for testing echo $url."<br><br><br><br><br>"; // use our get_url function to grab the content $content = $this->get_url($url); /* make sure we reformat it (14/05), trademark liabilities */ // and now lets echo the content we just scraped return $content; } function parse_results($results) { $opener = "<div class=g"; // untested closer.. may cause cut off results (TEST EXTENSIVELY) $closer = "</font></td></tr></table></div>"; // get what is inside of each $opener & $closer $doneres = $this->returnSubStrings($results, $opener, $closer); // $doneres should be reformatted into a decent array now with some ereg stuff // currently the content is just as a "blob" return $doneres; }}/* sample code START */// create a new instantiation of the google class$google = new google;// use the results function to get our content (this will just echo out the contents of the array)$results = $google->results("gcse", 5);//echo $results."<br><br><br><br><br>";$parsed = $google->parse_results($results);print_r($parsed)."<br><br><br><br><br>";foreach($parsed as $parse) { if(stristr("bard id=gbardd", $parse)) { $parse = returnSubStrings($parse, "<div class=g", "</font></td></tr></table></div>"); } echo $parse;}/* sample code END */?>