PHP code example of wuding / opensearch
1. Go to this page and download the library: Download wuding/opensearch library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wuding / opensearch example snippets
// 单独定义查询地址便于引用
$search_url = 'https://cpn.red/?q={searchTerms}&client=firefox&src={referrer:source?}&prefix={suggestions:suggestionPrefix?}&index={suggestions:suggestionIndex?}';
return $config = [
'xmlns' => [
'ie' => 1,
'referrer' => 'http://a9.com/-/opensearch/extensions/referrer/1.0/',
],
'short name' => '红券网',
'description' => '搜索淘宝、天猫优惠券',
'tags' => 'shopping coupon',
'contact' => '[email protected] ',
'url' => [
$search_url,
[
'https://cpn.red/suggestions?q={searchTerms}&client=firefox',
'type' => 'application/x-suggestions+json',
],
[
'https://cpn.red/opensearch',
'rel' => 'self',
'type' => 'application/opensearchdescription+xml',
],
],
'moz: search form' => 'https://cpn.red/',
'image' => [
'https://cpn.red/favicon.ico',
'height' => 16,
'width' => 16,
'type' => 'image/x-icon',
],
'input encoding' => 'utf-8',
];
$config = = new \OpenSearch\Generate($config);
return $generate->xml();
$query = $_GET['q'] ?? '';
// 查询数据库搜索建议
$sug = new \Model\SearchSuggestions;
$arr = $sug->list($query);
// 生成 JSON 格式搜索建议列表
$suggestions = new \OpenSearch\Suggestions($arr, $query);
$suggestions->configFile(APP_PATH . '/config/opensearch.php');
// 这里之后会引用 $search_url 生成规范的 Query URLs
return $suggestions->json();
json
{
"autoload": {
"psr-4": {
"OpenSearch\\": "vendor/wuding/opensearch-php/src/"
}
}
}
sh
composer dump-autoload