Class 'GuzzleHttp\Client' not found on Laravel production shared server - laravel

I'm trying to retrieve data from an API using Guzzle. I followed the steps in Guzzle website, installed it using composer, added the route and the code in a controller.
I tried these two options:
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://jsonplaceholder.typicode.com/',
// You can set any number of default request options.
'timeout' => 2.0,
]);
and...
$client = new \GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'https://jsonplaceholder.typicode.com/',
// You can set any number of default request options.
'timeout' => 2.0,
]);
When running I got the error...
Class 'GuzzleHttp\Client' not found.
I have tried:
require( dirname( __FILE__ ) . '/../../../vendor/autoload.php');
use GuzzleHttp\Client;
In composer.json:
"require": {
"guzzlehttp/guzzle": "^6.3",
...
},
Also...
composer update
composer dump-autoload
php artisan config:clear
The project is on shared hosting; I noticed that there was no Guzzle folder inside the /vendor folder, so I uploaded via FTP. But still the same error.
I've tried everything I found in the forum on this topic; I'm running out of ideas, please any advice would be appreciated.
Thanks in advance for your valuable help.

If you can't run composer on shared hosting, you should upload also /vendor/composer directory (after local $ composer require guzzlehttp/guzzle).

Related

Fatal error with Stripe Class 'Stripe\Stripe' not found

I'm trying to implement Stripe Checkout into my website. In local the api work normal but in host I get the error :
Class 'Stripe\Stripe' not found
Note: In my host I don't have SSH. And I added files manually with FTP.
\Stripe\Stripe::setApiKey("sk_test_XXXXXX");
$token = $request->stripeToken;
$customer = \Stripe\Customer::create([
'email' => $client->email,
'source' => $token,
]);
As mentioned you have installed Stripe library manually and uploaded on server.
To use this library include init file.
require_once('/path/to/stripe-php/init.php');
ON next step set Api key, Make sure to use test api key for testing.
\Stripe\Stripe::setApiKey( "sk_test_XXXXXX");
Make sure to download latest stripe library from Githut repo
As i have seen mentioned in the comments:
Stripe needs to be installed using (preferably) composer.
This can be done with the command: composer require stripe after having SSHed into the right directory.
You then need to include the vendor/autoload.php that is generated by composer.
In your case where you cant run composer do the following:
Download stripe`s latest release manually from the github page: https://github.com/stripe/stripe-php/releases
Then you need to include the init.php file found in the downloaded stripe-php directory like this require_once('/path/to/stripe-php/init.php');
Ensure you are running at least PHP 5.4 (Note! This version has reached its end of life. Upgrade if possible to PHP 7.2). You also need the PHP extensions curl, json and mbstring.
After having used require_once('/path/to/stripe-php/init.php'); in the file the stripe code will be running in you can then set your API key using \Stripe\Stripe::setApiKey("sk_test_XXXXXX"); and then run your code like f.ex: $customer = \Stripe\Customer::create([
'email' => $client->email,
'source' => $token,
]);
`
Please use stripe library with the below code to resolve the error
$stripe_obj = new Stripe();
$stripe = $stripe_obj->setApiKey(env('STRIPE_SECRET'));

Laravel 5.5 Socialite Error

I have an issue with Socialite on Laravel 5.5 I have Socialite required by composer, updated services.php with different providers credential and updated providers in app.php This is the error I am getting
Type error: Argument 1 passed to Laravel\Socialite\SocialiteManager::formatRedirectUrl() must be of the type array, null given
Any help is highly appreciated.
In most of the cases, the issue is solved through 3 steps:
Run php artisan clear-compiled
Run composer dump-autoload
Run php artisan optimize
Hope it helps. Thanks.
Check config/services.php for the provider you're authenticating with. The redirect entry needs to be set to your defined route.
'redirect' => 'http://your-callback-url'
Example:
// config.services.php
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => 'http://example.com/github/redirect',
],
// routes/web.php
Route::get('github/redirect', 'GitHubController#redirect')

Laravel Class 'Socialite' not found

Using composer, I have installed Socialite package to my local machine. It works well. Then I upload all the vendor/laravel/socialite directory as it is to server. Then on server, in composer.json added the line -
"laravel/socialite": "^2.0"
Also, in config/app.php make below changes -
In provider section add the line -
Laravel\Socialite\SocialiteServiceProvider::class,
In aliases section add this line -
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
Then, as per documentation add below code in config/services.php file -
'facebook' => [
'client_id' => '{{my_client_id}}',
'client_secret' => '{{my_secret_key}}',
'redirect' => 'http://mydomain/public/callback',
],
The Controller, I am using has included use Socialite; at top.
Bur now, it gives me error as Class 'Socialite' not found on server. Where as it works fine on my local machine.
Am I missing something to upload on server?
Your help is appreciated.
Thanks.
You need to do dump autoload everytime you make changes in composer.json, composer dump-auto

