aleszatloukal/google-search-api

Laravel 包,用于从 Google Custom Search Engine API 获取 Google 定制搜索结果,包括免费和付费版本。

1.0.0 2019-12-11 11:03 UTC

This package is not auto-updated.

Last update: 2024-09-26 12:52:39 UTC


README

GitHub version GitHub license Packagist

Google API 搜索引擎

Laravel 包,用于从 Google Custom Search Engine API 获取 Google 定制搜索结果,包括免费和付费版本。

安装

aleszatloukal/google-search-api 添加到 composer.json

"aleszatloukal/google-search-api": "~1.0.0"

运行 composer update 以获取最新版本。

或者运行

composer require aleszatloukal/google-search-api

现在打开 /config/app.php 并将服务提供者添加到 providers 数组中。

'providers' => [
    AlesZatloukal\GoogleSearchApi\GoogleSearchApiProvider::class,
]

现在添加别名。

'aliases' => [
    'GoogleSearchApi' => AlesZatloukal\GoogleSearchApi\Facades\GoogleSearchApi::class,
]

配置

运行 php artisan vendor:publish --provider="AlesZatloukal\GoogleSearchApi\GoogleSearchApiProvider" 并使用您自己的信息修改配置文件。

/config/googlesearchapi.php

在 Laravel 5 中,编辑 config.php 文件很简单 - 事实上,您甚至不需要修改它!只需将以下内容添加到您的 .env 文件中,即可开始使用

GOOGLE_SEARCH_ENGINE_ID=
GOOGLE_SEARCH_API_KEY=

创建您的自定义搜索引擎

  1. 如果您在 https://#/cse/ 创建引擎,您可以在点击设置后找到 ID
  2. 只需检查您的 URL,例如 https://#/cse/setup/basic?cx=search_engine_id,并在 cx= 后面的字符串就是您的搜索引擎 ID

!! 注意 !! 如果您更改自定义搜索引擎的样式,ID 可能会更改

获取您的 API 密钥

  1. 转到 https://console.developers.google.com,然后
  2. 点击 Google API 标志右侧的菜单并点击 '创建项目'
  3. 输入新项目的名称 - 由您决定,您可以使用 'Google CSE'
  4. 等待项目创建 - 指示器是右上角围绕铃铛图标的颜色圆圈
  5. 显示 API 列表 - 搜索 'Google Custom Search API' 并点击它
  6. 点击自定义搜索 API 标题右侧的 '启用' 图标
  7. 点击左侧菜单下 '库' 部分下的 '凭据'
  8. 点击 '创建凭据' 并选择 'API 密钥'
  9. 您的 API 密钥已显示,因此复制并粘贴到这里

用法

简单用法

创建一个对象并调用 getResults 函数以获取前 10 个结果

$googleSearch = new GoogleSearchApi(); // initialize

$results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase' 

不要忘记使用 ,将命名空间映射,因此示例控制器如下(以最简方式)

这只是示例控制器名称,您可以使用任何想要的,这个通知主要是针对 Laravel 新手

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use AlesZatloukal\GoogleSearchApi\GoogleSearchApi;

class GoogleSearchController extends Controller {
    
    public function index() {
        $googleSearch = new GoogleSearchApi(); // initialize

        $results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase'// 
    }
}

您还可以获取有关搜索的信息,如总记录和搜索时间

$googleSearch = new GoogleSearchApi(); // initialize

$results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase' 
$info = $googleSearch->getSearchInformation(); // get search information

高级用法

您可以使用 Google 支持的任何参数。参数列表在此:https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters

例如,您想获取下一个 10 个结果

$parameters = array(
    'start' => 10, // start from the 10th results,
    'num' => 10 // number of results to get, 10 is maximum and also default value
);

$googleSearch = new GoogleSearchApi(); // initialize

$results = $googleSearch->getResults('some phrase', $parameters); // get second 10 results for query 'some phrase'

您还可以获取来自 Google 的原始结果,包括其他信息。响应变量的完整列表在此:https://developers.google.com/custom-search/json-api/v1/reference/cse/list#response

$googleSearch = new GoogleSearchApi(); // initialize

$results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase'
$rawResults = $googleSearch->getRawResults(); // get complete response from Google

仅使用获取结果数量

$googleSearch = new GoogleSearchApi(); // initialize

$results =  $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase'
$noOfResults = $googleSearch->getTotalNumberOfResults(); // get total number of results (it can be less than 10)

如果您有更多引擎/更多 API 密钥,您可以使用以下函数覆盖配置变量

$googleSearch = new GoogleSearchApi(); // initialize

$googleSearch->setEngineId('someEngineId'); // sets the engine ID
$googleSearch->setApiKey('someApiId'); // sets the API key

$results =  $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase'

许可协议

本软件包是开源软件,根据 MIT许可证 许可