Coder Social home page Coder Social logo

google-search-results-php's Introduction

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

google-search-results-php's People

Contributors

alaa-abdulridha avatar atefbb avatar dimitryzub avatar drewroberts avatar encreinformatique avatar exfriend avatar freaky avatar hartator avatar ilyazub avatar jvmvik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

google-search-results-php's Issues

Please keep the minimum of conventions

Currently, this cannot be integrated nicely into a own project. Please use namespaces for your code and tag the final versions, otherwise everyone will have to integrate your repo into their own project on the dev-master state or a dedicated commit.

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.

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

Timeouts when using the PHP library

We are using the library to pull Google reviews. Very frequently, we get errors like this, where it says 'cURL error 28: Operation timed out after 5009 milliseconds with 0 bytes received'.

Is there a way to increase the timeout limit to something like 60 seconds?

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"
                }
            }
        }
    ]
}

php 8.2 support

Hi there, is there a plan to support php 8.2 soon?

`Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 266

Deprecated: Return type of RestClient::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 66

Deprecated: Return type of RestClient::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 74

Deprecated: Return type of RestClient::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 70

Deprecated: Return type of RestClient::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 78

Deprecated: Return type of RestClient::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 61

Deprecated: Return type of RestClient::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 84

Deprecated: Return type of RestClient::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 90

Deprecated: Return type of RestClient::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 99

Deprecated: Return type of RestClient::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/sb/main/vendor/serpapi/google-search-results-php/restclient.php on line 103`

PHP 8

We're relying on this package and need it to support PHP version 8. Are there any plans to upgrade the PHP requirement?

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.