Couchbase Client for php not work - php

I download the source code from https://github.com/couchbase/php-ext-couchbase. I then I compile it in ubuntu use the following command .
$ phpize
$ ./configure
$ make
$ make test.
Then I got a couchbase.so in module , then I include the couchbase.so in php.ini. Then, restart the Apache. However, where I check the server with phpinfo(), I cannot find any couchbase module is installed.
Then when I run
$myCluster = new CouchbaseCluster('couchbase://couchbase:8091');
$cb = new Couchbase("couchbase:8091", "testbucket",
"testpassword", "test_bosh_bucket");
In apache log , I found that the error message is CouchbaseCluster Class is not found. And Couchbase class is not found. It seems PHP does not load the couchbase.so. Since I cannot find the couchbase in phpinfo() and the Class is not found. Anything that I am missed?
Please help. Thank,s

Install http://docs.couchbase.com/developer/c-2.4/download-install.html
I'm use:
<?php
$cluster = new CouchbaseCluster("192.168.0.90:8091", "Administrator", "password", "default");
$db = $cluster->openBucket('default');
for ($i = 1; $i <= 400; $i++) {
$DocumentName = "document_name_" . substr(md5(rand()), 0, 6);
$res = $db->insert($DocumentName, array("user_id" => md5(rand())));
var_dump($res);
}
?>

You need to use DLLs that match your PHP version. Please refer to below link. Use the Thread Safe version.
http://pecl.php.net/package/couchbase/1.1.5/windows
Then, use the following code.
$cb = new Couchbase("127.0.0.1:8091", "", "", "beer-sample");
$cb->set("foo", "bar");
var_dump("foo");
Note: Don't use accounts with admin permissions to perform data transactions. Either pass NULL as the username or name of the bucket.

Related

Error GRPC Spanner Google Cloud With PHP

I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini

PHP and MongoDB

I am trying to connect to MongoDB via PHP.
mongod --version
db version v3.2.8
Then,
php -i | grep mongo
/etc/php/7.0/cli/conf.d/20-mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.8
mongodb stability => stable
libmongoc version => 1.3.5
mongodb.debug => no value => no value
I tried:
sudo pecl install mongodb
which returns
pecl/mongodb is already installed and is the same as the released version 1.1.8
install failed
which (the install failed bit) gets redressed if I try:
sudo pecl uninstall mongodb
and
sudo pecl install mongodb
I have this php file:
<?php
echo "I am here";
$connection = new Mongo('localhost');
$db = $connection->mydb;
$list = $db->listCollections();
foreach ($list as $collection) {
echo "$collection </br>";
}
echo "I am never here";
?>
I cannot see the second echo.
I would appreciate any thoughts.
Thank you.
First, check #sanjay comment and then apply this connection:
$db = new MongoClient('mongodb://localhost', [
'username' => 'root',
'password' => '',
'db' => 'YOUR DB'
]);
Hope it will work for you.
I managed to have it working. Thanks to this post:
After upgrading PHP to version 7, why can't I use the mongodb driver?
and this post:
Using the PHP Library for MongoDB (PHPLIB)
Sorry as it seems pretty odd (to me!) but indeed: MongoClient() has become (verbatim! with the slash!) MongoDB\Client().
Hence, the file I had in my original post becomes:
<?php
require 'vendor/autoload.php';
echo "I am here";
$manager = new MongoDB\Client();
$database = $manager->mydb;
foreach ($database->listCollections() as $databaseInfo) {
var_dump($databaseInfo);
}
echo "I managed to arrive here";
?>
and I can see the contents of mydb as well as the 2 echos. The vendor/autoload.php was generated by
sudo composer require "mongodb/mongodb=^1.0.0"
as I ran it from within the directory of the php file above. Probably not the best idea as I, now, have to resolve how to make the autoload.php (and all it carries with) globally available. But, nonetheless, at least, I managed to arrive somewhere.
I am using: PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
under: Ubuntu 16.04.1 LTS

How to install PECL HTTP extension for XAMPP?

