拉德沃伊/searcher

统一搜索API,支持Bing和Google驱动程序

v0.4.0 2017-04-21 18:01 UTC

This package is auto-updated.

Last update: 2024-09-14 02:56:00 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

统一搜索API,用于Google、Bing以及未来可能的其他搜索引擎。使用Guzzle进行http请求。

目标

  • 使用Google / Bing / 可能的其他Web搜索引擎API,无论底层使用的是哪个。

要求

  • PHP >= 7.0
  • guzzlehttp/guzzle

安装

composer require radowoj/searcher

使用

use GuzzleHttp\Client as GuzzleClient;
use Radowoj\Searcher\SearchProvider\Bing;
use Radowoj\Searcher\SearchProvider\Google;
use Radowoj\Searcher\Searcher;

$client = new GuzzleClient();

// 
// If you want to use Google
//
$searchProvider = new Google(
    $client,
    'your-google-api-key',
    'your-custom-search-cx-value-set-in-google-account-panel'
);

//
// Or if you want to use Bing
//
$searchProvider = new Bing(
    $client,
    'your-bing-api-key'
);

//
// Rest of the necessary code is the same regardless of search provider used
//

$searcher = new Searcher($searchProvider);

$results = $searcher->query('"nyan cat"')
    ->limit(10)
    ->offset(0)
    ->find();

//Array access
var_dump(($results[5])->toArray());

//Traversable
foreach($results as $result){
    var_dump($result->toArray());
}

//Countable
var_dump(count($results));

//...and total-result-countable ;)
var_dump($results->totalCount());