Elasticsearch-php composer安装

本文详细介绍如何在PHP环境中安装Elasticsearch-php客户端,包括所需环境配置如PHP版本、Composer、ext-curl及原生JSON扩展的要求。同时,提供解决国内云服务器下composer安装失败的解决方案,以及安装完成后目录结构和基本使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elasticsearch-php 的安装需要满足以下 4 个需求:

    PHP 7.0.0 或更高版本
    Composer
    ext-curl:PHP 的 Libcurl 扩展(详情查看下方注意事项)
    原生 JSON 扩展 (ext-json) 1.3.7或更高版本

 

安装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(); 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值