I want to install the PHP PECL HTTP extension in my XAMPP environment (OS is Windows). I have attempted to add multiple variations of the php_http.dll extension into my ext directory, and added extension=php_http.dll to the php.ini file. Yet when I go to start the Apache service, it throws some sort of error.
It's pretty clear I'm doing something wrong, however I have no idea what. The last relevant question I could find was 5 years out of date. Does anybody have any idea how to install this?
You can try:
For pecl.
Look in xampp\php for the pecl.bat file
Open a command console window in this folder and issue the pecl.bat command and it will give a list of commands to use.
You also have to load raphf and propro:
http://windows.php.net/downloads/pecl/releases/raphf/1.0.4/
http://windows.php.net/downloads/pecl/snaps/propro/1.0.0/
... and iconv, hash and spl.
This post shows how to install XAMPP on Windows to run PHP applications that connect to a remote Oracle Database.
*
XAMPP is an open source package that contains Apache, PHP and many PHP
'extensions'. One of these extension is PHP OCI8 which connects to
Oracle Database.
*
To install XAMPP: D: drive.
Download "XAMPP for Windows" and follow the installer wizard. I installed into my D: drive.
Start the Apache server via the XAMPP control panel.
Visit http://localhost/dashboard/phpinfo.php via your browser to see the architecture and thread safety mode of the installed PHP. Please note this is the architecture of the installed PHP and not the architecture of your machine. It’s possible to run a x86 PHP on an x64 machine.
Oracle OCI8 is pre-installed in XAMPP but if you need a newer version you can download an updated OCI8 PECL package from pecl.php.net.
Pick an OCI8 release and select the DLL according to the architecture and thread safety mode. For example, if PHP is x86 and thread safety enabled, download "7.2 Thread Safe (TS) x86". Then replace "D:\xampp\php\ext\php_oci8_12c.dll" with the new "php_oci8_12c.dll" from the OCI8 PECL package.
Edit "D:\xampp\php\php.ini" and uncomment the line "extension=oci8_12c". Make sure "extension_dir" is set to the directory containing the PHP extension DLLs. For example,
extension=oci8_12c
extension_dir="D:\xampp\php\ext"
Download the Oracle Instant Client Basic package from OTN.
Select the correct architecture to align with PHP's. For Windows x86 download "instantclient-basic-nt-12.2.0.1.0.zip" from the Windows 32-bit page.
Extract the file in a directory such as "D:\Oracle". A subdirectory "D:\Oracle\instantclient_12_2" will be created.
Add this subdirectory to the PATH environment variable. You can update PATH in Control Panel -> System -> Advanced System Settings -> Advanced -> Environment Variables -> System Variables -> PATH. In my example I set it to "D:\Oracle\instantclient_12_2".
Restart the Apache server and check the phpinfo.php page again. It shows the OCI8 extension is loaded successfully.
If you also run PHP from a terminal window, make sure to close and reopen the terminal to get the updated PATH value.
To run your first OCI8 application, create a new file in the XAMPP document root "D:\xampp\htdocs\test.php". It should contain:
<?php
 
error_reporting(E_ALL);
ini_set('display_errors', 'On');
 
$username = "hr";                  // Use your username
$password = "welcome";             // and your password
$database = "localhost/orclpdb";   // and the connect string to connect to your database
 
$query = "select * from dual";
 
$c = oci_connect($username, $password, $database);
if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}
 
$s = oci_parse($c, $query);
if (!$s) {
    $m = oci_error($c);
    trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
    $m = oci_error($s);
    trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
}
 
echo "<table border='1'>\n";
$ncols = oci_num_fields($s);
echo "<tr>\n";
for ($i = 1; $i <= $ncols; ++$i) {
    $colname = oci_field_name($s, $i);
    echo "  <th><b>".htmlspecialchars($colname,ENT_QUOTES|ENT_SUBSTITUTE)."</b></th>\n";
}
echo "</tr>\n";
 
while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "<td>";
        echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):" ";
        echo "</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";
 
?>
You need to edit this file and set your database username, password and connect string. If you are using Oracle Database XE, then the connect string should be "localhost/XE".
The SQL query can also be changed. Currently it queries the special DUAL table, which every user has.
Load the test program in a browser using http://localhost/test.php. The output will be the single value "X" in the column called "DUMMY".
---- NOTE
Maybe this can help, but this issue is very complex in XAMPP. I have heard several reports about this problem in xampp, I advise testing VPS free for further study.
if you have any questions, post in the comments.
If something is incompatible or wrong, be sure to comment! thanks

PECL_HTTP not recognised php ubuntu

