PHP Classes

Google Search API PHP Example Tutorial with Demo to Get the First Web Pages and Images as a JSON Array

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Google Search API PHP...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  

Author:

Updated on: 2025-12-21

Viewers: 4,172

Last month viewers: 594

Categories: PHP Tutorials

Appearing in the Google Search results is very important these days but it is not that easy. The competition is very high and your pages need to rank with the right keywords.

Finding if you are ranking with the right keywords is very difficult. You need to take every keyword and test it against a search, then look for your site in the search results list. However, Google blocks scripts that scrape the search result pages.

Fortunately there is an official API from Google that lets you search for certain keywords and you can use it from PHP or any other language.

Read this article to learn how to use the Google Search API from PHP to automate searches and find your site in the results.




Loaded Article
Picture of Ash Kiswany
By Ash Kiswany Palestine

<email contact>

Contents

Introduction

How to Scrape Google Search Results using PHP

Google Search API Tutorial Using the PHP Google Custom Search API package 

Install the PHP Google CustomSearch Package using PHP Composer

Simple Google API PHP Example

Can I Use this Package to Implement a Google Indexing API PHP Client

How to Implement a PHP Google Keyword Ranking Position Finder to Implement a Search Engine Keyword Position Checker PHP Script

Conclusion

Introduction

The Google Search API lets you search for given keywords and receive the results in a JSON encoded array that you can easily process in PHP.

Google is the leading search engine. Its search API provides you the same quality of results that you are used to from the Google search engine site. Using some PHP loops and array searching methods you can get very good analysis to find out where are your golden keywords ranking.

How to Scrape Google Search Results using PHP

You should not try to scrape Google Search results pages because that is not the way officially supported by Google.

Scraping may overload Google server computers that serve Google Search results. That imposes a cost that Google needs to pay in terms of energy and servers that are necessary to provide Google search results to external applications.

Therefore, Google uses CAPTCHA solutions to avoid excessive accesses from robot scripts that scrape Google Search results.

The official way that Google allows to access search results is to use an official Google API. This article explains how you access Google Search results using the official Google API from PHP scripts.

Google Search API Tutorial Using the PHP Google Custom Search API package 

Google Search API enables you to send search queries 100 times a day for free. You can use it on your site but here we are using it for our applications. You can configure your search query to search both Web pages and images and you can fine-tune the ranking.

Using PHP Google Custom Search API package you can perform a search by passing in the search keywords plus an optional array of advanced search options to API call URL and get a response in JSON or ATOM.

Table of Search Options with URL Request Google API PHP CustomSearch Parameters 

Parameter nameValueDescription
qstringThe search expression
c2coffstringEnables or disables Simplified and Traditional Chinese Search.
crstringRestricts search results to documents originating in a particular country.
crefstringThe URL of a linked custom search engine specification to use for this request.
cxstringThe custom search engine ID to use for this request.
dateRestrictstringRestricts results to URLs based on date.
exactTermsstringIdentifies a phrase that all documents in the search results must contain.
excludeTermsstringIdentifies a word or phrase that should not appear in any documents in the search results.
fileTypestringRestricts results to files of a specified extension.
filterstringControls turning on or off the duplicate content filter.
glstringGeolocation of end user.
googlehoststringThe local Google domain (for example, google.com, google.de, or google.fr) to use to perform the search.
highRangestringSpecifies the ending value for a search range.
hlstringSets the user interface language.
hqstringAppends the specified query terms to the query, as if they were combined with a logical AND operator.
imgColorTypestringReturns black and white, grayscale, or color images: mono, gray, and color.
imgDominantColorstringReturns images of a specific dominant color.
imgSizestringReturns images of a specified size.
imgTypestringReturns images of a type.
linkSitestringSpecifies that all search results should contain a link to a particular URL
lowRangestringSpecifies the starting value for a search range.
lrstringRestricts the search to documents written in a particular language (e.g., lr=lang_ja).
numunsigned integerNumber of search results to return.
orTermsstringProvides additional search terms to check for in a document, where each document in the search results must contain at least one of the additional search terms.
relatedSitestringSpecifies that all search results should be pages that are related to the specified URL.
rightsstringFilters based on licensing. Supported values include: cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial, cc_nonderived, and combinations of these.
safestringSearch safety level.
searchTypestringSpecifies the search type: image. If unspecified, results are limited to webpages.
siteSearchstringSpecifies all search results should be pages from a given site.
siteSearchFilterstringControls whether to include or exclude results from the site named in the siteSearch parameter.
sortstringThe sort expression to apply to the results.
startunsigned integerThe index of the first result to return.

