How to connect Couchbase for my Symfony project? - php

I couldn't find any good resource/article yet now to connect Couchbase server in my Controller page. I have installed PHP SDK successfully. But my code is not working. Object is not properly called. See my test code here. Object for ex. CouchbaseCluster, openBucket is not getting called properly.
public function getConnection() {
// Connect to Couchbase Server
$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$bucket = $cluster->openBucket('beer-sample');
// Retrieve a document
$result = $bucket->get('aass_brewery-juleol');
$doc = $result->value;
echo $doc->name . ', ABV: ' . $doc->abv . "\n";
}
Please help me out what the namespace or use will be included there.
Thanks in advance for giving some good point or references on Symfony and Couchbase.

The official Couchbase PHP SDK, is not just a PHP library, they distribute their SDK through libcouchbase package and a PECL extension.
The installation process is well documented .
Once installed, make sure the extension is loaded by checking phpinfo.
Assuming that installation went through succesfully, you should be able to use the Classes, methods exposed by the PECL extention.
The issue you are likely facing is that you are using a class from the Global Space in a Namespaced Class.
Observe the \
$cluster = new \CouchbaseCluster('http://127.0.0.1:8091');
mostly will solve the issue you are facing .

Related

Using matriphe/larinfo on Laravel 5.4 project - getting class not found error

I am trying to use this package in a Laravel 5.4 project (php 7.1 x86 on Windows 7 via XAMPP): https://github.com/matriphe/larinfo
As per instruction, I've loaded the package onto my project using:
composer require matriphe/larinfo
Then I've added the service provider in config/app.php like this:
Matriphe\Larinfo\LarinfoServiceProvider::class,
and added the Facade alias like this:
'Larinfo' => Matriphe\Larinfo\LarinfoFacade::class,
In a test route, I've tried to use it like this:
return \Larinfo::getInfo();
When I visit the test route; I am getting this error:
Class 'Larinfo' not found
When I tried to use it like this:
return Matriphe\Larinfo\Larinfo::getInfo();
I get this error:
Non-static method Matriphe\Larinfo\Larinfo::getInfo() should not be called statically
Any ideas why this might not be working? I've already tried running composer dump-auto and it hasn't helped.
How do you use this package? What am I missing?
thanks for your comment. I've updated the README in the package.
For the usage, just simply use the Facade like this.
$larinfo = Larinfo::getInfo(); // For all info
$hostIpinfo = Larinfo::getHostIpinfo(); // For host info
$clientIpinfo = Larinfo::getClientIpinfo(); // For client info only
$serverInfoSoftware = Larinfo::getServerInfoSoftware(); // For server software info only
$serverInfoHardware = Larinfo::getServerInfoHardware(); // For hardware info only
$uptime = Larinfo::getUptime(); // For uptime info only
$serverInfo = Larinfo::getServerInfo(); // For server info only
$databaseInfo = Larinfo::getDatabaseInfo(); // For database info only
Hope it helps!

Issue understanding CouchBase connection with PHP

I just installed CouchBase and the .dll files for XAMPP. I am following their example on how to connect: "Hello Couchbase" https://developer.couchbase.com/documentation/server/current/sdk/php/start-using-sdk.html
This code:
$bucketName = "default";
// Connect to Couchbase Server
$cluster = new CouchbaseCluster("couchbase://127.0.0.1");
$bucket = $cluster->openBucket($bucketName);
gives me:
LCB_AUTH_ERROR: Authentication failed. You may have provided an invalid username/password combination
I have a fresh installation, and following their guide #1, what am I doing wrong? I don't remember setting a password, I went to the CouchBase admin page and I can't find a way to change any password.
I sympathize here -- I'm having the same error (and no, it's not you, the docs have been neglected). Instantiating the CouchbaseCluster doesn't throw the error, it's actually the openBucket() method that causes problems, and for me, the problem was that the bucket didn't exist (it would be nice if the Couchbase error were in the right ballpark instead of bumbling around in irrelevant confusion). Here's some code that worked for me:
$cluster = new CouchbaseCluster('http://127.0.0.1:8091');
$manager = $cluster->manager('MYUSER', 'MYPASSWORD');
$manager->createBucket('new_bucket');
This does throw some errors:
[cb,WARN] (htconfig L:130 I:0) <couchbase:8091> (CTX=0x5601e6fd7e90,bc_http) Got 404 on config stream. Assuming terse URI not supported on cluster
[cb,EROR] (htconfig L:122 I:0) <couchbase:8091> (CTX=0x5601e6fd7e90,bc_http) Got 404 on config stream. Assuming bucket does not exist as we've tried both URL types
[cb,EROR] (htconfig L:142 I:0) <couchbase:8091> (CTX=0x5601e6fd7e90,bc_http) Got non-success HTTP status code 404
But it does work. The Couchbase forums are a better place for these types of questions -- they respond pretty quickly over there.

