Google Search Results PHP API via Serp Api

Overview

Google Search Results in PHP

PHP build

This Php API is meant to scrape and parse Google, Bing or Baidu results using SerpApi.

The full documentation is available here.

The following services are provided:

SerpApi provides a script builder to get you started quickly.

Installation

Php 7+ must be already installed and composer dependency management tool.

Package available from packagist.

Quick start

if you're using composer, you can add this package (link to packagist).

$ composer require serpapi/google-search-results-php

Then you need to load the dependency in your script.

<?php
require __DIR__ . '/vendor/autoload.php';
 ?>

if not, you must clone this repository and link the class.

require 'path/to/google-search-results';
require 'path/to/restclient';

Get "your secret key" from https://serpapi.com/dashboard

Then you can start coding something like:

$client = new GoogleSearch("your secret key");
$query = ["q" => "coffee","location"=>"Austin,Texas"];
$response = $client->get_json($query);
print_r($json_results)

This example runs a search about "coffee" using your secret api key.

The SerpApi service (backend)

  • searches on Google using the query: q = "coffee"
  • parses the messy HTML responses
  • return a standardizes JSON response The Php class GoogleSearch
  • Format the request to SerpApi server
  • Execute GET http request
  • Parse JSON into Ruby Hash using JSON standard library provided by Ruby Et voila..

Alternatively, you can search:

  • Bing using BingSearch class
  • Baidu using BaiduSearch class
  • Ebay using EbaySearch class
  • Yahoo using YahooSearch class
  • Yandex using YandexSearch class
  • Walmart using WalmartSearch class
  • Youtube using YoutubeSearch class

See the playground to generate your code. https://serpapi.com/playground

Example

How to set SERP API key

The SerpApi api_key can be set globally using a singleton pattern.

$client = new GoogleSearch();
$client->set_serp_api_key("Your Private Key");

Or

$client = new GoogleSearch("Your Private Key");

Search API capability

$query = [
  "q" =>  "query",
  "google_domain" =>  "Google Domain", 
  "location" =>  "Location Requested", 
  "device" =>  "device",
  "hl" =>  "Google UI Language",
  "gl" =>  "Google Country",
  "safe" =>  "Safe Search Flag",
  "num" =>  "Number of Results",
  "start" =>  "Pagination Offset",
  "serp_api_key" =>  "Your SERP API Key",
  "tbm" => "nws|isch|shop"
  "tbs" => "custom to be search criteria"
  "async" => true|false # allow async 
];

$client = new GoogleSearch("private key");

$html_results = $client->get_html($query);
$json_results = $client->get_json($query);

Location API

$client = new GoogleSearch($this->API_KEY);
$location_list = $client->get_location('Austin', 3);
print_r($location_list);

it prints the first 3 location matching Austin (Texas, Texas, Rochester)

[{:id=>"585069bdee19ad271e9bc072",
  :google_id=>200635,
  :google_parent_id=>21176,
  :name=>"Austin, TX",
  :canonical_name=>"Austin,TX,Texas,United States",
  :country_code=>"US",
  :target_type=>"DMA Region",
  :reach=>5560000,
  :gps=>[-97.7430608, 30.267153],
  :keys=>["austin", "tx", "texas", "united", "states"]},
  ...]

Search Archive API

Let's run a search to get a search_id.

$client = new GoogleSearch($this->API_KEY);
$result = $client->get_json($this->QUERY);
$search_id = $result->search_metadata->id

Now let's retrieve the previous search from the archive.

$archived_result = $client->get_search_archive($search_id);
print_r($archived_result);

it prints the search from the archive.

Account API

$client = new GoogleSearch($this->API_KEY);
$info = $client->get_account();
print_r($info);

it prints your account information.

Search Google Images

$client = new GoogleSearch($this->API_KEY);
$data = $client->get_json([
  'q' => "Coffee", 
  'tbm' => 'isch'
]);

foreach($data->images_results as $image_result) {
  print_r($image_result->original);
  //to download the image:
  // `wget #{image_result[:original]}`
}

this code prints all the images links, and download image if you un-comment the line with wget (linux/osx tool to download image).

Example by specification

