方法一php
使用coreseek源碼自帶testpack/api/test_coreseek.php代碼,
進行稍微修改就能夠使用了,只不過須要引入」spinxapi.php「類
方法二--製做php擴展
1.安裝sphinx擴展html
1 下載依賴 http://pecl.php.net/package/sphinx 2 解壓並執行如下代碼: 3 /usr/local/php7/bin/phpize 4 ./configure --prefix=/usr/local/libsphinxclient 5 make && make install
2.安裝sphinx擴展(這裏須要根據php版本進行分別安裝)mysql
php5版本git
1 wget http://pecl.php.net/get/sphinx-1.3.3.tgz 2 tar zxvf sphinx-1.3.3.tgz 3 cd sphinx-1.3.3 4 ./configure 5 --with-php-config=/usr/local/php/bin/php-config 6 --with-sphinx=/usr/local/libsphinxclient
7 上面有可能會報錯: 8 php_sphinx_client_handlers.read_property = php_sphinx_client_read_property; ^ 9 make: *** [sphinx.lo] Error 1 10 這個報錯,修改 sphinx.c 第105行爲: 11 retval = std_hnd->read_property(object, member, type TSRMLS_CC, NULL); 12 而後編譯便可經過
php7版本sql
1 下載地址: 2 http://git.php.net/?p=pecl/search_engine/sphinx.git;a=snapshot;h=339e123acb0ce7beb2d9d4f9094d6f8bcf15fb54;sf=tgz ---可能下載下來爲index.html文件,那就下載到本地而後rz上去。 3 4 接下來接着安裝: 5 6 這裏如今的版本爲:sphinx-339e123.tar.gz 8 tar -zxf sphinx-339e123.tar.gz 10 cd sphinx-339e123 12 ./configure 13 --prefix=/usr/local/sphinxextend 14 --with-php-config=/usr/local/php-fpm/bin/php-config 15 --with-sphinx=/usr/local/libsphinxclient/ 16 17 make && make install
以上兩個版本的步驟執行完成後,找到php.ini,在裏面添加extension=spninx.soapi
而後使用php-m 或者看下phpinfo();
能看到sphinx便可。php7
注:執行php腳本以前要先啓動sphinx服務,在shpinx安裝目錄的bin目錄下執行./searchd 啓動sphinx服務php-fpm
例子:post
1 $cl = new SphinxClient (); 2 $cl->SetServer ( '127.0.0.1', 9312); 3 $cl->SetConnectTimeout ( 3 ); 4 $cl->SetArrayResult ( true ); 5 $cl->SetMatchMode ( SPH_MATCH_ANY); 6 $res = $cl->Query ( '祖國', "*" ); 7 8 $ids = ''; 9 foreach ($res['matches'] as $key => $val) { 10 $ids .= $val['id'].','; 11 } 12 $ids = rtrim($ids,','); 13 14 $conn = mysqli_connect("localhost","root","root","post"); 15 mysqli_query($conn,"set names utf8"); 16 $sql = "select * from post_article where id in($ids)"; 17 $res = mysqli_query($conn,$sql); 18 $list = array(); 19 while($row = mysqli_fetch_assoc($res)){ 20 21 $list[] = $row; 22 23 } 24 25 mysqli_free_result($res); 26 27 mysqli_close($conn); 28 29 foreach($list as $v){ 30 31 echo $v['title'].'<br/>'.$v['content'].'<hr>'; 32 33 }