Lumen with Dingo API routes not defined

Have a fresh install of Lumen 5.2 and new install of Dingo 1.0.*#dev
I have installed the service provided in bootstrap/app.php
Also setup .env file eg
API_VERSION=v1
API_PREFIX=api
API_SUBTYPE=app
API_DEBUG=true
In the Http/routes.php I have added a test route eg
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api) {
$api->get('example', 'ExampleController#test');
});
This route is not working plus in command line if I try php artisan api:routes
I get error
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "api:routes" is not defined.
Did you mean this?
api:docs
Have I missed something? Also using HTTP Basic if it helps?
In Dingo Documentation -> Creating API Endpoints section you can find this sentence:
"If you're using Laravel 5.1 you can see the registered routes using Artisan.
$ php artisan api:routes
"
If you also run
$ php artisan list
only api:docs is available - api:routes is missing.
That means that this command do not work in Lumen.
composer requires jakubkratina/lumen-dingo-route-list
Add the following code in app/Console/Kernel.php:
protected $commands = [
\JK\Dingo\Api\Console\Commands\RouteListCommand::class
];
By default, as shown in docs, lumen don't ship with api:routes. But you can use lumen-dingo-route-list from jakubkratina. It will add route:list to your artisan.
By the way, I'd to do some adjustments to get it working:
First, include the backlash into the registration
protected $commands = [
\JK\Dingo\Api\Console\Commands\RouteListCommand::class
];
And last, edit vendor/jakubkratina/lumen-dingo-route-list/src/RouteListCommand.php and add this code:
public function handle()
{
return $this->fire();
}

Integrating Doctrine with CodeIgniter

