Location: PHPKode > scripts > Php Link Popularity Checker > php-link-popularity-checker/scripts/linkpopularity.class.php
<?php

class LinkPOP {
	var $debug;
	var $total;
	var $results;
	var $allowed_engines;
	var $engine;
	var $domain;

	function LinkPOP($allowed_engines = NULL, $debug = NULL) {
		if (isset($debug) && $debug == TRUE) {
			$this->debug = TRUE;
		}
		if (isset($allowed_engines) && is_array($allowed_engines)) {
			$this->allowed_engines = $allowed_engines;
		} else {
			$this->allowed_engines = array('google','yahoo','alltheweb', 'altavista', 'bing', 'alexa', 'dmoz', 'ask');
		}
		return true;
	}

	function GetData($domain) {
		if (!$this->validateURL($domain)) {
			$this->results[$engine]['value'] = "";
			$this->results[$engine]['url'] = "invalid url";
			$this->results[$engine]['name'] = "invalid url";
			return false;
		} else {
			$this->domain = urlencode($domain);
			$this->results['total'] = 0;
			$this->results = NULL;
			foreach ($this->allowed_engines as $engines) {
				switch($engines) {
					case 'google':
					    $this->Google();
						break;
					case 'yahoo':
					    $this->Yahoo();
					    break;
					case 'alltheweb':
					    $this->Alltheweb();
				    	break;
					case 'altavista':
					    $this->Altavista();
					    break;
					case 'bing':
					    $this->Bing();
				    	break;
					case 'ask':
					    $this->Ask();
				    	break;
					case 'alexa':
					    $this->Alexa();
				    	break;
					case 'dmoz':
					    $this->Dmoz();
					    break;
				}
			}
			return true;
		}
	}

	function validateURL($URL) {
   		$domain = "([[:alpha:]][-[:alnum:]]*[[:alnum:]])(\.[[:alpha:]][-[:alnum:]]*[[:alnum:]])+";
   		$dir = "(/[[:alpha:]][-[:alnum:]]*[[:alnum:]])*";
   		$page = "(/[[:alpha:]][-[:alnum:]]*\.[[:alpha:]]{3,5})?";
   		$getstring = "(\?([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+)(&([[:alnum:]][-_%[:alnum:]]*=[-_%[:alnum:]]+))*)?";
   		$pattern = "^".$domain.$dir.$page.$getstring."$";
   		return eregi($pattern, $URL);
	}

	function RetrieveData($descr, $engine) {
		if (!file_exists($this->engine['url'])) {
			if (!isset($this->debug)) {
				error_reporting(0);
			}
			$data = strip_tags(strtolower(file_get_contents($this->engine['url'], TRUE)));
			if (isset($this->debug) && $this->debug == TRUE) {
				echo $data;
			} else {
				error_reporting(E_ALL ^ E_NOTICE);
			}
			$data = preg_match($this->engine['regex'], $data, $match);
			if (isset($this->debug) && $this->debug == TRUE) {
				echo "<pre>"; print_r($match); echo "</pre>";
			}
			$match[$this->engine['match']] = str_replace('.', ',', $match[$this->engine['match']]);
			if (is_numeric(str_replace(',','',trim($match[$this->engine['match']]))) && $this->engine['return'] == 'total') {
				$this->total += str_replace(',','',trim($match[$this->engine['match']]));
				$this->results[$engine]['value'] = str_replace(',','',trim($match[$this->engine['match']]));
				$this->results[$engine]['url'] = $this->engine['url'];
				$this->results[$engine]['name'] = $descr;
				return true;
			} else if (is_numeric(str_replace(',','',trim($match[$this->engine['match']]))) && $this->engine['return'] == 'number') {
				$this->results[$engine]['value'] = str_replace(',','',trim($match[$this->engine['match']]));
				$this->results[$engine]['url'] = $this->engine['url'];
				$this->results[$engine]['name'] = $descr;
				return true;
			} else if ($match[$this->engine['match']] && $this->engine['return'] == 'yesno') {
				if (isset($this->engine['negative']) && $this->engine['negative'] == TRUE) {
					$this->results[$engine]['value'] = 'NO';
				} else {
					$this->results[$engine]['value'] = 'YES';
				}
    			$this->results[$engine]['name'] = $descr;
				$this->results[$engine]['url'] = $this->engine['url'];
				return true;
			} else if (!$match[$this->engine['match']] && $this->engine['return'] == 'yesno') {
				if (isset($this->engine['negative']) && $this->engine['negative'] == TRUE) {
					$this->results[$engine]['value'] = 'YES';
				} else {
					$this->results[$engine]['value'] = 'NO';
				}
				$this->results[$engine]['url'] = $this->engine['url'];
				$this->results[$engine]['name'] = $descr;
				return true;
			} else if ($this->engine['return'] == 'total') {
				$this->results[$engine]['url'] = $this->engine['url'];
				$this->results[$engine]['value'] = 'N/A';
				$this->results[$engine]['name'] = $descr;
				return false;
			} else {
				$this->results[$engine]['url'] = $this->engine['url'];
				$this->results[$engine]['value'] = 'N/A';
				$this->results[$engine]['name'] = $descr;
				return false;
			}
		} else {
			$this->results[$engine]['url'] = $this->engine['url'];
			$this->results[$engine]['value'] = 'N/A';
			$this->results[$engine]['name'] = $descr;
			return false;
		}
	}

