PHP Classes
PHP Classes
elePHPant
Icontem

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 (5)   Trackbacks (0)  

Author:

Updated on: 2017-02-25

Posted on:

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.





By Ash Kiswany Palestine

<email contact>

Contents

Introduction

The PHP Google Custom Search API package

Simple Example

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.

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

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.

Simple Example

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

<?php 
use Fogg\Google\CustomSearch\ CustomSearch; 
require 'CustomSearch.php'; 

//Initialize the search class 
$cs = new 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.

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,440,214 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

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 (5)   Trackbacks (0)