google serp

Free SERP Checker PHP Script Source Code

SERP Checker PHP Script

I’ve made this SERP checker PHP script that shows the position or Google rank of a page for a certain keyword search. It also displays a preview of the how a certain page looks search engine results page. Though SERP is a generic term referring to all search engine results, I will only focus on Google.

This is a keyword ranking checker PHP script that allows checking the Google ranking of a page for a particular keyword online, but this presents several challenges because Google restricts scrapping info from their pages. There is a work around by using their API.

 

You can download the source code for this PHP script for free. The SERP Checker PHP script is free to use for personal and commercial use with attribution. Please include a link to http://www.coding-dude.com

Using this Google keyword position checker PHP script you will be able to retrieve the current Google rank of a page for a specific keyword in the language or country you want.

Before gettting to the source code, let’s clear up some terms and lay out the basics for my SERP checker script:

What Does SERP Mean?

SERP or Search Engine Result Page refers to the way the search engine displays search results. Among SEO professionals SERP most often refers to how will a page show up in the results and the page ranking in the search results.

Can I Do Manual SERP Checking?

manual searching

Yes, but it’s very tedious.

Imagine the following:

I’ve recently published a post about a SEO experiment I’m running. With this experiment I’m trying to get that page to rank in Google for some keyword searches. I know the keywords I want to rank for, so to monitor the results for the experiment I want to know how high (or low) my page is in Google search results.

How do I do that?

I can manually search for “WordPress SEO experiment 2018” in Google and I can see that my page is number 1 in the results. That’s easy, but what if I want to check my page rank on Google for “WordPress SEO experiment”. I’m not on the first page. Turning to page 2 of the search results I can see I’m on first position of that second page.

But what if I’m lower, how will I know?

Google Webmaster Tools has Search Analytics that show you your pages ranking position for certain keywords. But, there’s a delay of about 4 days in the data collection. Also, you can only do that for your own site, and not for the competition.

How About Automated SERP Checking?

There are a lot of tools out there that claim to do SERP checking. And they do it. However, most of them are commercial tools.

How does automated SERP checking work?

Basically, you have a script that sends a query to Google emulating a user search. Then, through a process called scraping the script grabs the information that Google replies. Then the script extracts the information required.

That sounds simple,

But it’s actually not. Google doesn’t like automated scripts messing around in their search page.

Why?

Because they are recording every action real users do on the search page. They do that to improve the relevance of search results. It’s a known fact that Google will boost ranks of pages on which users tend to click more in the search results. But let’s get back to the SERP checks.

So, Google doesn’t like automated scraping. That’s why from time to time they will do checks using reCaptcha or even block access for such scripts.Many automated tools involve multiple IPs and proxies to avoid getting blocked by Google.

But there’s a safer way.

Using the Google Custom Search API, let’s see how we can create our own search engine keyword position checker PHP script.

Before Using The SERP Checking PHP Script

The SERP Checking PHP script below works with the Google Custom Search API. In order to use the script with the API you will need follow these steps:

  1. Activate Google Custom Search API and create credentials by going here: https://console.developers.google.com
  2. Create a Google Custom Search Engine here: https://cse.google.com/cse/all
  3. You can test that your credentials and the custom search engine is working here: https://developers.google.com/apis-explorer/?hl=en_US#p/customsearch/v1/search.cse.list
enable custom search api google console
1. Enable Google Custom Search API
create google custom search engine
2. Create Google Custom Search Engine

In Step 1, you will be obtain a Google API key and in Step 2 you will obtain a custom Search Engine ID. These will be required in the SERP Checker PHP Script.

Without further ado, let’s take a look at

The SERP Checker Source Code

$GOOGLE_API_KEY = 'Insert Your Google Custom Search API Key';
$GOOGLE_CSE_CX = 'Insert Your Search Engine ID';

//the search query
$query = urlencode($_POST["query"]);
//the domain for which to show the ranking
$domain = $_POST["domain"];

//gl - google host - https://developers.google.com/custom-search/docs/xml_results_appendices#countryCodes
//hl - user language - https://developers.google.com/custom-search/docs/xml_results_appendices#interfaceLanguages
//pages - how many pages should the search extend

$pages = isset($_POST["pages"])?$_POST["pages"]:1;
$gl = isset($_POST["gl"])?$_POST["gl"]:"us";
$hl = isset($_POST["hl"])?$_POST["hl"]:"en";

$found = false;
echo "<ul>";
for ($page = 1;$page <= $pages && $found == false;$page++){
$apiurl = sprintf('https://www.googleapis.com/customsearch/v1?q=%s&cx=%s&key=%s&hl=%s&gl=%s&start=%d',$query,$GOOGLE_CSE_CX,$GOOGLE_API_KEY,$hl,$gl,($page-1)*10+1);
$json = file_get_contents($apiurl);
$obj = json_decode($json);

foreach ($obj->items as $idx=>$item) {
if (strpos($item->link, $domain) ){
$found = true;
echo "<li>";
} else{
echo "<li class='other'>";
}
echo "<span class='rank'>".($idx + ($page-1)*10 +1)."</span>";
echo "<span class='title'>".$item->htmlTitle."</span>";
echo "<span class='link'>".$item->link."<small>▼</small></span>";
echo "<span class='snippet'>".$item->htmlSnippet."</span>";
echo "</li>";

}
}
if ($found !== true){
echo "<li>";
echo "<span class='title'>".$domain." not found</span>";
echo "</li>";
}
echo "</ul>";

You can download a fully working PHP script that shows the position of a domain for a certain keyword search:

Free SERP Checker PHP Script

The download includes CSS that will style the search results just like in the Google search results, thus making it a fully working SERP checker.

Here’s how the tool looks like:

serp checker php scriptThere you have it:

Your own Google keyword position checker PHP script. Please share if you found this useful.

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.
[jetpack-related-posts]

1 Comment

  1. […] a way to change the SEO meta tags and a preview of the SERP. By the way, I recently published a SERP checker PHP script – a Google keywords rank checker for your pages and also allows you to preview your […]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top