All Projects → serpapi → google-search-results-php

serpapi / google-search-results-php

Licence: MIT license
Google Search Results PHP API via Serp Api

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to google-search-results-php

google-search-results-java
Google Search Results JAVA API via SerpApi
Stars: ✭ 18 (-56.1%)
Mutual labels:  serp-api
faxplus-php
PHP SDK to use FAX.PLUS API
Stars: ✭ 23 (-43.9%)
Mutual labels:  php-api
php-antispam
A PHP API for antispam service cleantalk.org. Invisible protection from spam, no captches, no puzzles, no animals and no math.
Stars: ✭ 50 (+21.95%)
Mutual labels:  php-api
google-search-results-nodejs
SerpApi client library for Node.js. Previously: Google Search Results Node.js.
Stars: ✭ 46 (+12.2%)
Mutual labels:  serp-api
Apiato
PHP Framework for building scalable API's on top of Laravel.
Stars: ✭ 2,564 (+6153.66%)
Mutual labels:  php-api
Php Crud Api
Single file PHP script that adds a REST API to a SQL database
Stars: ✭ 2,904 (+6982.93%)
Mutual labels:  php-api
modulpos-php-api-client
PHP client for Modul.Kassa API Fiscal Service
Stars: ✭ 15 (-63.41%)
Mutual labels:  php-api
n11-php-api
Bu api n11 için yazılmıştır. N11 için yazılmış olan gelişmiş bir php apisi. Ekstra olarak n11 üzerinde mağazanıza gelen siparişleri websitenize aktaracak bir fonksiyonda mevcuttur.
Stars: ✭ 57 (+39.02%)
Mutual labels:  php-api
simple-php-api
An extremely simple API (with authentication) example, written in PHP (server) and JS (client), using JSON
Stars: ✭ 23 (-43.9%)
Mutual labels:  php-api
mixerapi
A CakePHP Plugin for RESTful API Development [READ-ONLY]
Stars: ✭ 26 (-36.59%)
Mutual labels:  php-api
Waktu-Solat-API
PHP API for fetching prayer time from JAKIM e-Solat website
Stars: ✭ 27 (-34.15%)
Mutual labels:  php-api
NeteaseCloudMusicApi
网易云音乐API PHP
Stars: ✭ 30 (-26.83%)
Mutual labels:  php-api
makeapidoc
php自动生成api文档
Stars: ✭ 67 (+63.41%)
Mutual labels:  php-api
CoronaVirusOutbreakAPI
A tiny and small program to crawler and analyze outbreak of COVID-19 in world and every country using PHP.
Stars: ✭ 20 (-51.22%)
Mutual labels:  php-api
google-search-results-golang
Google Search Results GoLang API
Stars: ✭ 36 (-12.2%)
Mutual labels:  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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].