教你在laravel中如何使用elaticsearch(步骤分明)

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

教你在laravel中如何使用elaticsearch(步骤分明)

下面由Laravel教程栏目带大家介绍在laravel中如何使用elaticsearch(步骤分明),希望对大家有所帮助!

安装相关扩展包

guzzlehttp/guzzleelasticsearch/elasticsearchlaravel/scoutbabenkoivan/scout-elasticsearch-driverpredis/predis 数据量大需要使用队列同步、拉取数据时安装
1.安装 guzzlehttp/guzzle
composer require guzzlehttp/guzzle
登录后复制在 app/Services 目录下编写 Http 服务类
<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}
登录后复制2.安装 elasticsearch/elasticsearch
composer require elasticsearch/elasticsearch
登录后复制3.安装 laravel/scout
composer require laravel/scoutphp artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
登录后复制4.安装 scout 第三方驱动 babenkoivan/scout-elasticsearch-driver
composer require babenkoivan/scout-elasticsearch-driverphp artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
登录后复制

scout 服务配置,在 env 中增加配置项

// 驱动的host,若需账密:http://es_username:password@127.0.0.1:9200SCOUT_ELASTIC_HOST=elasticsearch:9200// 驱动SCOUT_DRIVER=elastic// 队列配置,数据量大时建议开启SCOUT_QUEUE=true
登录后复制5.安装 predis/predis
composer require predis/predis
登录后复制
初始化 elatic Template

这里以 artisan 命令的方式配置 生成命令文件

php artisan make:command EsInit
登录后复制
<?phpnamespace App\Console\Commands;use App\Services\HttpService;use Illuminate\Console\Command;class EsInit extends Command{  /**   * The name and signature of the console command.   *   * @var string   */  protected $signature = 'es:init';  /**   * The console command description.   *   * @var string   */  protected $description = 'init laravel es for article';  /**   * Create a new command instance.   *   * [@return](https://learnku.com/users/31554) void   */  protected  $http;  public function __construct()  {      parent::__construct();      parent::__construct();      $this->http = new HttpService();  }  /**   * Execute the console command.   *   * [@return](https://learnku.com/users/31554) mixed   */  public function handle()  {      $this->createTemplate();  }  protected function createTemplate()  {      $aData = [          /*          * 这句是取在scout.php(scout是驱动)里我们配置好elasticsearch引擎的index项。          * PS:其实都是取数组项,scout本身就是return一个数组,          * scout.elasticsearch.index就是取          * scout[elasticsearch][index]          * */          'template'=>config('scout.elasticsearch.index'),          'mappings'=>[              'articles' => [                  'properties' => [                      'title' => [                          'type' => 'text'                      ],                      'content' => [                          'type' => 'text'                      ],                      'created_at' => [                          'format' => 'yy-MM-dd HHss',                          'type' => 'date'                      ],                      'updated_at' => [                          'format' => 'yy-MM-dd HHss',                          'type' => 'date'                      ]                  ]              ]          ],      ];      $url = config('scout.elasticsearch.hosts')[0] . '/' . '_template/rtf';      $this->http->put($url,$aData,'json');  }}
登录后复制生成检索 model
php artisan make:model Models/Article
登录后复制创建 model 索引配置文件Elasticsearch\ArticleIndexConfigurator.php
<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}0
登录后复制创建 model 检索规则文件

Elasticsearch\SearchRules\ArticleRule.php

<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}1
登录后复制设置 model Mapping 及检索字段
<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}2
登录后复制使用步骤

生成 elatic Template 类似 mysql 表结构

<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}3
登录后复制

更新 elatic 类型映射

<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}4
登录后复制

数据库数据导入 elatic

<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}5
登录后复制

PS: 其他命令

清空 elatic 数据

<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}6
登录后复制使用检索
<?phpnamespace App\Services;use GuzzleHttp\Client;use GuzzleHttp\Cookie\CookieJar;class HttpService{    protected $client;    public function __construct()    {        $this->client = new Client(['verify' => false, 'timeout' => 0,]);    }    /**     * 发送 get 请求     * @param $url     * @param array $aQueryParam     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function get($url, $aQueryParam = [], $isDecode = true)    {        $response = $this->client->request('GET',            $url,            [                'query' => $aQueryParam            ]);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 post 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function post($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('POST', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     *  发送 put 请求     * @param $url     * @param array $aParam     * @param string $type     * @param string $isDecode     * [@return](https://learnku.com/users/31554) mixed     * @throws \GuzzleHttp\Exception\GuzzleException     */    public function put($url, $aParam = [], $type = 'form_params', $isDecode = true)    {        $aOptions = [];        // Sending application/x-www-form-urlencoded POST        if ($type == 'form_params') {            $aOptions['form_params'] = $aParam;        }        //  upload JSON data        if ($type == 'json') {            $aOptions['json'] = $aParam;        }        $response = $this->client->request('PUT', $url, $aOptions);        if($isDecode){            return \GuzzleHttp\json_decode($response->getbody()->getContents(), true);        }        return $response->getbody()->getContents();    }    /**     * 保存远程文件到本地     * 跟随第三方服务器url重定向     * @param $url     * [@return](https://learnku.com/users/31554) bool|string     */    public function getRemoteFile($url)    {        $jar = new CookieJar();        $aOptions = ['cookies' => $jar];        $response = $this->client->request('GET', $url, $aOptions);        return $response->getBody()->getContents();    }}7
登录后复制其他使用请自行查看文档

以上就是教你在laravel中如何使用elaticsearch(步骤分明)的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:介绍一个高性能、简单、跨平台的 PHP7 代码加密扩展
下一篇:如何使用PHP防止网站被挂马

发表评论

关闭广告
关闭广告