Class not found with Guzzle Http Client - php

This is a strange error that's constantly occurring.
Fatal error: Class 'Guzzle\Http\Client' not found in /home/futcoins/public_html/autobuyer/classes/shopify.php on line 15
This is the source code. I think this question is pretty straight forward and I've been stuck with this problem for a couple of days, any ideas?
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\FileCookieJar;
class Shopify {
//initialise the class
public function __construct() {
}
public function GetOrders() {
$client = new Client(null); //Line 15 where errors occurs
$request = $client->get("url");
$response = $request->send();
$json = $response->json();
return $json;
}
}

So you have a declaration at the top
use Guzzle\Http\Client;
That means you either have an autoloader or have included the appropriate file(s) manually. So you need to find the file that has that class and include it or else PHP will be looking for code you've not given to it.

I'm not sure it is the right solution for you but I had the exact same problem and to fix it I updated composer on my server and regenerated the autoload file:
sudo /usr/bin/composer.phar self-update
/usr/bin/composer.phar dump-autoload
I'm not sure it is necessary but I also restarted apache:
sudo /etc/init.d/httpd restart
To prevent this in the future and because we are using Elastic Beanstalk I created an configuration file to make sure that composer is up to date:
commands:
01updateComposer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
Source: http://blogs.aws.amazon.com/php/post/Tx2M04LCN1UEE0E/Reduce-Composer-Issues-on-Elastic-Beanstalk

Related

running minimal app with xhp-lib v4 and hhvm v 4.81.1 throws error

I'm trying the following setup and am getting this error:
\nFatal error: Uncaught Error: Found top-level code in /home/user/code/xhp-simple/src/example.php:7\nStack trace:\n#0 (): main()\n#1 {main}
Setup:
composer.json
{
"require": {
"hhvm/hhvm-autoload": "^3.1",
"facebook/xhp-lib": "^4.0"
}
}
src/index.hack
use type Facebook\XHP\HTML\div;
// require_once(__DIR__."/../vendor/hh_autoload.hh"); // also tried here instead of in main
<<__EntryPoint>>
function main(): void {
require_once(__DIR__."/../vendor/hh_autoload.hh");
echo <div>{1 + 2}</div>;
}
hh_autoload.json
{"roots": ["src/"]}
run command:
hhvm -m server -p 8080 -d hhvm.server.default_document=./src/example.hack
I have hhvm v 4.83.1 installed
I think that you're running into the fact that hhvm-autoload hasn't caught up with the recent restrictions to top-level code. In particular, where require_once seems to no longer be allowed at the top level. With your hh_autoload.json, hhvm-autoload generates this hh_autoload.hh:
<?hh // partial
require_once(__DIR__.'/autoload.hack');
Facebook\AutoloadMap\initialize();
Where I believe that require_once is illegal. If you place this code in your main, it should work. I tested this with no problems on HHVM 4.84.0:
// src/index.hack
use type Facebook\XHP\HTML\div;
<<__EntryPoint>>
async function main(): Awaitable<void> {
require_once(__DIR__.'/../vendor/autoload.hack');
Facebook\AutoloadMap\initialize();
echo await (<div>{1 + 2}</div>)->toStringAsync();
}
$ # run with:
$ hhvm src/index.hack
Also note that all rendering is async now with XHP-lib, so you can't just echo XHP objects directly; instead:
Calls to $xhp->toString() need to be updated to $xhp->toStringAsync().
I've just noticed the updated README on hhvm-autoload, where indeed hh_autoload.hh has been phased out and you need to need to generate the autoload map yourself (emphasis mine):
Replace any references to vendor/autoload.php with vendor/autoload.hack and call Facebook\AutoloadMap\initialize()

How to force Composer script to load local class instead of global class