The code described above is tested in the file test.php and example.php. To run the test locally.

export API_KEY='your secret key'
make test example

Composer example

see: https://github.com/serpapi/google-search-results-php/example_composer/

To run the code.

Change log

  • 2.0
    • Code refractoring SearchResult -> Search
    • Add walmart and youtube search engine
  • 1.2.0
    • Add more search engine
  • 1.0
    • First stable version

Conclusion

SerpApi supports all the major search engines. Google has the more advance support with all the major services available: Images, News, Shopping and more.. To enable a type of search, the field tbm (to be matched) must be set to:

  • isch: Google Images API.
  • nws: Google News API.
  • shop: Google Shopping API.
  • any other Google service should work out of the box.
  • (no tbm parameter): regular Google search. The field tbs allows to customize the search even more.

The full documentation is available here.

Author: Victor Benarbia [email protected] For more information: https://serpapi.com

Thanks Rest API for Php

You might also like...
Production-grade rapid controller development with built in love for API and Search

Installation For CakePHP 4.x compatible version: composer require friendsofcake/crud For CakePHP 3.x compatible version: composer require friendsofca

Biblioteca para busca (on-line) de estados e os seus respectivos municípios via API disponibilizada pelo IBGE.
Biblioteca para busca (on-line) de estados e os seus respectivos municípios via API disponibilizada pelo IBGE.

LocalidadeX Biblioteca para busca (on-line) e listagem de estados e seus respectivos municípios, efetua a consulta via API do IBGE, disponibilizando o

Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

JSONFinder - a library that can find json values in a mixed text or html documents, can filter and search the json tree, and converts php objects to json without 'ext-json' extension.

Helps detect the user's browser and platform at the PHP level via the user agent

cbschuld/browser.php Helps detect the user's browser and platform at the PHP level via the user agent Installation You can add this library as a local

Simple JQL builder for Jira search

Jql Builder Simple JQL builder for Jira search Installation composer require devmoath/jql-builder Usage Generate query with one condition: \DevMoath\J

An ultra-fast Laravel docs search workflow for Alfred 3+.
An ultra-fast Laravel docs search workflow for Alfred 3+.

Laravel Docs Workflow for Alfred An ultra-fast Laravel docs search workflow for Alfred 3+. Installation Download the latest version Install the workfl

Silverstripe-fulltextsearch - Adds external full text search engine support to SilverStripe

FullTextSearch module Adds support for fulltext search engines like Sphinx and Solr to SilverStripe CMS. Compatible with PHP 7.2 Important notes when

