PHP – Class Google Scraper

This PHP class can retrieve search results from Google.

It can send an HTTP request to Google search site Web server to perform a search for given keywords. 

The request can be sent using an HTTP proxy.

The class can parse the retrieved results page and extract the URLs of the result links.

Code Example: Download Example
include 'google.scraper.class.php';
$obj=new GoogleScraper();
echo "<pre>";


class GoogleScraper{
	var $keyword	=	"phpmoot";
	var $urlList	=	"";
	var $time1		=	4000000;
	var $time2		=	8000000;
	var $proxy		=	"";
	var $cookie		=	"";
	var $header		=	"";
	var $ei			=	"";

	function __construct() {
		$this->cookie = tempnam ("/tmp", "cookie");
		$this->headers[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
		$this->headers[] = "Connection: keep-alive"; 
		$this->headers[] = "Keep-Alive: 115"; 
		$this->headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
		$this->headers[] = "Accept-Language: en-us,en;q=0.5"; 
		$this->headers[] = "Pragma: "; 

	function getpagedata($url)
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
		curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1');
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); 
		curl_setopt($ch, CURLOPT_COOKIEFILE,  $this->cookie); 
		curl_setopt($ch, CURLOPT_COOKIEJAR,  $this->cookie);
		curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		return $data;

	function pause() {

	function initGoogle() {
		$data=$this->getpagedata('');		//	Open ( Might redirect to country specific site e.g.
		$this->getpagedata('');		//	Moves back to

	// This function opens the preference page and saves the count for "Results per page" to 100
	function setPreference() {
		preg_match('/<input type="hidden" name="sig" value="(.*?)">/', $data, $matches);

	function fetchUrlList()
		for($i=0;$i<201;$i=$i+100) {
			preg_match('/ei=(.*?)&amp/', $data, $matches);
			if ($data) {
				if(preg_match("/", $data)) {
					echo "You are blocked";
				} else {
					preg_match_all('@<h3\s*class="r">\s*<a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>@siU', $data, $matches);
					for ($j = 0; $j < count($matches[2]); $j++) {
						$this->urlList[] = $matches[1][$j];
				echo "Problem fetching the data";

	function getUrlList($keyword,$proxy) {
		return $this->urlList;

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *