Having real problems with Composer - php

I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.

In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.

Related

Class not found exception in PHP from composer

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.

Problem with composer not autoloading certain package

I have a package I created that I included with composer called ShinePHP (https://packagist.org/packages/adammcgurk/shine-php#0.0.4), and it has been working fine being autoloaded etc..., but right now the autoload just all of a sudden shut off. There is no reason for this, I didn't touch the composer.json file, I really didn't touch anything with the library, I'm just getting the error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in
/Applications/XAMPP/xamppfiles/htdocs/manager-reporting/src/index.php:12
Stack trace: #0 {main} thrown in
/Applications/XAMPP/xamppfiles/htdocs/manager-reporting/src/index.php
on line 12
Here is how that code is being called:
<?php
declare(strict_types=1);
session_start();
require_once 'vendor/autoload.php';
require_once 'model/Page.php';
require_once 'model/Auth.php';
use ShinePHP\{Crud, CrudException, HandleData, HandleDataException, EasyHttp, EasyHttpException};
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ehe) {
// Google Analytics
echo $ehe->getMessage();
exit;
}
I know it is a problem with this particular library, because I also have PHPMailer installed here with Composer, and I just tried instantiating PHPMailer using:
$mail = new PHPMailer(true);
And it worked no problem.
I've ran:
composer dumpautoload
And got this response:
Generating autoload files
Why is my autoload broken for my ShinePHP package?
Autoloading rules in your package (adammcgurk/shine-php) are incorrect. Since your classes are inside of src/ShinePHP directory your autolading rules should look like that:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/ShinePHP/"
}
},

Composer not auto-loading required classes

I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");

Push Notification: Fatal error: Class 'EccFactory' not found in pushnotification/vapidkeys.php on line 124

I am trying to implement push notification.
I downloaded web-push library from: https://github.com/web-push-libs/web-push-php
The first thing I wanted was VAPID keys .
filename is vapidkeys.php, this file is inside pushnotification directory, and in pushnotification directory I have web-push-php-master directory.
<?php
require('web-push-php-master');
use Minishlink\WebPush\WebPush;
var_dump(VAPID::createVapidKeys());
?>
But the above line throws following error:
Fatal error: Class 'EccFactory' not found in pushnotification/vapidkeys.php on line 124
Please help me solve this issue, I am new to push notification and namespaces
You have to get web-push-php with Composer, so that all the dependencies are installed.
Install Composer
Run composer require minishlink/web-push. This will install web-push-php and all its dependencies in the vendor folder.
In your PHP file, require it : require __DIR__ . '/vendor/autoload.php';
Here's a basic example that uses web-push-php: https://github.com/Minishlink/web-push-php-example
Hope this helps.
HTTPD Website Only Generate Key,Your website is HTTP can't generate Key

Fatal error: require(): Failed opening required 'C:\wamp\www\sep24\e/src/functions.php' (include_path='.;C:\php\pear')

Tried to run the trans.php program from wamp server from the path
C:\wamp\www\sep24\e\trans.php
I have included the AWS folder in
C:\wamp\www\sep24\e\Amazon\
And AWS credential file in wamp/www folder as well user directory for the access
C:\wamp\www\.aws\credentials & C:\Users\username\.aws\credentials
This is my program
<?php
define('ROOT', dirname(__FILE__));
require ROOT . '/vendor/autoload.php';
use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
-------------
------------
// no error here.
?>
When i'm trying to run the program, I get this error
Fatal error: require(): Failed opening required 'C:\wamp\www\sep24\e/src/functions.php' (include_path='.;C:\php\pear') in C:\wamp\www\sep24\e\vendor\composer\autoload_real.php on line 54
I have included all the packages of AWS which I downloaded from the git.
What change should I make ?
There are two main problems are:
1 Composer Autoloading
The AWS dependency needs to be downloaded with Composer,
if you want the Composer Autoloader to work correctly.
Do not move folders around, when working with Composer.
The autoloading expects the files and folders inside the vendor folder.
I have included all the packages of AWS which I downloaded from the git.
You don't need to do this manually.
2 The use statement is wrong.
Change use Amazon\Aws\ElasticTranscoder\ElasticTranscoderClient;
to use \Aws\ElasticTranscoder\ElasticTranscoderClient;
3 Example Application
Because it is your third question and you seem to have problems with the application structure in connection with Composer, i will provide a simple PHP application template to demonstrate how you work with the AWS dependency.
This example provides a basic namespaced PHP application and includes the Client class from the AWS dependency(, which you have to fetched by Composer).
You find the file over here:
https://www.dropbox.com/s/q1b406thgu3146n/php-app-composer-aws.zip?dl=0
Extract the test folder into your www folder.
Then execute a composer install and run index.php.
You will end up with a error from TranscoderClient, because it expects a configuration. Not part of the problem.
Use composer.
Create testaws directory and put composer.json file with content below (you can adjust it to your needs for example PHP version or dev packages)
{
"name": "yourname/sampleapp",
"description": "Sample app",
"require": {
"php": ">=5.5.0",
"aws/aws-sdk-php" : "dev-master"
},
}
run composer install
then in index.php in testaws directory put this line in index.php
require __DIR__ . '/vendor/autoload.php';
After you do this steps it should work. More about composer you will find there
Also you can find sample project here
Delete the vendors folder and run composer install.

Categories