	function Google()  {

		$this->engine['url'] = "http://www.google.com/search?as_lq=".$this->domain;
		$this->engine['regex'] = "/of about ([0-9,]{1,13}) linking/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('Google', 'google');
	}

	function Bing()  {
		$this->engine['url'] = "http://www.bing.com/search?q=link%3Ahttp%3A%2F%2F".$this->domain."&FORM=QBNO";
		$this->engine['regex'] = "/results[0-9]{1,2}-[0-9]{1,3} of ([0-9,\.]{1,13})/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('Bing','bing');
	}

	function Yahoo()  {
		$this->engine['url'] = "http://search.yahoo.com/search?p=linkdomain%3A".$this->domain."&ei=UTF-8&fr=FP-tab-web-t&fl=0&x=wrt";
		$this->engine['regex'] = "/inlinks \(([0-9,]{1,13})\)/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('Yahoo!','yahoo');
	}

	function Alltheweb()  {
		$this->engine['url'] = "http://www.alltheweb.com/search?cat=web&cs=utf8&q=link%3A".$this->domain."&rys=0&_sb_lang=pref";
		$this->engine['regex'] = "/[0-9]{1,2} - [0-9]{1,3} of ([0-9,]{1,13})\nresults/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('AlltheWeb','alltheweb');
	}

	function Altavista()  {
		$this->engine['url'] = "http://www.altavista.com/web/results?itag=wrx&q=link%3A".$this->domain."&kgs=0&kls=0";
		$this->engine['regex'] = "/altavista found ([0-9,]{1,13}) results/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('AltaVista','altavista');
	}

	function Ask()  {
		$this->engine['url'] = "http://www.ask.com/web?o=10182&q=links%3A".$this->domain."&qcat=1&qsrc=1";
		$this->engine['regex'] = "/of ([0-9,]{1,13}) for/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('Ask','ask');
	}

	function Alexa()  {
		$this->engine['url'] = "http://www.alexa.com/siteinfo/http://".$this->domain;
		$this->engine['regex'] = "/3 month avg([0-9,]{1,13})/";
		$this->engine['match'] = 1;
		$this->engine['return'] = 'total';
		$this->RetrieveData('Alexa Traffic<br>Ranking','alexa');
	}

	function Dmoz()  {
		$this->engine['url'] = "http://search.dmoz.org/cgi-bin/search?search=".str_replace('www.', '', $this->domain);
		$this->engine['regex'] = "/no open directory project results found/";
		$this->engine['negative'] = TRUE;
		$this->engine['match'] = 0;
		$this->engine['return'] = 'yesno';
		$this->RetrieveData('Found in<br>DMOZ','dmoz');
	}


}
?>
Return current item: Php Link Popularity Checker