I am using the following code to create a connection to a MongoDB server, so as to create databases and stuff but I keep getting class 'MongoDB\Cient' not found. I really need help.
<?php
$client = new MongoDB\Client('mongodb://localhost:27017');
$db = $client->reach;
$collection = $db->messages;
$collection->insertOne($record);
?>
where $record is some record.
Also, $mng = new MongoDB\Driver\Manager("mongodb://localhost:27017"); connects and I can use it to communicated with any already created db (using the command line). So, I want to ask if MongoDB\Driver\Manager can substitute MongoDB\Client?
After all the struggle, this solved the issue...
installing using composer:
$ composer require "mongodb/mongodb=^1.0.0"
OUTPUT:
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Installing mongodb/mongodb (1.0.0)
Downloading: 100%
Writing lock file
Generating autoload files
Usage:
<?php
require 'vendor/autoload.php'; // include Composer goodies
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
That fixed the issue for me. follow the link:
http://docs.php.net/manual/en/mongodb.tutorial.library.php
Related
I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.
I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.
use Google\Cloud\Storage\StorageClient;
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
That as my code.Here I have installed composer on windows and getting following error:-
Fatal error: Class 'Google\Cloud\StorageClient' not found in C:\xampp\htdocs\fingertips\application\controllers\teacher.php on line 214
And even after running commands with composer to use google cloud api's, then also nothing is happening.
On cmd, when I am running this, "composer require google/cloud-storage" ,I am getting this
Using version ^1.3 for google/cloud-storage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I have run so many commands to fix this but didnt get anything in success.Can somebody please help what went wrong
In regards to your second question, you didn't include the actual error you're seeing.
I can see an issue with this code block though:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$file = fopen($params['book']['tmp_name'], 'r');
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload($params['book']['name'], [
'name' => 'test.pdf'
]);
I'm missing the actual data you want to upload. The upload method needs data to upload. This should work:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload(file_get_contents($params['book']['tmp_name']), [
'name' => 'test.pdf'
]);
See the documentation for more examples of how to go about uploading a file.
Have you installed storage client? as stated here.
https://packagist.org/packages/google/cloud-storage
use
composer require google/cloud-storage
I installed a composer, downloaded the Aura, created an index.pxp and wrote in it:
require('vendor/autoload.php');
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$object = $di->newInstance('Vendor\Package\ClassName');
But phpStorm says:Undefined namespase DI
And i have error: Fatal error: Class 'Aura\Di\ContainerBuilder' not found in... on line 4
I do as follows: http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1
In order for the composer auto-loader to pick up \Aura\Di, the dependency needs to be managed by composer.
You can easily do this by executing
composer require aura/di
which will add the dependency to your composer.json file and register with the auto-loader.
If you have manually downloaded and installed aura/di, you can revert that.
I'm running my php in cli under my user.
I have installed composer and autoloader.php does exist. Under vendor folders and file have been downloaded (autoload.php aws, bin, composer, guzzlehttp, mtdowling and psr)
Now in my php code I do:
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('KEY', 'SECRETKEY');
$client = Route53Client::factory(array(
'credentials' => $credentials
));
When I run the script I get: PHP Fatal error: Class 'Credentials' not found in /home/user/updatedns.php on line 15
I tried running it a sudo also (in case it needs to write to the directory) and still get this error.
Just found out that use Aws\Common\Credentials\Credentials; is now under use Aws\Credentials\Credentials;