I recently uploaded a new dependency to a PHP app I am working on and I am now continually getting the following error in Postman
Fatal error: Interface 'Psr\Container\ContainerInterface' not found in
/var/www/html/api/vendor/container-interop/container-interop/src/Interop/Container/ContainerInterface.php on line
13
I have updated composer as well as a multitude of other things and still cannot seem to pinpoint the issue. (Also not sure if it means the error is with the index.php file or the container.php file)
Here is the code from the container.interface.php file
<?php
/**
* #license http://www.opensource.org/licenses/mit-license.php MIT
(see the LICENSE file)
*/
namespace Interop\Container;
use Psr\Container\ContainerInterface as PsrContainerInterface;
/**
* Describes the interface of a container that exposes methods to
read its entries.
*/
interface ContainerInterface extends PsrContainerInterface
{
}
And here is the initial code from my index.php file
<?php
ini_set('display_errors', 1);
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
// Includes ;
require_once( 'config/database.php' );
require_once( 'controller/base.php' );
//$app = new Slim\App();
$app = new Slim\App(['settings' => ['displayErrorDetails' => true]]);
$twilio = new Twilio\Rest\Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
$serviceAccount = ServiceAccount::fromJsonFile('my_file.json');
$firebase = (new Factory)->withServiceAccount($serviceAccount)-
>withDatabaseUri('my_firebase_website')->create();
Solution: I was utilizing filezilla to transfer updated dependencies from my vendor folder on my local machine & in the process I was downloaded the wrong autoload.php file
Related
I've read a dozen of articles, but none provides a proper solution. I have an SSH access to my server and I installed the imagick through composer as follows:
composer require calcinai/php-imagick
The library successfully installed on both my local server (open server) and on the remote server. Then I do:
$imagick = new imagick();
$imagick->setResolution(300, 300);
Everything works fine on my local machine, but on my web-hosting it returns Uncaught Error: Class 'imagick' not found. Do I need to do the require for all the files or something? Because I tried to add the following in the beginning:
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/calcinai/php-imagick/src/Imagick.php';
now it throws me this: Uncaught Exception: Imagick::setResolution not implemented
Please, advice.
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/calcinai/php-imagick/src/Imagick.php';
Above line is not required. As mentioned in the comments, Composer is strict with casing of class names and tweaking with cases of class names is discouraged as composer has proper strict autoloading of them in its classmaps.
$imagick = new Imagick();
Above line is the correct way of initialization. As far as the exception is concerned, their own internal implementation throws it as something yet to be implemented. Below is their method body they have,
Method definition:
/**
* #param float $x_resolution
* #param float $y_resolution
* #return bool
*/
public function setResolution($x_resolution, $y_resolution)
{
throw new Exception(sprintf('%s::%s not implemented', __CLASS__, __FUNCTION__));
}
As far as remote server is concerned, everything is performing fairly well there too. If you really need some implementation for setResolution, some other library might help.
I am using graphaware to connect to a neo4j graph database. I keep getting an error saying Fatal error: Uncaught Error even though I'm using the library in composer.json. Here is the code for the autoload.php:
<?php
/*
* This file is part of the GraphAware Neo4j PHP OGM package.
*
* (c) GraphAware Ltd <info#graphaware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
error_reporting(E_ALL | E_STRICT);
$basedir = __DIR__.'/../';
//$basedir = __DIR__.'C:/xampp/htdocs/';
$proxyDir = $basedir.DIRECTORY_SEPARATOR.'_var';
putenv("basedir=$basedir");
putenv("proxydir=$proxyDir");
$loader = require_once __DIR__.'/../vendor/autoload.php';
Here us the code for the configuration php file named configNeo4j.php:
<?php
// Connection to the database
require_once __DIR__.'/vendor/autoload.php';
use GraphAware\Neo4j\Client\Client;
use GraphAware\Neo4j\Client\ClientBuilder;
$client = new Client ();
$client = ClientBuilder::create ()
->addConnection ( 'default', 'http://neo4j:neo4jj#127.0.0.1:7474' )
-> addConnection('bolt', 'bolt://neo4j:neo4jj#127.0.0.1:7687')
->build ();
$query = "MATCH (X) RETURN X";
$result = $client->run ( $query );
?>
Here is an image of the file structure:
Now when I run the webpage on a web browser which I am doing using xampps apache server I get this error message:
Fatal error: Uncaught Error: Class 'GraphAware\Neo4j\Client\Client' not found in C:\xampp\htdocs\configNeo4j.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\configNeo4j.php on line 11
This might also help:
This is strange because I am using the library in eclipse and I have also installed the composer in the php.exe file in xampp. If anyone has any solution to this problem it would be great if you could let me know how this problem can be fixed.Thanks in advance.
try this:
require_once __DIR__.'/vendor/autoload.php';
your code is:
require_once __DIR__.'C:/xampp/htdocs/vendor/autoload.php';
you dont need to specify the full path of the files ('c:/xampp/...')
__DIR__ will give you the current directory of the file you wrote your codes
oh and anyway, did you edit the autoload.php? if you use third party classes or plugins, you should not have to edit their core files.
Use better relative path to load autoload file. With this you are also making the app independent from the OS and your filesystem. Just like this:
require_once __DIR__.'/vendor/autoload.php';
I need to access to my app services in a simple php script. I don't want to create a HTTP/Console kernel. If the following is the content test.php file in the app root, what else should I call to make it working?
require_once __DIR__ . "/vendor/autoload.php"; //composer
$app = new Illuminate\Foundation\Application(realpath(__DIR__));
$app->boot(); // not sure about this
$app->make('db')->table('user')->get()->toArray();
Currently I get the following error:
PHP Fatal error: Uncaught ReflectionException: Class db does not exist in foo\vendor\laravel\framework\src\Illuminate\Container\Container.php:752
Fatal error: Uncaught ReflectionException: Class db does not exist in foo\vendor\laravel\framework\src\Illuminate\Container\Container.php:752
After checking the source code it's clear that the Application class does not load all classes defined in the config/app.php file and either you have bootstrap it yourself of running bootstrap function of build-in kernel. So, If you create a php file with the following lines you can access all app dependencies without bothering creating artisan command to do your snippet codes directly in phpstorm.
require_once __DIR__ . "/vendor/autoload.php"; //composer
$app = new Illuminate\Foundation\Application(realpath(__DIR__));
// http Kernel yet have to be registered, perhaps a bad design issue.
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
// optional
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
// and playground is ready
$users= $app->make('db')->table('user')->get()->toArray();
or
require_once __DIR__ . "/vendor/autoload.php";
$app = include_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
dd(app('db')->table('user')->count());
I have run into a few problems trying to make Codeception work with my Slim Framework project.
First, I cannot call a service/repository/model from a unit class without it being in the same namespace. When the tests are run, the following code block results in:
[Error] Class 'OMIS\Services\UserService' not found`
// tests
public function testUser()
{
$userService = new UserService();
$this->tester->assertNotNull($userService->find(1));
}
Secondly, I am trying to get Codeception to interact with a testing database I have set up. Because I am using Laravel's Eloquent ORM I thought that enabling the Laravel ORM module for Codeception in unit.suite.yml might work as there is no Slim module available for unit testing, but I get the following error when running the tests:
ExampleTest: UserPHP Fatal error: Class 'Symfony\Component\HttpKernel\Client' not found in /Users/me/dev/project/vendor/codeception/codeception/src/Codeception/Lib/Connector/Laravel5.php on line 13
Fatal error: Class 'Symfony\Component\HttpKernel\Client' not found in /Users/Me/dev/project/vendor/codeception/codeception/src/Codeception/Lib/Connector/Laravel5.php on line 13
Here is the relevant part of my bootstrap/app.php file
session_start();
require __DIR__ . '/../vendor/autoload.php';
// Load environment variables
$dotenv = new Dotenv(__DIR__.'/../');
$dotenv->load();
$container = require __DIR__.'/../config/container.php';
$app = $container->get(App::class);
$config = new Config(__DIR__.'/../config/database.php');
$db = $config->get('db');
$capsule = new Capsule();
$capsule->addConnection($db);
$capsule->setAsGlobal();
$capsule->bootEloquent();
require __DIR__.'/../config/dependencies.php';
require __DIR__.'/../config/services.php';
require __DIR__.'/../config/controllers.php';
require __DIR__ . '/../app/routes.php';
// Middleware
I'm a google recaptcha v2 on a local machine running wamp. Everything looks fine except it keeps dying when its supposed to validate the form
I'm getting this error:
Fatal error: Class 'ReCaptcha\RequestMethod\Post' not found in C:\wamp\www\php\contactForm\Captcha\ReCaptcha.php on line 73
I've pretty much copy/pasted the example code from google:
if (!empty($human)) {
require_once('Captcha\ReCaptcha.php');
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($human, $remoteIp);
if ($resp->isSuccess()) {
// verified!
I've downloaded the files from the google github (https://github.com/google/recaptcha/tree/master/src/ReCaptcha) and just used the folder /files names as they came. I've got my validation file one folder above but I've also tried coping the files into the same folder as my validation script just in case.
Any ideas?
It seems google bank on the fact that everyone use composer to install their repository. And to be honest that is the only installation method they give on thier github repo readme.md https://github.com/google/recaptcha
When you install a package such as google recaptcha with composer the package has the option to register with an autoloader in https://github.com/google/recaptcha/blob/master/composer.json
"autoload": {
"psr-4": {
"ReCaptcha\\": "src/ReCaptcha"
}
},
This way all you have to include in your script to get access to all your package classes is the autoload.php that composer generates when it installs your packages.
Line 34: https://github.com/google/recaptcha/blob/master/examples/example-captcha.php
// Initiate the autoloader.
require_once __DIR__ . '/../vendor/autoload.php';
An autoloader is a function that tries to load a class when php is asked for it. And in this case it kind of maps a namespace to a directory structure on the disk.
More about php autoloaders here: http://php.net/autoload and here: http://www.php-fig.org/psr/psr-4/examples/
If you do not wish to use composer and its autoload functionality, you may find this useful: https://github.com/abraham/twitteroauth it has an autoload.php which you could borrow which can load classes without composer.
Place a copy of it in your recaptcha top level folder (the one with README.md in it)
Replace line 12 with $prefix = 'ReCaptcha\\';
replace line 15 with $base_dir = __DIR__ . '/src/ReCaptcha/';
Require autoloader.php (this file) in your code somewhere
Instantiate your ReCaptcha object something like this in your code
new ReCaptcha\ReCaptcha($RECAPTCHASECRETKEY);
I hope I don't get slammed to hard but I found if you require all of the files it works as designed without using an autoloader or composer.
//GOOGLE RECAPTCH CODE
require_once('/cgi-bin/src/ReCaptcha/ReCaptcha.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod.php');
require_once('/cgi-bin/src/ReCaptcha/RequestParameters.php');
require_once('/cgi-bin/src/ReCaptcha/Response.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/Post.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/Socket.php');
require_once('/cgi-bin/src/ReCaptcha/RequestMethod/SocketPost.php');
$gRecaptchaResponse = $_POST['g-recaptcha-response'];
$secret = 'YOUR SECRET KEY';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
if ($resp->isSuccess()) {
//DO ACTION IF SUCCESSFUL
} else {
$errors = $resp->getErrorCodes();
}
//END OF GOOGLE RECAPTCHA CODE