undefined method
(Relevant files linked at the bottom of my question.)
I let Composer run some post-install-cmd and post-update-cmd scripts. In my script I want to make use of the readlink() function from symfony/filesystem. Inside my projects /vendor folder there is the 3.4 version of the filesystem package, fine.
I use Symfony\Component\Filesystem\Filesystem; at the top of my file.
But whenever I run:
$fs = new Filesystem();
$path = '/path/to/some/symlink';
if ($fs->readlink($path)) {
// code
}
I get the following error which tells me I'm calling an undefined method:
PHP Fatal error: Uncaught Error: Call to undefined method
Symfony\Component\Filesystem\Filesystem::readlink() in
/Users/leymannx/Sites/blog/scripts/composer/ScriptHandler.php:160
OK, so I double-checked the class inside my project's /vendor folder. This method is there. My IDE points me there. But when I run:
$fs = new Filesystem();
get_class_methods($fs);
this method is not listed.
Which file is it trying to load the method from?
OK, so I tried to check which file it's loading this class from:
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
and that returns me phar:///usr/local/Cellar/composer/1.7.2/bin/composer/vendor/symfony/filesystem/Filesystem.php – But why? Why is it taking the package from my Mac's Cellar? That's odd.
But OK, so I thought that's a Homebrew issue, and uninstalled the Homebrew Composer $ brew uninstall --force composer and installed it again as PHAR like documented on https://getcomposer.org/doc/00-intro.md#globally.
But now it's the same.
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
returns me phar:///usr/local/bin/composer/vendor/symfony/filesystem/Filesystem.php.
But why? Why does it pick up the (outdated) package from my global Composer installation? How can I force my script to use the project's local class and not the one from my global Composer installation?
What else?
Initially my $PATH contained /Users/leymannx/.composer/vendor/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin. I removed /Users/leymannx/.composer/vendor/bin to only return /usr/local/bin /usr/bin /bin /usr/sbin /sbin. Still the same.
I also tried setting the following in my composer.json. Still the same:
"optimize-autoloader": true,
"classmap-authoritative": true,
"vendor-dir": "vendor/",
I finally created an issue on GitHub: https://github.com/composer/composer/issues/7708
https://github.com/leymannx/wordpress-project/blob/master/composer.json
https://github.com/leymannx/wordpress-project/blob/master/scripts/composer/ScriptHandler.php
This is matter of context where your code is run. If you're executing some method directly in post-install-cmd it will be executed inside of Composer's process. It means that it will share all code bundled inside of composer.phar. Since you can't have two classes with the same FQN, you can't include another Symfony\Component\Filesystem\Filesystem in this context.
You can bypass this by running your script inside of separate process. You may create post-install-cmd.php file where you do all bootstrapping (like require vendor/autoload.php) and call these methods. Then run this file in your post-install-cmd hook:
"scripts": {
"post-install-cmd": [
"php post-install-cmd.php"
]
},

Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255

