Google 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 the Google 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
//www.coding-dude.com
Check out the free Google SERP checker DEMO page.
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 downloading the source code for the PHP keyword rank checker, 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. This post will be about Google SERPs.
Can I Do Manual Google SERP Checking?
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 Google 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.
You can take this even further. You could use the same code and store the position for each keyword and link. This way you can create your own free SERP rank tracker. This could potentially be a very powerful SEO tool similar to what you get for high paying SEO tools like Ahrefs or Semrush.
Before Using The Google 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:
- Activate Google Custom Search API and create credentials by going here: https://console.developers.google.com
- Create a Google Custom Search Engine here: https://cse.google.com/cse/all
- 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
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
Some PHP Settings
While deploying this PHP SERP checker script on various server configurations I ran into the following error:
failed to open stream: no suitable wrapper could be found
This happens when trying to access the data from Google APIs using the PHP function file_get_contents()
. To fix the error you need to make sure that your php.ini configuration file contains the following setting:
allow_url_fopen = on
The Google 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>";
SERP CHECKER PHP ONLINE DEMO
OR
You can download a fully working PHP script that shows the position of a domain for a certain keyword search:
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:
Your own Google keyword position checker PHP script. Please share if you found this useful.
[…] 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 […]
Hello Friend!
if I include my site in “Sites to search” on cse.google.com, I only get results with my site. If I delete it – I do not get any results, what am I doing wrong?
What do you want to achieve?
Make “Search Entire Search” Option “ON”
Yup, you are right. It’s actually called “Search Entire Web”. I’ve added the info to the article, thanks for letting me know.
Hi. Great tutorial. What do you put in “Sites to search”? It won’t let me proceed without specifying at least one, getting “missing information”.
If I put “google.com” it doesn’t return correct results..
Do you mean the “domain” field? You should put a domain name there (for example your website) and the PHP SERP checker script will highlight the search result entry for that particular keyword. See the example image
Hello,
thank you for the great script! I have an error when try to use the code:
Warning: Invalid argument supplied for foreach() in /home/accname/public_html/serp-checker.php on line 480
Any ideas ?
Best Regards!
Hi,
I think it’s only a warning, so functionality is not affected. Probably to avoid getting the warning wrap the foreach block with a check like this:
…
if (is_array($obj->items))
{
foreach…
hope it works for you
Imposible instalarlo, muy complicado la API de Google; me vuelvo loco para habilitar un mapa con esto que no encuentro los link.. En fin, una pena
Hi Luis, what exactly are you missing? Just follow the steps and the screenshots
Hi John, thank you for this script! It works great.
I have a really stupid question about billing.
I understand you get 100 free queries per day, and an additional 1000 is $5, per day.
If I wanted to run 1000 queries every day, for a month, does that mean I would pay ($5 x 30 days)? Or do I pay $5 for the month and get 1000 queries a day?
$150 ($5 every day for 30 days) seems on the high side, considering they give you 100 free a day.
I’ve tried to understand the Google billing docs but they might as well be in a different language.
Thank you in advance
Hi Ryan! Glad to hear you found this script useful. I will try to answer your question:
The billing model is pretty straight forward. Please note that it’s $5/1000 queries (not per day).
I think they charge fractions, so $0.005/query and each day you get 100 queries free.
In your example, $150 for 30 days would get you 1100 queries each day totaling to 33000 queries/month.
I agree that they can be quite expensive, but so are pretty much all Google’s APIs when you want to use them on a large scale.
hope this makes sense! (for reference I guess you already read this: https://developers.google.com/custom-search/v1/overview)
Thanks for the response John, I was hoping to use it as a rank tracker so the cost is not worth it. I’d be better off scraping with rotating proxies, or even continuing to pay $100 a month for 1500 daily keywords with a rank tracker than using Google and doing the coding myself 😀
What a shame, I was hoping I was about to build an easy and cheap rank tracker for a few bucks a month! Nevermind.
Thank you again, much appreciated
I’m using this myself exactly as a keyword rank tracker, and I have multiple websites, but I never needed more than 100 queries per day. If you want to turn it into an online web app for users, then that becomes costly and you need to charge users for that. Looking at Ahrefs and SEMRush which do this also, explains why they charge so much for their subscription plans.
If you find cheaper alternatives for doing this please let me know because I would also be interested.
This doesnt seem to work anymore – atleast the online tool.
Did it really work in the past?
You do know that the CSE only searches Sites that you define in the CSE, right?
You have to check the option “Search Entire Web”. I’ve added the info in the article now. I assure you that the script works and I use it all the time here: http://www.coding-dude.com/serp-checker/
Hello,
When I try this script, I get “domain not found” once I submit….Any thoughts?
did you follow the instructions for creating the CSE?