iblank/laravel-gov-search

此Laravel包集成了美国政府搜索API。请使用配置文件添加您的API令牌和联盟。

v0.1 2015-04-14 13:26 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:02:11 UTC


README

Laravel PHP Facade/Wrapper for the USA.gov, DigitalGov Search API. You will need to create an account with DigitalGov to create an affiliate id and access token key: http://search.digitalgov.gov/

安装

  1. 将项目添加到您的composer.json文件中的require数组

     "require": {
         "iblank/laravel-gov-search": "dev-master"
     }
    
  2. 运行composer update以拉取包的最新版本。

  3. 打开app/config/app.php*并在您的providers数组中添加服务提供者

     'providers' => array(
         'iblank\GovSearch\GovSearchServiceProvider'
     )
    

注意:如果您使用环境配置,请确保将服务提供者放置在相应的环境app.php文件中。

配置

对于Laravel 5

运行php artisan vendor:publish并在文件中设置您的API密钥和联盟ID

/app/config/govsearch.php

对于Laravel 4

运行php artisan config:publish iblank/laravel-gov-search并在文件中设置您的API密钥和联盟ID

/app/config/packages/iblank/laravel-gov-search/config.php

默认选项

可选地使用配置文件中的DEFAULTS数组更改搜索请求的默认值

'DEFAULTS' => array(
    'highlight' => true,
    'limit' => 20, // 1 to 999
    'sort' => 'relevance' // or 'date'
)

用法

/**
 * Returns PHP object of search results
 * @param $search (string) - what to search for
 * @param $options (array) - set an 'offset' index and/or override any of the defaults set in the config file
 * @return (object) - PHP object (details below)
 */
$apiResult = GovSearch::search($search, $options);

返回对象的示例格式

{
    "total": 356,
    "next_offset": 20,
    "spelling_correction": null,
    "results": [
        {
            "title": "sample title",
            "url": "http://www.anyplace.com",
            "snippet": "sample snippet",
            "publication_date": "2014-11-24"
        }
    ]
}

基本搜索分页示例

// Set Defaults
$search = 'Medal of Honor';
$options = array(
    'offset' => 0,
    'limit' => 25
);

// Make initial call
$apiResult = GovSearch::search($search, $options);

// Set total results
$totalResults = $apiResult['total'];

// Set total pages
$totalPages = ceil($totalResults / $options['limit']);

// Using this example, with 77 total results, would give you 4 pages
// If you want to go to page 3 of 4...
$page = 3;
$options['offset'] = ($page - 1) * $options['limit'];
$apiResults = GovSearch::search($search, $options);

致谢

基于Alaouy的代码Youtube Laravel Facade/Wrapper构建。