Comments
  • Update PHP version to 8.1.3

    Update PHP version to 8.1.3

    Throws an error when updating google-search-results-php to latest 2.0 version.

    Local version is 1.2.0:

    $ composer show
    serpapi/google-search-results-php 1.2.0 Google search result via Serp API
    

    Updating via composer to version 2.0:

    $ composer require serpapi/google-search-results-php:2.0
    

    Error:

    - serpapi/google-search-results-php 2.0 requires php ^5.5 || ^7.0 -> your php version (8.1.3) does not satisfy that requirement.
    Installation failed, reverting ./composer.json and ./composer.lock to their original content.
    

    Pretty much a duplicate of PHP 8 #7 issue.

    opened by dimitryzub 4
  • Package name invalid

    Package name invalid

    Hi, I tried to submit the project to packagist.org to get it work with an other composer project but I got this error.

    The package name SerpAPI/google-search-results-php is invalid, it should not contain uppercase characters. We suggest using serp-api/google-search-results-php instead.

    Please change the name of the package Regards Jo

    opened by johaziel 3
  • [SerpApi - PHP library] Update restclient and fix SSL issue

    [SerpApi - PHP library] Update restclient and fix SSL issue

    Closes: https://github.com/serpapi/google-search-results-php/issues/9 Closes: https://github.com/serpapi/google-search-results-php/issues/7

    This PR will update the restclient and it will support PHP 8.

    Some users having issues regarding SSL as well, it'll be fixed in this PR..

    Intercom 1 | Intercom 2

    opened by Alaa-abdulridha 0
  • Access to Google Scholar BiBTeX data

    Access to Google Scholar BiBTeX data

    Thank you for this API!! It is good to see that at last someone is working on this officially. I've tried scraping Google Scholar via some code I wrote using Swift and whilst I was able to parse each entry, my code was temperamental because of Google's anti-robot mechanisms. Because I was using Swift's internal http client, I had issues with some cookie information that caused Google Scholar to invoke reCaptcha during my runs.

    Whilst this issue pertains to extending the API to enable access to the BiBTeX reference information (and other formats for that matter), I would also like to point out that (often) the reference data on Google Scholar is not as accurate as it could be. For example, using the playground to find "M-Coffee: combining multiple sequence alignment methods with T-Coffee" and comparing the returned data with Google Scholar's BiBTeX information as well as Oxford's Academic Database, the discrepancies will be clear:

    Google Scholar's BiBTeX data for this paper (No DOI information):

    @article{wallace2006m,
      title={M-Coffee: combining multiple sequence alignment methods with T-Coffee},
      author={Wallace, Iain M and O'sullivan, Orla and Higgins, Desmond G and Notredame, Cedric},
      journal={Nucleic acids research},
      volume={34},
      number={6},
      pages={1692--1699},
      year={2006},
      publisher={Oxford University Press}
    }
    

    Oxford Academic's data on the paper: https://academic.oup.com/nar/article/34/6/1692/2401531

    Data returned by this API (Only three of the four authors mentioned):

    {
        "search_metadata":
        {
            "id": "5f4bc5a9002fca2990aa41c2",
            "status": "Success",
            "json_endpoint": "https://serpapi.com/searches/48162f4a4112bb86/5f4bc5a9002fca2990aa41c2.json",
            "created_at": "2020-08-30 15:28:41 UTC",
            "processed_at": "2020-08-30 15:28:41 UTC",
            "google_scholar_url": "https://scholar.google.com/scholar?q=M-Coffee%3A+combining+multiple+sequence+alignment+methods+with+T-Coffee&hl=en",
            "raw_html_file": "https://serpapi.com/searches/48162f4a4112bb86/5f4bc5a9002fca2990aa41c2.html",
            "total_time_taken": 0.82
        }
        ,
        "search_parameters":
        {
            "engine": "google_scholar",
            "q": "M-Coffee: combining multiple sequence alignment methods with T-Coffee",
            "hl": "en"
        }
        ,
        "search_information":
        {
            "organic_results_state": "Results for exact spelling",
            "query_displayed": "M-Coffee: combining multiple sequence alignment methods with T-Coffee"
        }
        ,
        "organic_results":
        [
            "0":
            {
                "position": 0,
                "title": "M-Coffee: combining multiple sequence alignment methods with T-Coffee",
                "result_id": "_3o-xhuGyg0J",
                "link": "https://academic.oup.com/nar/article-abstract/34/6/1692/2401531",
                "snippet": "We introduce M-Coffee, a meta-method for assembling multiple sequence alignments (MSA) by combining the output of several individual methods into one single MSA. M-Coffee is an extension of T-Coffee and uses consistency to estimate a consensus alignment. We show that the procedure is robust to variations in the choice of constituent methods and reasonably tolerant to duplicate MSAs. We also show that performances can be improved by carefully selecting the constituent methods. M-Coffee outperforms all the individual methods …",
                "publication_info":
                {
                    "summary": "IM Wallace, O O'sullivan, DG Higgins… - Nucleic acids …, 2006 - academic.oup.com",
                    "authors":
                    [
                        "0":
                        {
                            "name": "IM Wallace",
                            "link": "https://scholar.google.com/citations?user=oYUWc7YAAAAJ&hl=en&oi=sra"
                        }
                        ,
                        "1":
                        {
                            "name": "O O'sullivan",
                            "link": "https://scholar.google.com/citations?user=rYniXB8AAAAJ&hl=en&oi=sra"
                        }
                        ,
                        "2":
                        {
                            "name": "DG Higgins",
                            "link": "https://scholar.google.com/citations?user=Ap0K7rUAAAAJ&hl=en&oi=sra"
                        }
                    ]
                }
                ,
                "resources":
                [
                    "0":
                    {
                        "title": "oup.com",
                        "file_format": "HTML",
                        "link": "https://academic.oup.com/nar/article/34/6/1692/2401531"
                    }
                ]
                ,
                "inline_links":
                {
                    "serpapi_cite_link": "https://serpapi.com/search.json?engine=google_scholar_cite&q=_3o-xhuGyg0J",
                    "html_version": "https://academic.oup.com/nar/article/34/6/1692/2401531",
                    "cited_by":
                    {
                        "total": 508,
                        "link": "https://scholar.google.com/scholar?cites=993754121636838143&as_sdt=5,44&sciodt=0,44&hl=en",
                        "serpapi_scholar_link": "https://serpapi.com/search.json?cites=993754121636838143&engine=google_scholar&hl=en&q=M-Coffee%3A+combining+multiple+sequence+alignment+methods+with+T-Coffee"
                    }
                    ,
                    "related_pages_link": "https://scholar.google.com/scholar?q=related:_3o-xhuGyg0J:scholar.google.com/&scioq=M-Coffee:+combining+multiple+sequence+alignment+methods+with+T-Coffee&hl=en&as_sdt=0,44",
                    "versions":
                    {
                        "total": 18,
                        "link": "https://scholar.google.com/scholar?cluster=993754121636838143&hl=en&as_sdt=0,44",
                        "serpapi_scholar_link": "https://serpapi.com/search.json?cluster=993754121636838143&engine=google_scholar&hl=en&q=M-Coffee%3A+combining+multiple+sequence+alignment+methods+with+T-Coffee"
                    }
                }
            }
        ]
    }
    
    opened by Napoleon-BlownApart 1
Releases(1.2.0)
Owner
SerpApi
API to get search engine results with ease.
SerpApi
SERP Scraping API code examples for Python, PHP and Node.js

SERP Scraping API List of contents Introduction Authentication Google Baidu Bing Yandex Parameters Targets Languages License Introduction With our SER

Smartproxy 8 Nov 7, 2022
Silverstripe-searchable - Adds to the default Silverstripe search by adding a custom results controller and allowing properly adding custom data objects and custom fields for searching

SilverStripe Searchable Module UPDATE - Full Text Search This module now uses Full Text Support for MySQL/MariaDB databases in version 3.* Adds more c

ilateral 13 Apr 14, 2022
search non profitable charity or organization through api search

Non Profile Charity Search Search non profitable organization or get the details of an organization Installation Require the package using composer: c

Touhidur Rahman 5 Jan 20, 2022
Nova Search is an open source search engine developed by the Artado Project.

Loli Search Loli Search açık kaynak kodlu bir arama motorudur ve yalnızca kendi sonuçlarını değil, diğer arama motorlarının sonuçlarını da göstermekte

Artado Project 10 Jul 22, 2022
Doogle is a search engine and web crawler which can search indexed websites and images

Doogle Doogle is a search engine and web crawler which can search indexed websites and images, and then use keywords to be searched later. Written pri

Zepher Ashe 9 Jan 1, 2023
WebAgency certificate results done by Mickaël Moreau, delivred by Axelo (web-agency.app)

WebAgencyCertificate WebAgency certificate results done by Mickaël Moreau, delivred by Axelo (web-agency.app) Résultat du certificat en pré-production

Mickaël Moreau 1 Sep 20, 2022
salah eddine bendyab 18 Aug 17, 2021
Connect Nios4 with Google Calendar via PHP

Nios4_Google_Calendar Connect Nios4 with Google Calendar via PHP ISTRUZIONI INTEGRAZIONE GOOGLE CALENDAR CON NIOS4. Guida per l'integrazione del calen

null 2 Sep 23, 2021
A privacy respecting free as in freedom meta search engine for Google and popular torrent sites

A privacy respecting free as in freedom meta search engine for Google and popular torrent sites

null 329 Dec 27, 2022
A tool to create php lambda's in AWS via custom runtime api

Getting Started This composer library assists in the creation, configuration, and testing of an AWS Lambda function. It utilizes the AWS Lambda custom

Mike McGrath 0 Jul 13, 2022