Elasticsearch-php 的安装需要满足以下 4 个需求:
安装composer命令
curl -s http://getcomposer.org/installer | php
如果遇到以下错误,
Failed to decode zlib stream
则执行
yum install epel-release
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum update
mv composer.phar /usr/bin/composer
执行composer -v命令,验证是否安装成功
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.10.0 2020-03-10 14:08:05
composer安装成功之后,在cd到项目根目录,安装Elasticsearch-php
执行以下命令,进行安装
composer require elasticsearch/elasticsearch
如果使用的国内的云服务器,大概率会遇到以下报错:
The "https://repo.packagist.org/packages.json" file could not be downloaded: failed to open stream: Connection timed out
https://repo.packagist.org could not be fully loaded, package information was loaded from the local cache and may be out of date
我们需要更换composer镜像地址,则执行:
composer -g config repo.packagist composer https://packagist.phpcomposer.com
然后再执行安装命令即可
Elasticsearch-php安装完成,会在当前目录生成一下目录和文件
composer.json
composer.lock
vendor/
使用脚本示例
<?php
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();