GithubHelp home page GithubHelp logo

faihad / google-search-results-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serpapi/google-search-results-php

0.0 1.0 0.0 30 KB

Google Search Results PHP API via Serp Api

Home Page: https://serpapi.com

License: MIT License

Makefile 3.53% PHP 96.47%

google-search-results-php's Introduction

Google Search Results in PHP

Build Status

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

Feel free to fork this repository to add more backends.

The full documentation is available here.

The following services are provided:

Serp API 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

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

require 'path/to/google_search_results';

Then you can start coding something like:

$client = new GoogleSearchResults("demo");
$result = $client(["q" => "coffee","location"=>"Austin,Texas"]);

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

The Serp API service (backend)

  • searches on Google using the query: q = "coffee"
  • parses the messy HTML responses
  • return a standardizes JSON response The Php class GoogleSearchResults
  • Format the request to Serp API 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 BingSearchResults class
  • Baidu using BaiduSearchResults class

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

Example

How to set SERP API key

The Serp API key can be set globally using a singleton pattern.

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

Or

$client = new GoogleSearchResults("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 GoogleSearchResults("private key");

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

Location API

$client = new GoogleSearchResults($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 GoogleSearchResults($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 GoogleSearchResults($this->API_KEY);
$info = $client->get_account();
print_r($info);

it prints your account information.

Search Google Images

$client = new GoogleSearchResults($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

Conclusion

This service supports Google Images, News, Shopping. 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 full documentation is available here.

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

Thanks Rest API for Php

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.