I moved my project from desk to another.
When I run php artisan it does not work.
I tried to run composer update, but it returns the error
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
This is how I solved this after an upgrade from laravel version 6.x - 7.x:
In App\Exceptions\Handler changed
//Use Exception;
Use Throwable;
Then methods to accept instances of Throwable instead of Exceptions as follows:
//public function report(Exception$exception);
public function report(Throwable $exception);
//public function render($request, Exception $exception);
public function render($request, Throwable $exception);
In config\session.php:
//'secure' => env('SESSION_SECURE_COOKIE', false),
'secure' => env('SESSION_SECURE_COOKIE', null),
Then run composer update
I solved the problem this way:
cd bootstrap/cache/
rm -rf *.php
The bootstrap directory contains the app.php file that initializes the structure. This directory also houses a cache directory that contains structure-generated files for performance optimization, such as files and route cache services. Laravel stores configuration files, provider, and cached services to optimize the fetching of this information. The problem with me was when the other developer ran the 'php artisan config: cache' command on your machine and since the cache folder contains files that can be deleted, I deleted them and solved the problem.
If this happened after Laravel update from 6.x to 7.x, then this could be due to the update of Symfony. See the upgrade guide of this part:
https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades
I was upgrading my Laravel from 5.8 to 8.0 and I got this error.
So my fixes were
As #nobuhiroharada mentioned that I had missed .env file in my project
Second is that Laravel removed Exception and replaced it with Throwable. So we need to fix that in our app\Exceptions\Handler.php. One can refer Medium.com for the error fix.
In the upgrade guide of Laravel 8.x you need to update the dependencies like this
Next, in your composer.json file, remove classmap block from the autoload section and add the new namespaced class directory mappings:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Finally from bootstrap\cache delete the cache files and run composer update.
These 5 steps might help you remove the error you are facing in your Laravel Project.
This happens because you have upgraded to Laravel 7.
To fix it, update app/Exceptions/Handler.php like so:
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable; // <-- ADD THIS
class Handler extends ExceptionHandler
{
public function report(Throwable $exception) // <-- USE Throwable HERE
{
parent::report($exception);
}
public function render($request, Throwable $exception) // AND HERE
{
return parent::render($request, $exception);
}
}
This is documented in the official upgrade guide here:
https://laravel.com/docs/7.x/upgrade#symfony-5-related-upgrades
I got the same problem in Win 8 and solve it:
Here is the steps.
Step-1: Go to your project directory
Step-2: And type command cd bootstrap/cache/
Step-3: Again type command del -rf *.php
Step-4: Update your composer composer update
Step-5: Now you are done: php artisan serve
Thanks.
Do you have .env file in your new project?
I had same error message. When I add .env file, error is gone.
success message like this.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: ixudra/curl
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: socialiteproviders/manager
Package manifest generated successfully.
I hope this will help you.
maybe you have an error in the project code (for example, in routes or controller). This may be one of the reasons for this error.
In my project, the web.php file has a syntax error. I defined this when I started the php artisan command
C:\OSPanel\domains\lara.shop.loc>php artisan
In web.php line
syntax error, unexpected end of file
Same issue when I update laravel from 6.x to 7.x
I tried the most voted answer but it didn't work, then I used php artisan serve I noticed that:
RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.
Try composer require laravel/ui maybe it will work.
I solve this error by deleting the vendor table then run composer update. I'm using Laravel 7. So, if you are not updating from the older Laravel version, maybe this is the solution.
I had this same problem when running composer update in a Laravel project. In the package.json it's configured to run artisan package:discover, which failed with:
Class 'Symfony\Component\Translation\Translator' not found in vendor/nesbot/carbon/src/Carbon/Translator.php on line 18
When I looked in the vendor/symfony/translation directory I found that it was completely empty, which explained the error.
The solution was to completely delete the vendor directory and then re-run composer update. This was the only way that I was able to make composer install the missing files.
I deleted composer.lock file and ran composer update.
That solved mine
I had the same issue, my problem was the PHP version of the server account did not match my Docker container. The SSH terminal was using the global php version for the server.
php -v
Confirm it's the version your project needs.
Composer did warn me that a higher php version was required but I rm -rf'd /vendor and ./composer.lock without paying too much attention to the warnings!
Got the same problem.
php artisan doesn't work.
composer install got error:
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
And this works for me.
When I switch another linux user. It works.
some files are owned by another linux user.
So I use root account and change all the project file to the specific user,
chown -R www:www project/
and use that user to execute composer cmd
and then it works.
My case/solution, in case it helps anyone...
I copied my repo over from my old Windows computer to a new one, and installed the latest php.
composer install was returning:
Root composer.json requires php ^7.1.3 but your php version (8.1.10) does not satisfy that requirement
...which I thought was odd (assuming 8 satisfied ^7), so I continued on with composer install --ignore-platform-reqs, and ended up with this particular issue.
After trying a bunch of other possible solutions, what ended up working for me was simply downgrading to the same PHP version from my old machine (7.4.33).
This is not an actual error. If you look a bit above you'll see the actual error.
In my case, there was an error in my code:
PHP Fatal error: Declaration of
App\Exceptions\Handler::render($request, App\Exceptions\Exception $exception)
must be compatible with
Illuminate\Foundation\Exceptions\Handler::render($request, Throwable $e)
It is not possible to tell you what is actually a problem in your code, so you have to look real reason for this error in your stack trace.
In my case there is missing folder and its file Kernel.php in
app/Console
So I created app/Console/Kernel.php using code from previous project.
Now everything working fine.
Make sure your config\constants.php (and/or resources\lang\en\local.php) has no syntax errors. I get this error a lot by missing commas in constants.php file.
If you have this error the simplest way is you can try using composer install instead of composer update
I deleted my project I created a new folder and cloned the repository again and after that I gave composer install / update.
I got the same problem in Win 10 and solve it:
Here is the steps.
Step-1: Go to your project directory
Step-2: Update your composer
composer update
Step-3: Now you are done: php artisan serve
Nothing worked, so I installed a new project, and I read Handler.php in App\Exceptions, it was different, probably because I copied some solution and Internet and deleted the following:
protected $dontReport = [
//
];
protected $dontFlash = [
'password',
'password_confirmation',
];
I copy here all of Handler.php generated by laravel 7.5, may be useful for someone:
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* #var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* #var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* #param \Throwable $exception
* #return void
*
* #throws \Exception
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Throwable $exception
* #return \Symfony\Component\HttpFoundation\Response
*
* #throws \Throwable
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
}
}
For me it was related to Kernel.php
I was adding new schedule task into the Kernel. I also updated some controllers, views, and installed twilio extension.
The error did not provide more information than
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 255
#Suresh Pangeni refences the Kernel.php doc so I checked by doc that is in PROJECTFOLDER\app\Console\Kernel.php
protected $commands = [
Commands\Inspire::class,
Commands\Test::class
\App\Console\Commands\Message::class,
];
Missing Comma between Commands\Test::class and the next line didn't let me proceed. It provided no further warning or information when I ran composer dump-autoload.
Hope this can help someone else that has a similar issue!
I had the same error today, the causes are as follows:
cause 1: the env file contains space in one of the configuration.
cause 2: incorrect configuration of the Handler file belonging to the App\Exceptions namespace;
cause 3: incorrect configuration of a file inheriting ExceptionHandler
I was using Laravel 9.x and got the same error after trying to install this package maatwebsite/excel!
thanks to #samuel-terra and #dqureshiumar there is the solution worked for me:
clear bootstrap/cache:
cd bootstrap/cache/
rm -rf *.php
then run composer update:
composer update
My problem was __construct method.
composer.json
"php": "^8.1",
"laravel/framework": "^9.19",
Handler.php
The problem originates from this code:
this->container->make(FlasherInterface::class);
My solution, I removed the construction directly and the problem is solved.
public function __construct(Container $container)
{
parent::__construct($container);
$this->flasher = $this->container->make(FlasherInterface::class);
//$this->request = $this->container->get(Request::class);
}
Getting this error when my composer version 2.x then i rollback this
composer self-update --1
Now its perfectly working

