serpwow/google-search-results

SerpWow.com的Google搜索结果PHP包

0.2.5 2020-02-15 09:56 UTC

This package is auto-updated.

Last update: 2024-09-15 19:39:47 UTC


README

此PHP包允许您使用SerpWow爬取和解析Google搜索结果。除了搜索之外,您还可以使用此包访问SerpWow的位置API批次API账户API

安装

您可以使用Composer安装google-search-results。

$ composer require serpwow/google-search-results

Packagist.com上查看包

文档

我们在此处包含了示例,但完整的SerpWow API文档可在API Docs找到。

您还可以使用API Playground通过SerpWow可视地构建Google搜索请求。

示例

简单示例

最简单的标准查询“披萨”示例,以JSON格式返回Google SERP(搜索引擎结果页面)数据。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the search parameters
$params = [
  "q" => "pizza"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// pretty-print the JSON result
print_r($result);

?>

获取API密钥

要获取免费的API密钥,请访问app.serpwow.com/signup

示例响应

以下显示的是JSON响应的快照(为了简洁而缩短)。有关解析Google搜索结果页面所有字段的详细信息,请参阅文档

{
  "request_info": {
    "success": true
  },
  "search_metadata": {
    "id": "20c8e44e9cacedabbdff2d9b7e854436056d4f33",
    "engine_url": "https://www.google.com/search?q=pizza&oq=pizza&sourceid=chrome&ie=UTF-8",
    "total_time_taken": 0.14
  },
  "search_parameters": {
    "q": "pizza"
  },
  "search_information": {
    "total_results": 1480000000,
    "time_taken_displayed": 0.45,
    "query_displayed": "pizza",
    "detected_location": "Ireland"
  },
  "local_map": {
    "link": "https://www.google.com/search?q=pizza&npsic=0&rflfq=1&rldoc=1&rlha=0&rllag=53350059,-6249133,1754&tbm=lcl&sa=X&ved=2ahUKEwiC3cLZ0JLgAhXHUxUIHQrsBC4QtgN6BAgBEAQ",
    "gps_coordinates": {
      "latitude": 53.350059,
      "longitude": -6.249133,
      "altitude": 1754
    },
    "local_results": [{
        "position": 1,
        "title": "Apache Pizza Temple Bar",
        "extensions": [
          "American-style pizza-delivery chain"
        ],
        "rating": 3.6,
        "reviews": 382,
        "type": "Pizza",
        "block_position": 1
      }
    ]
  },
  "knowledge_graph": {
    "title": "Pizza",
    "type": "Dish",
    "description": "Pizza is a savory dish of Italian origin, consisting of a usually round, flattened base of leavened wheat-based dough topped with tomatoes, cheese, and various other ingredients baked at a high temperature, traditionally in a wood-fired oven.",
    "source": {
      "name": "Wikipedia",
      "link": "https://en.wikipedia.org/wiki/Pizza"
    },
    "nutrition_facts": {
      "total_fat": [
        "10 g",
        "15%"
      ],
      "sugar": [
        "3.6 g"
      ]
    }
  },
  "related_searches": [{
      "query": "apache pizza",
      "link": "https://www.google.com/search?q=apache+pizza&sa=X&ved=2ahUKEwiC3cLZ0JLgAhXHUxUIHQrsBC4Q1QIoAHoECAUQAQ"
    }
  ],
  "organic_results": [{
      "position": 1,
      "title": "10 Best Pizzas in Dublin - A slice of the city for every price point ...",
      "link": "https://www.independent.ie/life/travel/ireland/10-best-pizzas-in-dublin-a-slice-of-the-city-for-every-price-point-37248689.html",
      "domain": "www.independent.ie",
      "displayed_link": "https://www.independent.ie/.../10-best-pizzas-in-dublin-a-slice-of-the-city-for-every-p...",
      "snippet": "Oct 20, 2018 - Looking for the best pizza in Dublin? Pól Ó Conghaile scours the city for top-notch pie... whatever your budget.",
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:wezzRov42dkJ:https://www.independent.ie/life/travel/ireland/10-best-pizzas-in-dublin-a-slice-of-the-city-for-every-price-point-37248689.html+&cd=4&hl=en&ct=clnk&gl=ie",
      "block_position": 2
    }
  ],
  "related_places": [{
    "theme": "Best dinners with kids",
    "places": "Pinocchio Italian Restaurant - Temple Bar, Cafe Topolisand more",
    "images": [
      "https://lh5.googleusercontent.com/p/AF1QipNhGt40OpSS408waVJUHeItGrrGqImmEKzuVbBv=w152-h152-n-k-no"
    ]
  }],
  "pagination": {
    "current": "1",
    "next": "https://www.google.com/search?q=pizza&ei=fRZQXMKqL8en1fAPitiT8AI&start=10&sa=N&ved=0ahUKEwiC3cLZ0JLgAhXHUxUIHQrsBC4Q8NMDCOkB"
  }
}

通过位置搜索

这是将查询地理位置为纽约的Google查询的示例。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the search parameters
$params = [
  "q" => "pizza",
  "location" => "New York,New York,United States"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// pretty-print the JSON result
print_r($result);

?>

搜索Google地点、Google视频、Google图片、Google购物和Google新闻

使用search_type参数搜索Google地点、视频、图片和新闻。有关每个搜索类型可用的其他参数的完整详细信息,请参阅搜索API参数文档

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");


// perform a search on Google News, just looking at blogs, ordered by date, in the last year, filtering out duplicates
$params = [
  "q" => "football news",
  "search_type" => "news",
  "news_type" => "blogs",
  "show_duplicates" => "false",
  "sort_by" => "date",
  "time_period" => "last_year"
];
// retrieve the search results as JSON
$result = $serpwow->json($params);
// pretty-print the JSON result
print_r($result);


// perform a search on Google Places for 'plumber' in London
$params = [
  "q" => "football news",
  "search_type" => "places",
  "location" => "London,England,United Kingdom"
];
$result = $serpwow->json($params);
print_r($result);


// perform an image search on Google Images for "red flowers"
$params = [
  "q" => "red flowers",
  "search_type" => "images"
];
$result = $serpwow->json($params);
print_r($result);

?>

以JSON、HTML和CSV格式返回结果

SerpWow可以使用jsonhtmlcsv方法以JSON、HTML和CSV格式返回数据。对于CSV结果,请使用csv_fields参数(文档)请求特定结果字段。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

/* 
  Set up parameters the query (q) and location parameters
  note that the "location" parameter should be a value
  returned from the Locations API.
  We'll re-use the same params for all 3 examples.
*/
$params = [
  "q" => "pizza",
  "location" => "New York,New York,United States"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);
print_r($result);


// retrieve the Google search results as HTML
$result = $serpwow->html($params);
print_r($result);

// retrieve the Google search results as CSV
$result = $serpwow->csv($params);
print_r($result);

?>

请求移动和平板电脑的结果

要请求SerpWow通过移动或平板电脑浏览器渲染Google搜索结果,请使用device参数

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the mobile params
$paramsMobile = [
  "q" => "pizza",
  "device" => "mobile"
];

// set up the tablet params
$paramsTablet = [
  "q" => "pizza",
  "device" => "tablet"
];

// // set up the desktop params (note we omit the 'device' param)
$paramsDesktop = [
  "q" => "pizza"
];

// retrieve the mobile search results
$result = $serpwow->json($paramsMobile);
print_r($result);

// retrieve the tablet search results
$result = $serpwow->json($paramsTablet);
print_r($result);

// retrieve the desktop search results
$result = $serpwow->json($paramsDesktop);
print_r($result);

?>

解析结果

通过json方法发送请求时,将返回一个对象。您可以检查此字典以迭代、解析并将结果存储在您的应用程序中。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// determine if the request was successful
$success = $result->request_info->success;

if ($success) {
  
  // extract the time taken and number of organic results
  $timeTaken = $result->search_metadata->total_time_taken;
  $organicResultCount = sizeof($result->organic_results);

  // print
  print_r($organicResultCount.' organic results returned in '.$timeTaken.'s');
  
}

?>

分页结果,每页最多返回100个结果

使用pagenum参数在谷歌搜索结果中进行分页。每页返回的最大结果数量(由num参数控制)为100(谷歌强制的限制),适用于所有除谷歌地点以外的search_type,在谷歌地点中最大为20。以下是一个示例。

<?php

// composer autoload
require "vendor/autoload.php";

$numberOfResults = 100;

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza",
  "page" => 1,
  "num" => $numberOfResults
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// print out the number of organic results returned 
print_r(sizeof(result.organic_results).' organic results returned');

?>

包含所有参数的搜索示例

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza",
  "gl" => "us",
  "hl" => "en",
  "location" => "New York,New York,United States",
  "google_domain" => "google.com",
  "time_period" => "custom",
  "sort_by" => "date",
  "time_period_min" => "02/01/2018",
  "time_period_max" => "02/08/2019",
  "device" => "mobile",
  "csv_fields" => "search.q,organic_results.position,organic_results.domain",
  "page" => "1",
  "num" => "100"
];

// retrieve the search results as CSV
$result = $serpwow->csv($params);

// print out the number of organic results returned 
print_r($result);

?>

位置API示例

地点API允许您搜索SerpWow支持的谷歌搜索位置。您可以将地点API返回的full_name作为搜索API查询中的location参数(请参阅上面的使用地点进行搜索示例)以检索定位到该地点的搜索结果。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple locations query
$params = [
  "q" => "mumbai"
];

// retrieve locations matching the query parameters as JSON
$result = $serpwow->locations($params);

// print out the locations returned 
print_r($result);

?>

账户API示例

账户API允许您检查当前的SerpWow使用情况和账单信息。

<?php

// composer autoload
require "vendor/autoload.php";

// create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// get our account info
$result = $serpwow->account($params);

// print out the locations returned 
print_r($result);

?>

批次API

批次API允许您在SerpWow账户中创建、更新和删除批次(批次允许您保存多达15,000次搜索,并让SerpWow按计划运行它们)。

有关更多信息和大面积的代码示例,请参阅批次API文档