Author: Ash Kiswany
Last month viewers: 594
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
|The search expression
|Enables or disables Simplified and Traditional Chinese Search.
|Restricts search results to documents originating in a particular country.
|The URL of a linked custom search engine specification to use for this request.
|The custom search engine ID to use for this request.
|Restricts results to URLs based on date.
|Identifies a phrase that all documents in the search results must contain.
|Identifies a word or phrase that should not appear in any documents in the search results.
|Restricts results to files of a specified extension.
|Controls turning on or off the duplicate content filter.
|Geolocation of end user.
|The local Google domain (for example, google.com, google.de, or google.fr) to use to perform the search.
|Specifies the ending value for a search range.
|Sets the user interface language.
|Appends the specified query terms to the query, as if they were combined with a logical AND operator.
|Returns black and white, grayscale, or color images: mono, gray, and color.
|Returns images of a specific dominant color.
|Returns images of a specified size.
|Returns images of a type.
|Specifies that all search results should contain a link to a particular URL
|Specifies the starting value for a search range.
|Restricts the search to documents written in a particular language (e.g., lr=lang_ja).
|Number of search results to return.
|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.
|Specifies that all search results should be pages that are related to the specified URL.
|Filters based on licensing. Supported values include: cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial, cc_nonderived, and combinations of these.
|Search safety level.
|Specifies the search type: image. If unspecified, results are limited to webpages.
|Specifies all search results should be pages from a given site.
|Controls whether to include or exclude results from the site named in the siteSearch parameter.
|The sort expression to apply to the results.
|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
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