Oto kod
Kod
<form method="get" action="google.php" style="text-align:center;">
<input type="text" maxlength="100" value="<? echo $_GET['sQ'];?>" name="sQ" class="contactForm1">
<input type="text" maxlength="100" value="<? echo $_GET['sSite'];?>" name="sSite" class="contactForm1">
<input type="submit" value="szukaj"/>
</form>
<?php
$pozycja = new checkPosition( $sQ, $sSite );
//$sQ = $_Get["sQ"];
//$sSite = $_Get["sSite"];
//$pozycja = new checkPosition( 'portal', 'http://www.onet.pl' );
//$pozycja = new checkPosition( $sQ, $sSite );
/*
@author Piotr `shpyo` Cichosz
http://blog.shpyo.net
*/
class checkPosition
{
public $sLinkRegExp = '/<h3 class="r"><a href="([^"]+)" class=l>/';
public $sURL = '';
public $iLimit = 100;
public static $aDC = array(
'www.google.pl'
);
private $sQ = '';
private $sSite = '';
public $iPosition = '-';
public function __construct( $sPhrase, $sSite )
{
$this->sQ = $sPhrase;
$this->sSite = self::getHost( $sSite );
}
public function getHost( $sPage )
{
preg_match('@^(?:http://)?([^/]+)@i', $sPage, $matches);
$matches[1] = str_replace("www.", "", $matches[1]);
return $matches[1];
}
public function getPhrase()
{
return $this->sQ;
}
public function getSite()
{
return $this->sSite;
}
public function setGoogleURL( $rDC = 'www.google.pl' )
{
$this->sURL = 'http://'. $rDC .'/search?hl=pl&q=%s&num='. $this->iLimit;
}
public function getDC()
{
return self::$aDC[ mt_rand( 0, count(self::$aDC) - 1 ) ];
}
public function search( $rDC = 'www.google.pl' )
{
$rC = curl_init();
self::setGoogleURL( $rDC );
curl_setopt( $rC, CURLOPT_HEADER, 0 );
curl_setopt( $rC, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $rC, CURLOPT_VERBOSE, 1 );
curl_setopt( $rC, CURLOPT_REFERER, $rDC );
curl_setopt( $rC, CURLOPT_URL, sprintf( $this->sURL, urlencode( self::getPhrase() ) ) );
$sData = curl_exec( $rC );
curl_close( $rC );
preg_match_all( $this->sLinkRegExp, $sData, $aResults );
$aResults = array_pop( $aResults );
$ii = 0;
for( $i=0; $i<100; $i++ )
{
$sPage = self::getHost($aResults[$i]);
if( $aResults[($i+1)] )
{
$sNext = self::getHost($aResults[($i+1)]);
if( $sDomena != $sNext )
{
$ii++;
if( $sPage == $this->sSite )
{
$this->iPosition = $ii;
break;
}
}
$sDomena = $sNext;
}
}
}
}
$pozycja->search();
echo $pozycja->iPosition;
?>