PHP code example of mofing / opensearch

1. Go to this page and download the library: Download mofing/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/ */

    

mofing / opensearch example snippets


$accessKey = "LTAICZ5bcHEHwg3d";//Access Key ID
$secret = "kFLCYaxTFedSNh9rJcjM3KHBkDoxlc";//Access Key Secret

$host = "http://opensearch-cn-hangzhou.aliyuncs.com";//公网API域名或者内网API域名
$options = [
    'debug' => true
];
//初始化授权
$openSearchClient = new OpenSearchClient($accessKey, $secret, $host, $options);
//初始化搜索客户端
$searchClient = new SearchClient($openSearchClient);
//设置搜索参数
$params = new SearchParamsBuilder();
//设置config子句的start值
$params->setStart(0);
//设置config子句的hit值
$params->setHits(20);
// 指定一个应用用于搜索
$params->setAppName('mofing_shops');
// 指定搜索关键词
$params->setQuery("title:'深圳'");
// 指定返回的搜索结果的格式为json
$params->setFormat("json");
//添加排序字段
//$params->addSort('id', SearchParamsBuilder::SORT_DECREASE);
// 执行搜索,获取搜索结果
$ret = $searchClient->execute($params->build());
// 将json类型字符串解码
$result = $ret->getItems();
//$result = json_decode($ret->result);
print_r($result);