john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

grank top100

<html>
<body>
Trying to find what rank a website has for certain keywords in google (out of the top 100 results).
<br \>&nbsp;<br \>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  query (spaces between keywords):&nbsp;<input type="text" name="query" size="64" maxlength="256" />
  <br \>&nbsp;<br \>
  target url:&nbsp;<input type="text" name="target_url"  maxlength="256" />
 <br \>&nbsp;<br \>
  <input type="Submit" value="Submit" />
</form>

<?php       //updated 2009-08-16-john-pfeiffer

// *********************************************************************
/* list search engine rankings for certain queries */

if( !isset($_POST['query']) || empty($_POST['query'] ) ||
    !isset($_POST['target_url']) || empty($_POST['target_url'] )    )
{
    echo "<b>You must fill in both the query and target url.</b>";
}
else
{                              //convert user's query spaces to +'s and get top 100 results
    $query = "http://google.com/search?q=" . strtr($_POST['query'], " ", "+") . "&num=100";

    $target_url = strtolower($_POST['target_url']);

    $result = file_get_contents($query);         //get the resulting page from google

    echo "<br \>\n";
    echo "Searching for <i>" . $target_url . "</i> in " . strlen($result) . " characters downloaded for query: " . $query;


    $result_array = explode("<h3 class=r>", $result);   //break string into results

    $subarray = explode("</cite>", $result_array[100]);  //cut off data past result 100
    $result_array[100] = $subarray[0];                   //replace with ONLY result 10 data

    unset($result_array[0]);                        //remove google adwords results (arr[0])
    $result_array = array_values($result_array);    //re-index and shift items down


    echo "<br \>\n";
    echo "array size: " . sizeof($result_array);
    echo "\n<br \>&nbsp;<br \>\n";

    for( $i=0; $i< sizeof($result_array); $i++)
    {
        $pos = strpos( $result_array[$i], $target_url);

        if( $pos === false )
        {}
        else
        {   echo "RANKING: " . ($i+1) . "<br \>&nbsp;<br \>\n" . $result_array[$i] . "<br \>\n";
        }
    }

    echo "\n<br \>&nbsp;<br \>\n";
    //echo $result;         //prints out the whole string as downloaded from google
} //end if-else query and target url filled in

?>
<br \>
<?php //include date modified
include( $_SERVER["DOCUMENT_ROOT"] . "/includes/foot.txt");
?>

<?php //include the google analytics javascript
    include( $_SERVER["DOCUMENT_ROOT"] . "/includes/google-code.htm");
?>

</body>
</html>

  • « css mouseover continued source
  • grank »

Published

May 12, 2010

Category

php

~248 words

Tags

  • grank 3
  • php 82
  • top100 1