PHP7的Sphinx扩展安装及使用方法

## 安装Sphinx扩展 在[http://pecl.php.net/package/sphinx](http://pecl.php.net/package/sphinx)发现sphinx的Available Releases版本中没有支持php7的版本,点击[ [Browse Source](http://git.php.net/?p=pecl/search_engine/sphinx.git) ] 菜单进去发现已经有了适配php7的源码,以此时最新的版本sphinx-9a3d08c.tar.gz为例: wget http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=9a3d08c67af0cad216aa0d38d39be71362667738;sf=tgz tar zxvf sphinx-9a3d08c.tar.gz cd sphinx-9a3d08c phpize ./configure make && make install ./configure 的时候可能出错,提示 checking for libsphinxclient headers in default path... not found configure: error: Cannot find libsphinxclient headers 解决方法如下: 进入coreseek-3.2.14/csft-3.2.14/api/libsphinxclient目录,编译安装libsphinxclient即可 ./configure make && make install 然后再次编译安装sphinx扩展即可,安装完成后修改php.ini,在末尾添加如下内容 extension = sphinx.so 然后重启php-fpm即可,执行php -m,看到有sphinx扩展说明安装成功 ### PHP使用sphinx $sphinx = new SphinxClient; $sphinx->setServer("localhost", 9312); $sphinx->setMatchMode(SPH_MATCH_ANY); //匹配模式 ANY为关键词自动拆词,ALL为不拆词匹配(完全匹配) $sphinx->SetArrayResult ( true ); //返回的结果集为数组 $result = $sphinx->query("test","*"); //星号为所有索引源 $count=$result['total']; //查到的结果条数 $time=$result['time']; //耗时 $arr=$result['matches']; //结果集 $id=''; for($i=0;$i <$count;$i++){ $id.=$arr[$i]['id'].','; } $id=substr($id,0,-1); //结果集的id字符串 注:执行php脚本之前要先启动sphinx服务,在shpinx安装目录的bin目录下执行./searchd 启动sphinx服务
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3