PHP | CouchbaseCluster not found

I successfully installed Couchbase Server CE 5.0.0-3217 Beta 2 on my localhost macOS 10.12 with PHP 7.0.23. I can create buckets, perform queries… Anyway, I tried to install the Couchbase PHP SDKs as per your documentation but any time I run a simple script as

$myCluster = new \Couchbase\Cluster(‘couchbase://127.0.0.1:8091’);
or
$myCluster = new CouchbaseCluster(‘couchbase://127.0.0.1:8091’);

I get this Error: Class ‘Couchbase\Cluster’ not found

Even class_exists(‘CouchbaseCluster’) and extension_loaded(‘couchbase’) return false.

I installed the SDKs this way:

brew update
brew install libcouchbase
brew install homebrew/php/php70-couchbase

I inserted the strings
extension=couchbase.so
in my php.ini file /usr/local/etc/php/7.0/php.ini

What do I miss?

Could you post output of php -m and php -v?

Hi, thank you. Here’s the output:

====================
php -m
[PHP Modules]
bcmath
bz2
calendar
Core
couchbase
ctype
curl
date
dba
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
json
ldap
libxml
mbstring
mysqli
mysqlnd
odbc
openssl
pcntl
pcre
PDO
pdo_mysql
PDO_ODBC
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib

[Zend Modules]

==========================
php -v
PHP 7.0.23 (cli) (built: Sep 25 2017 08:07:03) ( NTS )
Copyright © 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright © 1998-2017 Zend Technologies

I didn’t yet succeed. Any news from your side?

it says you load extension twoce but it suould be okay and work. are you using the same php binary and config to run your sample?

When I did run the command
$ php --ini

I got
PHP Warning: Module ‘couchbase’ already loaded in Unknown on line 0

So I have just removed the string extension=couchbase.so from my php.ini file and I restarted with
$ sudo apachectl -k restart

and run
$ php --ini

And now I don’t get that warning longer.
Configuration File (php.ini) Path: /usr/local/etc/php/7.0
Loaded Configuration File: /usr/local/etc/php/7.0/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.0/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.0/conf.d/ext-couchbase.ini,
/usr/local/etc/php/7.0/conf.d/ext-igbinary.ini

Then
$ php -i
phpinfo()
PHP Version => 7.0.23

Then
$ whereis php
/usr/bin/php

But the file /usr/bin/php-config is strangely configured to php 5.624 ! It contains

#! /bin/sh
SED="/usr/bin/sed"
prefix="/usr"
datarootdir="/usr/php"
exec_prefix="${prefix}"
version="5.6.24"
vernum=“50624”

Furthermore, the dir /usr/php doesn’t exist.
So maybe the trouble comes from that.

Anyway, just to complete the info, my php sample is the one I copied from the couchbase web page
https://developer.couchbase.com/documentation/server/current/sdk/php/start-using-sdk.html
It is located at: http: // localhost / couchbase dot php and it contains just
(I put the spaces in the URL because I can’t write more than 2 links here).

<?php $myCluster = new CouchbaseCluster('couchbase://127.0.0.1:8091'); // Same error here //$myCluster = new \Couchbase\Cluster('couchbase://127.0.0.1:8091'); // Always false if (class_exists('CouchbaseCluster')) { echo 'CouchbaseCluster exists'; } else{ echo "CouchbaseCluster doesn't exist"; return; } ?>

If I echo phpinfo(); from the file localhost/phpinfo.php
I get
++++++
PHP Version 7.0.23
php-osx.liip dot ch by Liip (originally developed by www.local dot ch)
Darwin leonardo.local 16.0.0 Darwin Kernel Version 16.0.0: Mon Aug 29 17:56:20 PDT 2016; root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64
Build Date Sep 14 2017 09:50:51
Configure Command ‘./configure’ ‘–prefix=/usr/local/php5’ ‘–with-apxs2=/usr/sbin/apxs’ ‘–with-config-file-scan-dir=/usr/local/php5/php.d’ ‘–with-libxml-dir=shared,/usr’ ‘–with-openssl=/usr/local/php5’ ‘–with-zlib=/usr’ ‘–with-zlib-dir=/usr’ ‘–with-gd’ ‘–with-ldap’ ‘–with-xmlrpc’ ‘–enable-exif’ ‘–enable-soap’ ‘–enable-wddx’ ‘–enable-ftp’ ‘–with-iodbc’ ‘–enable-sockets’ ‘–with-bz2=/usr’ ‘–enable-zip’ ‘–enable-shmop’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–enable-sysvmsg’ ‘–enable-mbstring’ ‘–enable-bcmath’ ‘–enable-calendar’ ‘–with-mhash’ ‘–enable-fpm’ ‘–with-mysql=mysqlnd’ ‘–with-mysqli=mysqlnd’ ‘–with-pdo-mysql=mysqlnd’ ‘–enable-pcntl’ ‘–enable-dtrace’ ‘–disable-phpdbg’ ‘–enable-opcache’ ‘–with-icu-dir=/usr/local/php5’ ‘–with-xsl=shared,/usr/local/php5’ ‘–with-imap=shared,…/imap-2007f’ ‘–with-kerberos=/usr’ ‘–with-imap-ssl=/usr/local/php5’ ‘–with-gettext=/usr/local/php5’ ‘–with-curl=shared,/usr/local/php5’ ‘–with-png-dir=/usr/local/php5’ ‘–with-jpeg-dir=/usr/local/php5’ ‘–enable-gd-native-ttf’ ‘–with-freetype-dir=/usr/local/php5’ ‘–with-pgsql=shared,/usr/local/php5’ ‘–with-pdo-pgsql=shared,/usr/local/php5’ ‘–with-mcrypt=shared,/usr/local/php5’ ‘–with-tidy=/usr/local/php5’ ‘–with-gmp=shared,/usr/local/php5’ ‘–with-readline=shared,/usr/local/php5’ ‘–with-libxml-dir=/usr/local/php5’ ‘CFLAGS=-Os ‘-mmacosx-version-min=10.10’ ‘-I/usr/local/php5/include’ ‘-arch’ ‘-no-cpp-precomp’ ‘-DENTROPY_CH_ARCHS=’’‘i386/x86_64’’’’ ‘-DENTROPY_CH_RELEASE=1’’ 'YACC=/usr/local/Cellar/bison/3.0.2/bin/bison ‘-y’'
Server API Apache 2.0 Handler
Virtual Directory Support disabled
Configuration File (php.ini) Path /usr/local/php5/lib
Loaded Configuration File /usr/local/php5/lib/php.ini
Scan this dir for additional .ini files /usr/local/php5/php.d
Additional .ini files parsed /usr/local/php5/php.d/10-extension_dir.ini, /usr/local/php5/php.d/20-extension-opcache.ini, /usr/local/php5/php.d/40-curl.ini, /usr/local/php5/php.d/40-openssl.ini, /usr/local/php5/php.d/50-extension-apcu.ini, /usr/local/php5/php.d/50-extension-curl.ini, /usr/local/php5/php.d/50-extension-gmp.ini, /usr/local/php5/php.d/50-extension-igbinary.ini, /usr/local/php5/php.d/50-extension-imap.ini, /usr/local/php5/php.d/50-extension-intl.ini, /usr/local/php5/php.d/50-extension-mcrypt.ini, /usr/local/php5/php.d/50-extension-memcached.ini, /usr/local/php5/php.d/50-extension-mongodb.ini, /usr/local/php5/php.d/50-extension-mssql.ini, /usr/local/php5/php.d/50-extension-pdo_pgsql.ini, /usr/local/php5/php.d/50-extension-pgsql.ini, /usr/local/php5/php.d/50-extension-propro.ini, /usr/local/php5/php.d/50-extension-raphf.ini, /usr/local/php5/php.d/50-extension-readline.ini, /usr/local/php5/php.d/50-extension-redis.ini, /usr/local/php5/php.d/50-extension-solr.ini, /usr/local/php5/php.d/50-extension-ssh2.ini, /usr/local/php5/php.d/50-extension-xdebug.ini, /usr/local/php5/php.d/50-extension-xsl.ini, /usr/local/php5/php.d/60-extension-pecl_http.ini, /usr/local/php5/php.d/99-liip-developer.ini
++++++

I can’t upload the whole document because I am new here and I can’t upload attachments.
Please feel free to ask me for any further information.
Thank you.

your apache server seems to be using different php. could you leave only one php interpreter in the system or at least make sure your apache server uses php which has couchbase extension?

I have one only php, now upgraded to 7.1. Same trouble.
I should have done the installation in 2 minutes, while I’m struggling myself for 2 days now.
It’s not a good user experience.
Clearly the Couchbase docs explaining how to install the PHP SDK are incorrect, at least, in my case they didn’t work.
I hope to fix this trouble soon.

The docs are correct. Your apache is loading wrong config:

Loaded Configuration File	/usr/local/php5/lib/php.ini

While your CLI test using correct:

Loaded Configuration File: /usr/local/etc/php/7.0/php.ini

And now you said you have installed 7.1. I suspect you are not uninstall them properly

this is because you still use php 5.6 for apache http server. there is nothing wrong with Couchbase SDK if your system misconfigured.

As you can see on these 2 web pages:



My system is not misconfigured. There are 2 php.ini files on macOS. One for the server and one for the CLI.
That’s the default installation on Mac, php built-in, and Couchbase should know that.

$ brew unlink php56
Error: No such keg: /usr/local/Cellar/php56

$ brew uninstall php56
Error: No such keg: /usr/local/Cellar/php56

$ brew unlink php70
Error: No such keg: /usr/local/Cellar/php70

$ brew uninstall --ignore-dependencies php70
Error: No such keg: /usr/local/Cellar/php70

$ php -v
PHP 7.1.9 (cli) (built: Sep 14 2017 10:05:35) ( NTS )

phpinfo();
Configuration File (php.ini) Path /usr/local/php5/lib
Loaded Configuration File /usr/local/php5/lib/php.ini
Scan this dir for additional .ini files /usr/local/php5/php.d

$ php --ini
Configuration File (php.ini) Path: /usr/local/php5/lib
Loaded Configuration File: /usr/local/php5/lib/php.ini
Scan for additional .ini files in: /usr/local/php5/php.d

Please note, the dir
/usr/local/php5
is redirected to
/usr/local/php5-7.1.9-20170914-100859

So, now apache and cli load the same /usr/local/php5/lib/php.ini, probably automatically, after the php71 installation.
Please note that’s a standard installation on macOS, “as is”.
And the file
/usr/local/php5/lib/php.ini
redirected to
/usr/local/php5-7.1.9-20170914-100859/lib/php.ini
as I wrote it, contains the line “extension=couchbase.so

Even the file
/usr/local/etc/php/7.1/php.ini
as I wrote it, contains the line "extension=couchbase.so"
But I still get the inital fatal error: Class ‘Couchbase\Cluster’ not found

If we really need to tell php to load
/usr/local/etc/php/7.1/php.ini
how to change that path once and forever such a way any further installation relies on that path?
Thank you.

also you cannot use same extension binary for different php versions. it have to be compiled and installed properly.

why are you trying to use php 5.6 and php 7.1 at the same time and mixing the config? the extension compiled for php 7.1 can be only installed for this version and cannot be loaded into php 5.6.

make sure you have compiled extension for the interpreter you are going to use

apache and php 5.6 are built in in the macOS 10.12.
So I didn’t do anything but just turn on apache with
$ sudo apachectl start
Then I installed mysql and my project localhost+php+mysql+html+css+js has been working for a year like a charm.

php and httpd were NOT in the /usr/local/Cellar folder.
$ brew unlink php56
Error: No such keg: /usr/local/Cellar/php56

$ brew uninstall php56
Error: No such keg: /usr/local/Cellar/php56

$ brew unlink httpd
Error: No such keg: /usr/local/Cellar/httpd

$ brew uninstall httpd
Error: No such keg: /usr/local/Cellar/httpd

Instead
$ brew install libcouchbase
$ brew install homebrew/php/php70-couchbase
both install within /usr/local/Cellar/
and don’t talk with the original httpd and php56.

Then I made some mess trying to install
$ brew install php71
which created /usr/local/Cellar/php71
As I wrote above, "No such keg: /usr/local/Cellar/php56"
So I don’t know how to remove the built-in php56.

I don’t know well what’s happening here but I guess that the built-in httpd and php56 are creating conflicts with the latest items installed within /usr/local/Cellar/
I can’t find a solution. Don’t know how to remove the built-in apache and php56. I’m not a gem with these stuffs.
I expect that I launch a command to install a lib and everything works in a few seconds.
So, Couchbase and the built-in apache/php on macOS 10.12 don’t talk each other. It’s a fact.

why don’t you install couchbase extension into this php then? if everything worked before with php 5.6? couchbase extension can be compiled and tests to work with php 5.6.

How do you decide what PHP version number to write in the command?

Hi, I don’t know how to install couchbase extension into this php. May you please show me the commands? I need that works on both cli and apache with php 7.1. Thank you.

If you cannot configure your local apache+mod_php environment, you can use officially provisioned docker image from php team:

$ docker run -ti --rm php:7.1-apache /bin/bash

And then run inside this docker image:

$ curl http://packages.couchbase.com/ubuntu/couchbase.key | apt-key add -
$ echo 'deb http://packages.couchbase.com/ubuntu jessie jessie/main' > /etc/apt/sources.list.d/couchbase.list
$ apt update && apt upgrade -y

$ apt install -y libcouchbase-dev libcouchbase2-libevent
$ pecl install couchbase

$ echo extension=couchbase.so > /usr/local/etc/php/conf.d/99-couchbase.ini

Now lets check it with apache httpd. The following lines still executed inside the container:

$ echo -e "<?php\nphpinfo();" > /var/www/html/test.php
$ apachectl start
$ curl -s localhost/test.php | sed 's/<[^>]*>//g' | grep couchbase
Additional .ini files parsed /usr/local/etc/php/conf.d/99-couchbase.ini
couchbase
couchbase support enabled 
libcouchbase runtime version 2.8.1 (git: 5a66d8607605bb23e66ed7ae9e7196fe45be6856) 
libcouchbase headers version 2.8.1 (git: 5a66d8607605bb23e66ed7ae9e7196fe45be6856) 
igbinary transcoder disabled (install pecl/igbinary and rebuild pecl/couchbase) 
zlib compressor disabled (install zlib headers and rebuild pecl/couchbase) 
couchbase.decoder.json_arrays00
couchbase.encoder.compressionoffoff
couchbase.encoder.compression_factor0.00.0
couchbase.encoder.compression_threshold00
couchbase.encoder.formatjsonjson
couchbase.log_levelWARNWARN

The last command demonstrates that extension is properly initialized.

If you don’t know how to run docker on macos, here is the instruction: https://docs.docker.com/docker-for-mac/

Thank you. I had Docker.
The istallation was successful, here’s the last command

root@50c56a5cb393:/var/www/html# curl -s localhost/test.php | sed ‘s/<[^>]*>//g’ | grep couchbase
Additional .ini files parsed /usr/local/etc/php/conf.d/99-couchbase.ini
couchbase
couchbase support enabled
libcouchbase runtime version 2.8.1 (git: 5a66d8607605bb23e66ed7ae9e7196fe45be6856)
libcouchbase headers version 2.8.1 (git: 5a66d8607605bb23e66ed7ae9e7196fe45be6856)
igbinary transcoder disabled (install pecl/igbinary and rebuild pecl/couchbase)
zlib compressor disabled (install zlib headers and rebuild pecl/couchbase)
couchbase.decoder.json_arrays00
couchbase.encoder.compressionoffoff
couchbase.encoder.compression_factor0.00.0
couchbase.encoder.compression_threshold00
couchbase.encoder.formatjsonjson
couchbase.log_levelWARNWARN
root@50c56a5cb393:/var/www/html#

Then I have been able to re-run and exit from the image
$ docker images
$ docker run -i -t imageID /bin/bash
exit

Now, how could I use the couchbase class? Inside the image? How?
Actually my php located at /localhost/couchbase.php returns false.

if (class_exists(‘CouchbaseCluster’) !== true) {
echo ‘false - CouchbaseCluster doesn’t exist’;
}

I guess I have to work with a different local host, put a kind of redirection…
Can you tell me how? Thank you.

Make sure the extension still in the container after you exit it

And in general you should deploy your code into container.

root@a41cadbbdcd0:/var/www/html# echo -e "<?php\nvar_dump(new \Couchbase\Cluster('couchbase://localhost'));" > /var/www/html/test.php
root@a41cadbbdcd0:/var/www/html# apachectl start
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
root@a41cadbbdcd0:/var/www/html# curl -s localhost/test.php
object(Couchbase\Cluster)#1 (2) {
  ["connstr"]=>
  string(21) "couchbase://localhost"
  ["authenticator"]=>
  NULL
}

There are a lot of guides how to use docker to deploy PHP application in it. This is one of them https://semaphoreci.com/community/tutorials/dockerizing-a-php-application

Thank you so much for your help, but at the end of the day I can’t deploy my code into the container that way. That’s impractical. I need to edit the php on my local host using an app, save it, and get the results in real time on the browser. That’s the idea when we work on the localhost. Also, which address should I pass to my browser just to see that php echo results?

Anyway, this solution/installation is miles away from the quick one proposed on the couchbase support page, as far as Installing Couchbase php SDK on Mac. https://developer.couchbase.com/documentation/server/current/sdk/php/start-using-sdk.html

After a week spent on this case I evaluated the complexity of this implementation and the effort we should do in order to succeed. So since we have a very short time to deploy our project, I will postpone the couchbase implementation to a later time. Hopefully.

Thank you for you kind and accurate support.