jainsubh/google-search-json-api
Composer 安装命令:
composer require jainsubh/google-search-json-api
包简介
Laravel package provide Google Custom Search JSON and Restricted JSON API lets you develop websites and applications to retrieve and display search results from Programmable Search Engine programmatically.
README 文档
README
Laravel package provide Google Custom Search JSON and Restricted JSON API lets you develop websites and applications to retrieve and display search results from Programmable Search Engine programmatically.
Package Installation
Add jainsubh/google-search-json-api to composer.json.
"jainsubh/google-search-json-api": "~1.0.1"
Run composer update to pull down the latest version.
Or run
composer require jainsubh/google-search-json-api
Now open up /config/app.php and add the service provider to your providers array.
'providers' => [ jainsubh\GoogleSearchApi\GoogleSearchApiProvider::class, ]
Now add the alias.
'aliases' => [ 'GoogleSearchApi' => jainsubh\GoogleSearchApi\Facades\GoogleSearchApi::class, ]
Configuration
Run php artisan vendor:publish --provider="jainsubh\GoogleSearchApi\GoogleSearchApiProvider" and modify the config file with your own information.
/config/googlesearchapi.php
With Laravel 5, it's simple to edit the config.php file - in fact you don't even need to touch it! Just add the following to your .env file and you'll be on your way:
GOOGLE_SEARCH_ENGINE_ID=
GOOGLE_SEARCH_API_KEY=
How to get Search Engine ID
- If you create your engine at https://cse.google.com/cse/ you will find the ID after you click at Settings
- Just check the URL you have like https://cse.google.com/cse/setup/basic?cx=search_engine_id and the string after cx= is your search engine ID
!! Attention !! If you change style of your Custom search engine, the ID can be changed
Get your Google API key
- go to https://console.developers.google.com, than
- click on the menu on the right side of the GoogleAPI logo and click on 'Create project'
- enter the name of the new project - it is up to you, you can use 'Google CSE'
- wait until project is created - the indicator is color circle on the top right corner around the bell icon
- API list is shown - search for 'Google Custom Search API' and click on it
- click on 'Enable' icone on the right side of Custom Search API headline
- click on the 'Credentials' on the left menu under the 'Library' section
- click on the 'Create credentials' and choose 'API key'
- your API key is shown, so copy and paste it here
Usage
Simple usage
Create an object and call the function getResults to get first 10 results
$googleSearch = new GoogleSearchApi(); // initialize $results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase'
Do not forget to map namespace with, so sample controller will look like this (in min. way)
It is only example controller name, you can use whatever you want, this is notice mainly for novices in Laravel
namespace App\Http\Controllers; use App\Http\Controllers\Controller; use jainsubh\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'// } }
You can also get information about the search like total records and search time
$googleSearch = new GoogleSearchApi(); // initialize $results = $googleSearch->getResults('some phrase'); // get first 10 results for query 'some phrase' $info = $googleSearch->getSearchInformation(); // get search information
Advanced usage
You can use any parameter supported at Google. List of parameters is here: https://developers.google.com/custom-search/json-api/v1/reference/cse/list#parameters
E.g. you want to get next 10 results
$parameters = array( 'start' => 10, // start from the 10th results, 'num' => 10 // number of results to get, 10 is maximum and also default value ); $search_type = 'site_restrict'// if empty by default Google Custom Search JSON API would work $googleSearch = new GoogleSearchApi(); // initialize $results = $googleSearch->getResults('some phrase', $parameters, $search_type); // get second 10 results for query 'some phrase'
You can also get the raw result from Google including other information Full list of response variables is available here: 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
For getting the number of results only use
$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)
If you have more engines / more api keys, you can override the config variables with following functions
$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'
jainsubh/google-search-json-api 适用场景与选型建议
jainsubh/google-search-json-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 987 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「google」 「engine」 「laravel」 「custom」 「site_restric」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jainsubh/google-search-json-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jainsubh/google-search-json-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jainsubh/google-search-json-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Abstraction Layer to index and search entities
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Allow KoolReport to use twig template engine to render view
统计信息
- 总下载量: 987
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-19