Author: Ash Kiswany
Updated on: 2017-02-25
Posted on: 2015-12-09
Categories: PHP Tutorials
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
The PHP Google Custom Search API package
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
|q||string||The search expression|
|c2coff||string||Enables or disables Simplified and Traditional Chinese Search.|
|cr||string||Restricts search results to documents originating in a particular country.|
|cref||string||The URL of a linked custom search engine specification to use for this request.|
|cx||string||The custom search engine ID to use for this request.|
|dateRestrict||string||Restricts results to URLs based on date.|
|exactTerms||string||Identifies a phrase that all documents in the search results must contain.|
|excludeTerms||string||Identifies a word or phrase that should not appear in any documents in the search results.|
|fileType||string||Restricts results to files of a specified extension.|
|filter||string||Controls turning on or off the duplicate content filter.|
|gl||string||Geolocation of end user.|
|googlehost||string||The local Google domain (for example, google.com, google.de, or google.fr) to use to perform the search.|
|highRange||string||Specifies the ending value for a search range.|
|hl||string||Sets the user interface language.|
|hq||string||Appends the specified query terms to the query, as if they were combined with a logical AND operator.|
|imgColorType||string||Returns black and white, grayscale, or color images: mono, gray, and color.|
|imgDominantColor||string||Returns images of a specific dominant color.|
|imgSize||string||Returns images of a specified size.|
|imgType||string||Returns images of a type.|
|linkSite||string||Specifies that all search results should contain a link to a particular URL|
|lowRange||string||Specifies the starting value for a search range.|
|lr||string||Restricts the search to documents written in a particular language (e.g., lr=lang_ja).|
|num||unsigned integer||Number of search results to return.|
|orTerms||string||Provides 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.|
|relatedSite||string||Specifies that all search results should be pages that are related to the specified URL.|
|rights||string||Filters based on licensing. Supported values include: cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial, cc_nonderived, and combinations of these.|
|safe||string||Search safety level.|
|searchType||string||Specifies the search type: image. If unspecified, results are limited to webpages.|
|siteSearch||string||Specifies all search results should be pages from a given site.|
|siteSearchFilter||string||Controls whether to include or exclude results from the site named in the siteSearch parameter.|
|sort||string||The sort expression to apply to the results.|
|start||unsigned integer||The index of the first result to return.|
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.
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,512,413 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
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