Install the PHP Google CustomSearch Package using PHP Composer

With this class the work becomes very easy. You do not need to create any search URLs and process any hard JSON responses.

The first step to use this package it is to install it in your PHP environment.

You can install using a more traditional way just by downloading and extracting a compressed archive and use the require statement to include the CustomSearch.php function

You can use also use a more modern tool that is PHP composer. In this case, you need to have a composer.json file that includes the following configuration:

{
  "require":
  {
    "phpclasses/google-custom-search": ">=1.0.0"
  },
  "repositories":
  [
    {
      "type": "composer",
      "url": "https:\/\/www.phpclasses.org\/"
    },
    {
      "packagist": false
    }
  ]
}

Then you install or update the package using the following statement from the command line if you have PHP composer installed using the composer.phar archive:

php composer.phar update

From this point you can use the CustomSearch class or any other class installed using the PHP composer tool using the vendor/autoload.php script.

Simple Google API PHP Example

<?php

// If you installed this package using PHP composer,
// use the require_once statement below. 
require_once 'vendor/autoload.php';

// If you installed this package saving CustomSearch.php script in the
// current directory, uncomment the require_once statement below. 
// require_once 'CustomSearch.php';

use Fogg\Google\CustomSearch\CustomSearch;

// Initialize the search class 

$cs = new Fogg\Google\CustomSearch\CustomSearch\CustomSearch();

// Perform a simple search 
$response = $cs->simpleSearch( 'whole foods' );

The response will be an object with all the results for the keywords you send in the query, in this example "whole foods". You will need then search for your site in this array and you will get your position. Do that for all your keywords and you will get which keywords best suites for you.

Can I Use this Package to Implement a Google Indexing API PHP Client

The Google Indexing API is a different API. It is use to tell Google to index certain pages that were updated.

Currently, this package is for a different purpose, which is to perform custom searches using Google.

For using Google Indexing API or any other API, you may use the PHP OAuth Library package. It supports many types of API and can use refresh tokens to automatically use updated API access tokens, so you can use it with scripts that need to run without human intervention.

Here is an article on how to access APIs from PHP using OAuth without user interaction.

How to Implement a PHP Google Keyword Ranking Position Finder to Implement a Search Engine Keyword Position Checker PHP Script

This package can search of any keywords in Google. If you want to check the position of a given page in Google search results, you check the results of the simpleSearch class function until you find the page that you want to find the the position in Google for the given search keywords.

Conclusion

The PHP Google Custom Search API is a package that allows external applications to perform searches using the Google search engine. This class can perform arbitrary searches on Google using its custom search API.

Next part of this article you will learn how to perform advanced queries and how to find your Web site pages ranked in the Google search results.

If you liked this article or have questions about searching using the official Google Search API, post a comment here.




You need to be a registered user or login to post a comment

1,618,020 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

3. show - Alexandre Zonari (2017-06-18 06:06)
show... - 0 replies
Read the whole comment and replies

1. Fatal error: Uncaught exception 'RuntimeException' with message - exzant (2017-03-26 05:39)
Fatal error: Uncaught exception 'RuntimeException' with message... - 3 replies
Read the whole comment and replies

2. PHP Google Custom Search API - Kostiantyn N (2016-06-28 17:06)
requirements... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Google Search API PHP...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)