jan-drda / laravel-google-custom-search-engine
Laravel 包,用于从 Google 自定义搜索引擎 API 获取自定义搜索引擎的结果,支持免费版和付费版。
Requires
- php: >=5.4.0
- illuminate/support: >=5.0.0
README
#Laravel Google Custom Search Engine Laravel package to get Google Custom Search results from Google Custom Search Engine API for both free and paid version.
简要历史
由于 Swiftype 关闭了免费计划,我开始寻找一些不需要太多编码的替代方案,但未能成功。我找到的最好的是 Spatie 的 Google Search 包,用于 Google CSE 付费版本,所以我进行了一些研究,并按类似的方式开发了一个包,但独立于 Google CSE 版本。
为开发者提供咖啡
如果您喜欢这个项目,可以通过购买我一杯咖啡来帮助我获得灵感。 :) https://ko-fi.com/A067ES5
安装
1/ 使用 Composer 安装
composer require jan-drda/laravel-google-custom-search-engine
2/ 将服务提供者添加到 config/app.php
'providers' => [ '...', 'JanDrda\LaravelGoogleCustomSearchEngine\LaravelGoogleCustomSearchEngineProvider' ];
3/ 在 config/app.php 中添加 Facade 的别名
'aliases' => [ ... 'GoogleCseSearch' => 'JanDrda\LaravelGoogleCustomSearchEngine\Facades\LaravelGoogleCustomSearchEngineProvider', ... ]
4/ 发布配置文件
php artisan vendor:publish --provider="JanDrda\LaravelGoogleCustomSearchEngine\LaravelGoogleCustomSearchEngineProvider"
配置
创建您的自定义搜索引擎
- 如果您在 https://#/cse/ 创建引擎,点击“设置”后您将找到 ID
- 只需检查您拥有的 URL,例如 https://#/cse/setup/basic?cx=search_engine_id,cx=后面的字符串就是您的搜索引擎 ID
!! 注意 !! 如果您更改自定义搜索引擎的样式,ID 可能会更改
获取您的 API 密钥
- 访问 https://console.developers.google.com,然后
- 点击 GoogleAPI 标志右侧的菜单,然后点击“创建项目”
- 输入新项目的名称 - 由您决定,您可以使用“Google CSE”
- 等待项目创建完成 - 指示器是右上角围绕铃铛图标的颜色圆圈
- 显示 API 列表 - 搜索“Google 自定义搜索引擎 API”并点击它
- 点击自定义搜索引擎标题右侧的“启用”图标
- 点击左侧菜单下的“Library”部分下的“Credentials”
- 点击“创建凭证”并选择“API 密钥”
- 您的 API 密钥已显示,因此复制并粘贴此处
保存配置值
在您的 config/laravelGoogleCustomSearchEngine.php 中保存搜索引擎 ID 和 API ID
用法
简单用法
创建一个对象并调用 getResults 函数获取前 10 个结果
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
不要忘记使用使用映射命名空间,因此示例控制器将如下所示(以最简方式)
这只是示例控制器名称,您可以使用任何您想要的,这个注意事项主要是针对 Laravel 新手
namespace App\Http\Controllers; use App\Http\Controllers\Controller; use JanDrda\LaravelGoogleCustomSearchEngine\LaravelGoogleCustomSearchEngine; class GoogleSearchController extends Controller { public function index(){ $fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase' } }
您还可以获取有关搜索的信息,如总记录和搜索时间
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase' $info = $fulltext->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 ) $fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->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
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase' $rawResults = $fulltext->getRawResults(); // get complete response from Google
要获取结果数量,请使用
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase' $noOfResults = $fulltext->getTotalNumberOfResults(); // get total number of results (it can be less than 10)
如果您有更多引擎/更多 API 密钥,您可以使用以下函数覆盖配置变量
$fulltext = new LaravelGoogleCustomSearchEngine(); // initialize $fulltext->setEngineId('someEngineId'); // sets the engine ID $fulltext->setApiKey('someApiId'); // sets the API key $results = $fulltext->getResults('some phrase'); // get first 10 results for query 'some phrase'
文档
基本文档将在GitHub Wiki上,目前正在开发中。
许可证
本软件包是开源软件,许可协议为MIT许可证
关于
我是自1997年以来居住在捷克共和国的独立高级软件顾问,从事IT业务。