I am trying to use the HttpRequest class which should be in the PECL_HTTP extension of php.
I have php5 and used the following to install pecl_http.
sudo pecl install pecl_http
pecl/pecl_http is already installed and is the same as the released version 2.0.1
install failed
After that i enter into my php.ini:
[PHP]
extension=http.so
[...]
Then i restart my apache2 server, and try using the HttpRequest class, but it gives me the following error:
PHP Fatal error: Class 'HttpRequest' not found
What are possible error's i might have missed?
UPDATE: (Changed the title)
The Extension is not shown in phpinfo(), i set
extension=http.so
And checked if the http.so file is in my extension_dir. I really don't know how to make php recognise the extension.
UPDATE 2:
I managed to install extension, but the class still does not exist.
For others, i had to reference the other extensions pecl_http needs. (For me: propro.so, raphfr.so)
UPDATE 3:
I did not manage to make the Class visible, the answers below show some approaches with other classes.
I solved this issue by using CURL.
You have installed the new version of the extension which uses a completely different API. I still don't know how it works, but I will update my answer one I know. The Documentation of the new version is to be found at http://devel-m6w6.rhcloud.com/mdref/http.
To Install the old version, first uninstall the new verion and then execute
pecl install http://pecl.php.net/get/pecl_http-1.7.6.tgz
UPDATE: here are two examples from the documentation, both should work well:
one request (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/enqueue):
<?php
(new http\Client)->enqueue(new http\Client\Request("GET", "http://php.net"),
function(http\Client\Response $res) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
return true; // dequeue
})->send();
multiple requests (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/once):
<?php
$client = new http\Client;
$client->enqueue(new http\Client\Request("HEAD", "http://php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pecl.php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pear.php.net"));
printf("Transfers ongoing");
while ($client->once()) {
// do something else here while the network transfers are busy
printf(".");
// and then call http\Client::wait() to wait for new input
$client->wait();
}
printf("\n");
while ($res = $client->getResponse()) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
$res->getResponseCode());
}
In both examples you can use $res->getBody() to return the body of the response.
I couldn't test these examples but I have heard of other people that they work.
I had the same problem and solved it by arrange the order in wich extensions are loaded in php.ini
All my other extensions was loaded using their own .ini files in /etc/php5/mods_available.
In my apache2 error.log I noticed that json_decode was needed by http.so to load.
I created a file for http (http.ini) and a symlink in /etc/php5/apache/conf.d with a higher prefix than json.
my http.ini
; configuration for php http module
; priority=50
extension=raphf.so
extension=propro.so
extension=http.so
use the new pecl_http.2.0.6 like this
new http\Message();
or extend in your own class as extends http\Message

php sftp seg fault

What i need to do if next code gives me seg fault ?
$handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
$file = readdir($handle);
closedir($handle);
where $sftp is
$this->_connection = ssh2_connect($this->_server, 22);
if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
} else {
ssh2_auth_password($this->_connection,$this->_user,$this->_password);
}
$sftp = ssh2_sftp($this->_connection);
Connect work well. and segfault is when only i use readdir function.
I'm answering this old question due to high ranking on Google search. I was experiencing the same segmentation fault and the real solution wasn't here.
It turns out that since PHP 5.6, the behavior of the function ssh2_sftp changed and is now returning a resource that doesn't allow to be concatenated as a string to form filesystem paths.
So, once you have the return of the ssh2_sftp, you have first to pass it through intval in order to build the remote file path:
$sftp = ssh2_sftp($this->_connection);
$handle = opendir('ssh2.sftp://' . intval($sftp) . '/usr/bin/');
$file = readdir($handle);
closedir($handle);
You are welcome.
I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3
sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3
It worked for me after some problems.
Regards
A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. You should file a bug against the extension in question.
Don't forget to include a short, reproducible example. Since this may be only reproducible with your specific combination of ssh client and server, you should reproduce the bug first in a brand-new VM and record every step you make, like this:
Install Ubuntu 11.04 amd64 in the VM
Install ssh with $ sudo apt-get install ssh
Configure ssh with ...
Install php with $ sudo apt-get install php5-cli
Copy the script [link to http://pastebin.com/ here] to the VM
Type php ssh-segfault.php and receive a segfault.
Not applicable to the OP's situation, but if you fail to call closedir explicitly on the handle created by opendir, you'll also get a segfault during PHP internal cleanup at script end.
I had a similar problem, though with some additional factors. I'll start with the solution: use absolute paths, rather than relative. And avoid the ~.
As for my findings. Take this script:
#!/usr/bin/php
<?php
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
unset($res); unset($ssh);
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
Results in an authentication error in the 2nd ssh2_auth_pubkey_file call. A bit strange to encounter, after two identical calls - but easy enough to analyze.
But having the rest of my application code around it would result in the segfault. Eventually I narrowed it down to that that including a file, that defines a function that uses the __FILE__ constant changes the error to turn into a segmentation fault.
Even a file as simple as this:
<?php
function nothing() {
error_log(__FILE__);
}
I hope this helps somebody else at some point...
One of the answers above suggests you need to cast the resource to an int intval($sftp) to avoid the segfault.
While I believe this used to be the case, this now appears to have inverted, at least on php 7.1.9 and casting to an int now causes the segfault, at least with file_put_contents:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . intval($sftp) . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://2675/var/file.text
Segmentation Fault
Without a cast it works:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . $sftp . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://Resource id #2675/var/file.text

Categories