this script gets the position of your site at google.
“google position” makes use of the built-in PHP functions
strtr( ),
fopen( ),
feof( ),
fgets( ),
eregi( ),
end( ),
array_reverse( ),
explode( ),
strtolower( ),
strip_tags( ),
strpos( ),
substr( ),
strcmp( ) and
fclose( ).
001 <?php
002
003 if(!empty($_POST['searchquery']) && !empty($_POST['searchurl']))
004 {
005 $querycleaner = array(' '=>'+','%26'=>'&');
006 $query = strtr($_POST['searchquery'],$querycleaner);
007
008 // total entries to search
009 $total_to_search = 100;
010
011 // hits per page to search through
012 $hits_per_page = 10;
013
014 $position = 0;
015 $real_position = 0;
016
017 $found = NULL;
018 $lastURL = NULL;
019
020 $despacer = array(' '=>'');
021 for($i = 0; $i < $total_to_search && empty($found); $i += $hits_per_page)
022 {
023 $filename = 'http://www.google.com/search?as_q='.$query.
024 '&num='.$hits_per_page.'&hl=en&ie=UTF-8&btnG=Google+Search'.
025 '&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype='.
026 '&as_qdr=all&as_nlo=&as_nhi=&as_occt=any&as_dt=i'.
027 '&as_sitesearch=&safe=images&start='.$i;
028
029 $file = fopen($filename, 'r');
030 if(!$file)
031 {
032 echo '<p>Unable to open remote file '.$filename;
033 }else{
034 while(!feof($file))
035 {
036 $var = fgets($file, 1024);
037 if(eregi('<b>1</b> - <b>10</b> of about <b>(.*)</b>',$var,$outs))
038 {
039 $total = end(array_reverse(explode(' ',$outs[1])));
040 }
041 if(eregi('<font color=#008000>(.*)</font><nobr>',$var,$out))
042 {
043 $out[1] = strtolower(strip_tags($out[1]));
044 $x = strpos($out[1],'/');
045 $url = substr($out[1],0,$x);
046 $position++;
047 if(strcmp($lastURL,$url)<>0) $real_position++;
048 $lastURL = $url;
049 if(strcmp($_POST['searchurl'],$url)==0)
050 {
051 $urlparts = explode(' - ',$out[1]);
052 $foundurl = strtr($urlparts[0],$despacer);
053 $found = $position;
054 break;
055 }
056 }
057 }
058 }
059 fclose($file);
060 }
061
062 if($found)
063 {
064 $result = '
065 <p>
066 The site <a href="http://'.$foundurl.'">'.$_POST['searchurl'].'</a>
067 is at position <b>'.$found.'</b> ( '.$real_position.' ) of about <b>'.$total.'
068 for the term <b>'.$_POST['searchquery'].'</b>
069 </p>';
070 }else{
071 $result = '
072 <p>
073 The site '.$_POST['searchurl'].' is not in the
074 top '.$total_to_search.' for the term <b>'.$_POST['searchquery'].'</b>
075 </p>';
076 }
077 }
078
079 echo '
080 <form method="post" action="./">'.(isset($result) ? '
081 <fieldset>
082 <legend>result</legend>
083 '.$result.'
084 </fieldset>' : '').'
085 <fieldset>
086 <legend>search term</legend>
087 <input name="searchquery" type="text"'.(isset($_POST['searchquery']) ?
088 ' value="'.$_POST['searchquery'].'"' : '').' />
089 </fieldset>
090 <fieldset>
091 <legend>domain (without http:// and subdirectories)</legend>
092 <input name="searchurl" type="text"'.(isset($_POST['searchurl']) ?
093 ' value="'.$_POST['searchurl'].'"' : '').' />
094 </fieldset>
095 <fieldset>
096 <legend>get position</legend>
097 <input type="submit" value="get position" />
098 </fieldset>
099 </form>';
100
101 ?>