john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

grank topten php

<html>

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
    $query = "http://google.com/search?q=" . strtr($_POST['query'], " ", "+") ;
    $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[10]);  //cut off data past result 10
    $result_array[10] = $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 . "<br \>" . $result_array[$i] . "<br \>\n";
        }
    }


    echo "\n<br \>&nbsp;<br \>\n";

}

?>
</html>

  • « php file listing header
  • good websites »

Published

Feb 6, 2010

Category

php

~200 words

Tags

  • grank 3
  • php 82
  • topten 1