phpredis errors Class Redis not found in Linux

I met a weied problem when installing phpredis by
cd phpredis && ./configure && make && make install
after that, I add
extension=redis.so
into php.ini.
I can get an OK by running
php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"
BUT when running http:127.0.0.1, nginx throw a error " Fatal error: Class 'Redis' not found in index.php"
<?php>
$client = new Redis();
<?>
I guess this may be some problems related with environment...
Thanks for any advice!
The command line probably does not use the same php.ini file than the web server.
Use phpinfo(); to know which configuration file is loaded in both cases and then declare your extension in the ini file used by your web server.
I had this issue minutes ago, and I solved it restarting the server, this way the server refresh *.ini files
If you're using composer and get the error "Class Redis not found" try put a backslash before the name class. Like this:
<?php
$client = new \Redis();
<?

Installing Slim, PHP, Ubuntu

I'm trying to install Slim on my local LAMP server on Ubuntu only I get stuck at second base.
From my tutorial and various documentation found online:
You now have access to the composer command. Sure enough if I go to
the terminal and enter:
$ composer
Composer version b474944155429eb4cce186c746d55287ee6bb3f4
Usage:
[options] command [arguments]
The next step is to specify Slim as a required package for your app.
This can be accomplished, via a composer.json file within the root of
your project.
Where is the root of my project? I thought it would be
/var/www/slim
I've tried adding composer.json to:
/var/www/slim
and stood in /slim define an index.php script with:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
?>
Go to
http://localhost/var/www/slim
and the browser returns:
Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/slim/index.php on line 2
Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/slim/index.php on line 2
Well, there seems to be more then one question in your post, but regardless ...
"Where is the root of my project?"
The root of your project should be in /var/www if you haven't changed your hosts / Apache settings.
Then on to your 2nd question, which I will take the liberty to rephrase :)
"How to create your web App, and include the composer-installed packages inside?"
go to your respective web-root directory, likely in your case /var/www and in it create "index.php". Then, there, in the console, run the command:
composer install
This should install the packages defined in your composer.json, which should in the same web root dir.
If everything went OO, you will have in the following a new directory: /var/www/vendor
Now, go to your web-root directory and create your index.php and in it, in the beginning add the following lines:
require 'vendor/autoload.php';
// to load all the packages installed with composer, not only slim. If
//composer was run in the right directory and w/o problems of course :)
$app = new \Slim\Slim();
// to instantiate Slim script instance and assign it to the pointer $app
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
})
//to create your 1st route ...
you need to run
composer install
from terminal. After that, add
$app->run();
in index.php.

Categories