Pecl uuid error (uuid_make() is not a function)

I've install UUID using pecl and then i add the extension to php.ini.
The extension is correctly loaded but i get the error
PHP Fatal error: Call to undefined function uuid_make()
This is my code :
<?php
$uuid = v4();
echo $uuid;
function v4() {
$context = $uuid = null;
uuid_create($context);
uuid_make($context, UUID_MAKE_V4);
uuid_export($context, UUID_FMT_STR, $uuid);
return trim($uuid);
}
?>
Why i'm getting this error?
If i print the list of available function i don't have neither uuid_make and uuid_export.
Hi search around the web but i always find someone who use uuid_make and uuid_export.
I can't find documentation about this module.
Thanks
The uuid_make and uuid_export are from the OSSP uuid extension, which is completely different from the PECL extension (although they both have a function named uuid_create).
The PECL extension has very little documentation and several limitations (at least from what I can tell scanning the source, e.g., no support for UUID V5). The OSSP extension is complete but must be installed from source.
If you only need to occasionally generate UUIDs, one simple alternative could be installing a uuid utility (e.g., apt-get install uuid) then use something like exec.

Class 'Zend_Oauth_Consumer' not found

I am using ZendOAuth to authenticate with an OAuth authentication. I have installed the package and all it dependencies using composer. But when I try to use this code, it can't find the Zend classes.
I use the following example code to test if the OAuth is working:
$consumer = new Zend_Oauth_Consumer($this->options);
$token = $consumer->getRequestToken();
echo $token;
When I execute the code I get an error telling me that the class Zend_Oauth_Consumer could not be found.
I am sure my composer packages are correctly loaded because all other packages work fine.
Can someone tell me if I am forgetting something? Do I need an extra include or use?
There is no such class in the library. I think you mean:
$consumer = new \ZendOAuth\Consumer($this->options);

Failed to create IO instance of Couchbase

I just change Couchbase 2.0beta for using Views.So,I follow the offical manual to build couchbase php extsion.
First,install a pure RHEL 6.2.Then,install couchbase-server-2.0.0-1723 and all dependent packages from libcouchbase:
libcouchbase2-dummy-2.0.0beta2-1.x86_64
couchbase-server-2.0.0-1723.x86_64
libcouchbase2-2.0.0beta2-1.x86_64
libcouchbase-devel-2.0.0beta2-1.x86_64
libev-4.03-3.el6.x86_64
libevent-1.4.13-1.el6.x86_64
Extract php-ext-couchbase-1.1.0-dp5-centos62-x86_64.tar.gz,copy couchbase.so to /usr/lib64/php/modules/,and edit /etc/php.d/json.ini:
extension=json.so
extension=couchbase.so
Finally,restart HTTP Server.Then,Check couchbase modules is lauch corrent by:
php -m|grep couchbase
and phpinfo() can output couchbase version is 1.1.0-dp5.
All looks well,but I try to run php code:
<?php
$cb = new Couchbase("127.0.0.1:8091",'Administrator','redflag','default');
$cb->set('a',1);
It's wrong:
$ php getview.php
PHP Warning: Couchbase::__construct(): failed to create IO instance in /var/www/html/getview.php on line 2
PHP Warning: Couchbase::set(): unintilized couchbase in /var/www/html/getview.php on line 3
In order to check couchbase setup is right,I open Couchbase GUI by Administrator as username,redflag as password.Then,create new document,use REST API get item or Views.Those things are OK,no problem,except php code.
No other way out,I git from https://github.com/couchbase/php-ext-couchbase. Build new php-couchbase extsion,and try agin.But the problem is the same.
I find someone has the same error on here & this.Unfortunately there was't solutions.
How can I use php-ext-couchbase 1.1dp5 modules?THX.
on a first glance it seems to load the couchbase extension properly. The thing that may be wrong here is how to access your bucket.
If you didn't do any changes, the "default" bucket has no password associated with it and in general the user of the bucket is the bucket name itself. So in the case of the "default" bucket, it would look like this:
$client = new Couchbase("127.0.0.1:8091", "default", "", "");
And if you have a password defined:
$client = new Couchbase("127.0.0.1:8091", "bucketname", "", "password");
Hope this helps,
Michael
Use yum to install libcouchbase2-libevent package can solve this problem.
BTW.While you run yum install libcouchbase-devel,it don't include libcouchbase2-libevent.If you miss that,you also can make & install php-ext-couchbase module with no error.But you can't connect Couchbase in php,as memtion above.

Categories