【php elasticsearch】Elasticsearch-PHP requires cURL, or a custom HTTP handler.


問題


當我用 composer 很開心的把 elasticsearch 裝好以後
在php裡寫下了
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = ['localhost:9200'];
$client = ClientBuilder::create()->setHosts($hosts)->build();
?>
接下來卻回我
Fatal error: Uncaught exception 'RuntimeException' with message 'Elasticsearch-PHP requires cURL, or a custom HTTP handler.' in /opt/httpd22/htdocs/maillog/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ClientBuilder.php:142 Stack trace: #0 /opt/httpd22/htdocs/maillog/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ClientBuilder.php(376): Elasticsearch\ClientBuilder::defaultHandler() #1 /opt/httpd22/htdocs/maillog/index.php(5): Elasticsearch\ClientBuilder->build() #2 {main} thrown in /opt/httpd22/htdocs/maillog/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ClientBuilder.php on line 142

DEBUG


從訊息裡面找問題,看來問題是出在 Elasticsearch/ClientBuilder.php:142 附近
直接打開這個檔案進去看
往上幾行以後發現
if (extension_loaded('curl')) {
阿...php 沒開 curl 這個 extension
當初 build 的時候也沒有把這個.so make出來

解決方法


首先回到我的 php src裡面
重新 configure ,這次心一橫,一口氣全弄了
# ./configure --prefix=/opt/php --with-apxs2=/opt/httpd/bin/apxs --with-pear=/opt/pear --with-curl=shared --with-mcrypt=shared --enable-bcmath=shared --enable-mbstring=shared --enable-soap=shared --enable-sysvshm=shared --enable-opcache=shared --with-mysqli=shared --with-mysql=shared --with-snmp=shared
# make -j4 && make install
之後再把 curl.so 加進去 php.ini 的 extension 裡面
extension=/opt/php/lib/php/extensions/no-debug-non-zts-20131226/curl.so
重啟 apache ,搞定
有時候你永遠不知道自己怎麼知道的
多看看然後寫下來絕對沒壞處

留言