• Stars
    star
    57
  • Rank 522,947 (Top 11 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 7 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Google Search Results PHP API via Serp Api

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($response)

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
  • HomeDepot using HomeDepotSearch class
  • Apple App Store using AppleAppStoreSearch class
  • Naver using NaverSearch 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(getenv("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(getenv("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(getenv("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

More Repositories

1

google-search-results-python

Google Search Results via SERP API pip Python Package
Python
593
star
2

awesome-seo-tools

Curated list of awesome SEO tools
HTML
290
star
3

lego-ai-parser

Lego AI Parser is an open-source application that uses OpenAI to parse visible text of HTML elements.
Python
227
star
4

nokolexbor

High-performance HTML5 parser for Ruby based on Lexbor, with support for both CSS selectors and XPath.
C
182
star
5

turbo_tests

Run RSpec tests on multiple cores. Like parallel_tests but with incremental summarized output. Originally extracted from the Discourse and Rubygems source code.
Ruby
173
star
6

clauneck

A tool for scraping emails, social media accounts, and much more information from websites using Google Search Results.
Ruby
159
star
7

google-search-results-nodejs

SerpApi client library for Node.js. Previously: Google Search Results Node.js.
JavaScript
78
star
8

google-search-results-golang

Google Search Results GoLang API
Go
58
star
9

serpapi-python

a Python client library for SerpApi.
Python
55
star
10

google-search-results-ruby

Google Search Results via SERP API Ruby Gem
Ruby
53
star
11

public-roadmap

Public Roadmap for SerpApi, LLC (https://serpapi.com)
50
star
12

serpapi-javascript

Scrape and parse search engine results using SerpApi.
TypeScript
48
star
13

google-search-results-java

Google Search Results JAVA API via SerpApi
Java
38
star
14

review-analyzer

A Chrome Extension for extracting valuable insights from reviews, generating concise summaries, sentiment analysis, and keyword extraction
JavaScript
32
star
15

google-reviews-analyzer

Uses LLM to summarize reviews of a business
JavaScript
28
star
16

serapis-ai-image-classifier

Automatic Image Classification from SERP Data
Python
26
star
17

Wander

Replicate Wanderlust demo that shown in OpenAI Dev Day
JavaScript
24
star
18

code-challenge

SerpApi code challenge
HTML
18
star
19

google-local-results-ai-parser

A ruby gem to extract structured data from Google Local Search Results using the serpapi/bert-base-local-results model, enabling parsing, classification, and information extraction from English HTML content.
Ruby
14
star
20

google-local-results-ai-server

A server code for serving BERT-based models for text classification. It is designed by SerpApi for heavy-load prototyping and production tasks, specifically for the implementation of the google-local-results-ai-parser gem.
Python
13
star
21

automatic-images-classifier-generator

Generate machine learning models fully automatically to clasiffiy any images using SERP data
Python
11
star
22

uule_converter

A Ruby library for encoding and decoding UULE parameters in Google search URLs using coordinates
Ruby
10
star
23

google-maps-pb-decoder

Google Maps pb (i.e., protobuf) parameter decoder.
Ruby
9
star
24

Auto-GPT-SerpApi-Plugin

An Auto-GPT Plugin that connects SerpApi to Auto-GPT
Python
8
star
25

google-search-results-dotnet

Google Search Results via SERP API DotNet Package
C#
8
star
26

serpapi-search-swift

Scrape and parse search resuts from Google, Bing, Baidu, Yandex, Yahoo, Home depot, Ebay and more.. using [SerpApi](https://serpapi.com).
Ruby
6
star
27

google-apps-script

Google Apps Scripts for Google Sheet to integrate SerpApi
JavaScript
4
star
28

serpapi-ruby

Official Ruby wrapper for SerpApi HTTP endpoints
Ruby
4
star
29

ved_decoder

VedDecoder is a decoder for the Google ved parameter
Ruby
3
star
30

serpapi-golang

SerpApi client implementation in Golang
Go
3
star
31

serpapi-search-rust

Search results in Rust powered by SerpApi.com
Rust
3
star
32

seo-rank-tracker

TypeScript
2
star
33

serpapi-rust

Scrape any major search engine from our easy, fast, scalable and feature rich API powered by SerpApi
Rust
2
star
34

google-sheet-addon-guide

Documentation for Google Sheet Add-on for SerpApi
2
star
35

test-knowledge-graph-desktop

Tests for Google Knowledge Graph API
Ruby
1
star
36

test-bing-organic-results-desktop

Tests for SerpApi desktop Bing organic results https://serpapi.com/bing-organic-results
Ruby
1
star
37

serpapi-dotnet

SerpApi Client library for dotnet 5 and 6
C#
1
star
38

test-product-results

Test Google Product page results
Ruby
1
star
39

test-bing-organic-results-mobile

Tests for SerpApi mobile Bing organic results https://serpapi.com/organic-results
1
star
40

test-shopping-results-desktop

Test shopping results for SerpApi desktop
Ruby
1
star
41

test-scholar-organic-results

Test Google Scholar organic results for SerpApi
Ruby
1
star
42

test-bing-knowledge-graph-desktop

Tests for SerpApi desktop Bing knowledge graph results https://serpapi.com/bing-knowledge-graph
Ruby
1
star
43

test-organic-results-desktop

Tests for SerpApi desktop organic results
Ruby
1
star
44

test-product-reviews-results

Ruby
1
star
45

serpapi-search-cpp

Library to search on Google, Bing, HomeDepot, Baidu, Yandex and more using SerpApi written in C++.
C++
1
star
46

test-organic-results-mobile

Tests for SerpApi mobile organic results
Ruby
1
star
47

test-product-specs-results

Ruby
1
star
48

serpapi-vscode-snippets

SerAPI Snippets for VSCode - Scrape search engine results
JavaScript
1
star
49

test-related-questions-desktop

Test related questions "People also ask" block
Ruby
1
star
50

test-images-results-desktop

Tests for SerpApi Images results for desktop (beta)
Ruby
1
star
51

showcase-pot-stock-map

showcase market research with serpapi to track pot stock
JavaScript
1
star
52

test-product-sellers-results

Ruby
1
star
53

test-google-direct-answers-box-api

Tests for Google Direct Answers Box API
Ruby
1
star
54

test-bing-ad-results-desktop

Tests for SerpApi desktop Bing ad results https://serpapi.com/bing-ads
Ruby
1
star
55

spec-builder

Run all tests for SerpApi.com
Ruby
1
star
56

test-video-results

Test video result for mobile and desktop
Ruby
1
star
57

test-news-results-desktop

Test news results for SerpApi desktop
Ruby
1
star
58

hash-json-path

HashJsonPath is a simple gem to access hash and set hash value using json path
Ruby
1
star
59

serpapi-wallstreet-analysis

Analyze company business using Google search powered by SerpApi.com
Python
1
star
60

showcase-serpapi-tensorflow-keras-image-training

Tensorflow / Keras training a network to recognize Apple logo versus a real Apple fruit
Python
1
star
61

serpapi-java

Official Java wrapper for SerpApi HTTP endpoints
Java
1
star