I have successfully installed the latest version of CodeIgniter and have basic MVC pattern working. The problem that I've noticed is that CI doesn't naturally allow for prepared statements when it comes to queries. So, I decided to download Doctrine 1 from GitHub. I'm very new to Doctrine and needed some help integrating it with CI so I followed this tutorial.
In one of my controllers, I have
$this->load->library('doctrine');
$this->em = $this->doctrine->em;
But, when I go to load the view in my browser, I'm greeted with an error reading
Message: require_once(/Applications/MAMP/htdocs/CodeIgniter/application/libraries/Doctrine/Common/ClassLoader.php): failed to open stream: No such file or directory
Upon further inspection of the Doctrine download from GitHub, there doesn't even seem to be a folder titled "common" anywhere in there. I'm very new to CI and especially Doctrine. Does anyone have some advice that can help me get this working? Also, is it possible to use the MySQLi driver instead of the PDO one with Doctrine?
Downloading the Doctrine ORM straight from GitHub doesn't include the other dependencies. These are managed by Composer. If you look inside the composer.json file you can see these dependencies. If you want to install them manually, they are:
doctrine/common
doctrine/inflector
doctrine/cache
doctrine/collections
doctrine/lexer
doctrine/annotations
doctrine/dbal
symfony/console
I believe that's all of them. You will have to merge these files in their appropriate directories as they follow PSR-0 standards for the autoloading of classes.
Alternatively, install Doctrine 2 with Composer with the following composer.json file and any other dependencies will be installed automatically. Then integrate with CodeIgniter.
{
"minimum-stability": "stable",
"require": {
"doctrine/orm": "2.3.*"
}
}
Edit the index.php file of your CodeIgniter app by adding a single line to include the autoloader file before requiring the CodeIgniter core.
require_once BASEPATH.'../vendor/autoload.php';
require_once BASEPATH.'core/CodeIgniter.php';
Also if installing with Composer, use this edited version of the bootstrap as the contents of application/libraries/Doctrine.php, which is what worked for me
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\EntityManager;
class Doctrine
{
public $em;
public function __construct()
{
// Load the database configuration from CodeIgniter
require APPPATH . 'config/database.php';
$connection_options = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database'],
'charset' => $db['default']['char_set'],
'driverOptions' => array(
'charset' => $db['default']['char_set'],
),
);
// With this configuration, your model files need to be in application/models/Entity
// e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
$models_namespace = 'Entity';
$models_path = APPPATH . 'models';
$proxies_dir = APPPATH . 'models/Proxies';
$metadata_paths = array(APPPATH . 'models');
// Set $dev_mode to TRUE to disable caching while you develop
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
$this->em = EntityManager::create($connection_options, $config);
$loader = new ClassLoader($models_namespace, $models_path);
$loader->register();
}
}
Note: Version 3 of CodeIgniter when released, will be installable with Composer, but version 2 is not.
For those looking for a tutorial to integrate Doctrine 2 with CodeIgniter, this question and others answers are outdated (for CI 2).
This is a new tutorial for CI 3 I made and I checked is working:
How to install Doctrine 2 in CodeIgniter 3
I repeat it here.
Install Doctrine
Doctrine 2 ORM’s documentation - Installation and Configuration
Doctrine can be installed with Composer.
Define the following requirement in your composer.json file:
{
"require": {
"doctrine/orm": "*"
}
}
Then call composer install from your command line.
Integrating with CodeIgniter
Doctrine 2 ORM’s documentation - Integrating with CodeIgniter
Here are the steps:
Add a php file to your system/application/libraries folder called Doctrine.php. This is going to be your wrapper/bootstrap for the D2 entity manager.
Put the Doctrine folder (the one that contains Common, DBAL, and ORM) inside the third_party folder.
If you want, open your config/autoload.php file and autoload your Doctrine library: $autoload[‘libraries’] = array(‘doctrine’);
Creating your Doctrine CodeIgniter library
Now, here is what your Doctrine.php file should look like. Customize it to your needs.
<?php
/**
* Doctrine 2.4 bootstrap
*
*/
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine {
public $em = null;
public function __construct()
{
// load database configuration from CodeIgniter
require_once APPPATH.'config/database.php';
// include Doctrine's ClassLoader class
require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php';
// load the Doctrine classes
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party');
$doctrineClassLoader->register();
// load the entities
$entityClassLoader = new ClassLoader('Entities', APPPATH.'models');
$entityClassLoader->register();
// load the proxy entities
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
$proxiesClassLoader->register();
// load Symfony2 classes
// this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php)
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
// Set up the configuration
$config = new Configuration;
// Set up caches
if(ENVIRONMENT == 'development') // set environment in index.php
// set up simple array caching for development mode
$cache = new \Doctrine\Common\Cache\ArrayCache;
else
// set up caching with APC for production mode
$cache = new \Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// set up annotation driver
$driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings');
$config->setMetadataDriverImpl($driver);
// Proxy configuration
$config->setProxyDir(APPPATH.'/models/Proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE ); // only for development
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager, and store it for use in our CodeIgniter controllers
$this->em = EntityManager::create($connectionOptions, $config);
}
}
Setting up the Command Line Tool
Doctrine ships with a number of command line tools that are very helpful during development.
Check if these lines exists in the Doctrine.php file, to load Symfony classes for using the Command line tools (and for YAML mapping files):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine');
$symfonyClassLoader->register();
You need to register your applications EntityManager to the console tool to make use of the tasks by creating a cli-doctrine.php file in the application directory with the following content:
<?php
/**
* Doctrine CLI bootstrap for CodeIgniter
*
*/
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
require APPPATH.'libraries/Doctrine.php';
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
?>
Now run this script through the PHP command-line and should see a list of commands available to you.
php cli-doctrine.php
Generate mapping classes from database:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
if you get this error:
Fatal error: Call to undefined function Doctrine\Common\Cache\apc_fetch()
install the APC extension for PHP:
sudo apt-get install php-apc
sudo /etc/init.d/apache2 restart
For production mode you'll want to use a real caching system like APC, get rid of EchoSqlLogger, and turn off autoGenerateProxyClasses in Doctrine.php
Find the link for the doctrine integration in CI
https://github.com/mitul69/codeigniter-doctrine-integration
Please note that code igniter 2 has a little difference in its code organization. In code igniter 2 it is better to put the Doctrine folder in application/third_party folder, instead of application/libraries folder (or else it will not work!).
You can read more about it here
I had the same problem when I tried to follow this tutorial from doctrine user guide
http://doctrine-orm.readthedocs.org/en/latest/cookbook/integrating-with-codeigniter.html
This problem happens when I tried to install via composer so then I went to this web site
http://www.doctrine-project.org/downloads/ and do manually download the DoctrineORM-2.3.3-full.tar.gz version and the error was gone.
The original poster's problem seems to be an issue with autoloading. I was presented with a similar issue when trying to set up CodeIgniter and Doctrine with Composer. In CodeIgniter 3, you can enable the use of composer autoloading, which should allow you to load all Doctrine files correctly. You must point your Composer vendor dir to application/vendor for this to work. You can also do it in older versions, but then you have to include the Composer autoload file manually in your Doctrine library file for CodeIgniter.
If you want more information: I wrote a blog post describing exactly how to do it. http://blog.beheist.com/integrating-codeigniter-and-doctrine-2-orm-with-composer/
you can use this
via composer :
composer create-project rbz/codeigniter your-project
via git :
git clone https://github.com/dandisy/cihmvctwig.git

Resources