Fatal error: Class 'NexmoMessage' not found - php

I'm using this library to implement the Nexmo SMS service on my server. I required the library using Composer like so:
"require" : {
"prawnsalad/nexmo": "dev-master"
}
which I found here and followed the instructions included in the README like so:
require 'vendor/autoload.php';
use NexmoMessage;
$phone = '123456789';
$sms = new NexmoMessage(NEXMO_KEY, NEXMO_SECRET);//defined in another file
$sms->sendText($phone, 'from', "yo");//$phone is a valid number in actual case
However I keep getting the error in the title of this question. I see that Composer has imported the library successfully and I see the class and constructor for NexmoMessage, yet for some reason this error keeps happening no matter what I do. I'm not sure if this is due to an issue with the library or with how I'm using Composer. I've never had an issue with Composer in the past so I'm boggled why this is happening here. Thanks

Well, that library states that it supports PSR-4, which means they absolutely MUST use PHP namespaces, but they don't. That last commit 8 months ago https://github.com/prawnsalad/Nexmo-PHP-lib/commit/8e422c4f8f43c52acb70ebd5f9e7c6ff81a1f352 broke the autoloading. And nobody noticed up to today.
You can easily tell by looking at the source code: There is no namespace being used.
Your best action now would be to fix this issue and send a pull request! Github allows to edit files in the browser! Second best action would be to create an issue and let the authors know.

Related

PHP error calling a function from a package

I am new to php and have just installed my first package via composer. I'm now trying to call a function from the package I installed as follows:
<?php
require_once 'vendor/autoload.php';
$value = 1;
$aws = AmazonGiftCode::make()->buyGiftCard($value);
echo $aws;
?>
But I get the following error:
PHP Fatal error: Uncaught Error: Class 'AmazonGiftCode' not found in
/public_html/php/test.php:4 Stack trace:
#0 {main} thrown in /public_html/php/test.php on line 4
Based on my (albeit limited) experience with other languages, I'm guessing I have to load the package that contains the class first. The package folder is in the same directory as the test.php file, in the subfolder vendor/kamerk22/AmazonGiftCode/. But I think this is where I don't know enough to troubleshoot it based on the information I could find.
Your directory structure should look like this.
test.php
composer.json
vendor
└───autoload.php
└───kamerk22
└───AmazonGiftCode
Make sure you installed the package using composer, and not by downloading it.
I'm guessing I have to load the package that contains the class first.
Unlike what you would normally expect when importing stuff, when using composer you only have to import the autoload.php file, and composer will take care of loading other packages as needed. By as needed what I mean is that as soon as composer sees you use the AmazonGiftCode class it will import the AmazonGiftCode package, but if one of your REST endpoints doesn't use anything from the AmazonGiftCode package it won't ever load it. This allows you to not have to worry about slowing down the entire application when you want to use a composer package for only a few endpoints. At least that's the way I understand how composer works.
Just run composer dump-autoload once and the class should known then.
You could also get verbose and use kamerk22\AmazonGiftCode\AmazonGiftCode;
But that AmazonGiftCode looks quite Laravel specific... that's why it may still fail, even if it may be found by auto-load. One needs to setup Laravel framework in the first place; just see this query (just in case if you may wonder where all these missing classes may come from).

Issue with the implementation of the simplesamlphp library into my web application

I tried to implement the simplesamlphp library into my web application. But when I call the requireAuth() function I get a PHP fatal error message. Uncaught Exception: Loader: Illegal character in filename.....
It seems like he can't resolve the Class SimpleSAML\Module\saml\Auth\Source\SP
But I don't know why.
Does anyone have a idea how to fix this?
I already deleted the whole simplesamlphp installation and reinstalled it.
I use the following code:
require 'var/www/simplesamlphp/lib/_autoload.php';
$lAuthSrc = new \SimpleSAML\Auth\Simple('default-sp');
if (!$lAuthSrc->isAuthenticated()) {
$lAuthSrc->requireAuth();
}
$lAttributes = $lAuthSrc -> getAttributes();
foreach($lAttributes as $lAttribute) {
print_r($lAttribute);
}
Some additional informations:
The configured authentication source test works fine. If I login via the configured authentication source, everything works fine and I don't get any error messages (the requireAuth() function don't get called in this case).
I use the latest version of simplesamlphp v.1.18.3
If you need any more information, please let me know.
Honestly it looks like your path is messed up on the require... are you sure you should be using:
require 'var/www/simplesamlphp/lib/_autoload.php';
and not
require '/var/www/simplesamlphp/lib/_autoload.php';
Do you really have a 'var/www' subdirectory relative to the location of the script? That looks wrong to me. If you include that first / before var it makes that path absolute to the typical install location for SSP.
Thank you all for your help. I discovered this morning the issue. The issue was the autoloader which I use for my own application. I registered the application autoloader in another file which gets executed before the code you see above. And simplesamlphp uses some conditions like:
if (!class_exists($className))
And beacuse I registered my application autoloader before the function class_exists checked if the class exists in my application. In my application I don't use namespaces and this was the issue.
To fix this issue, I unregistered my application autoloader before using the simplesamlphp code and registered the autoloader again after the simplesamlphp code.
I hope this will save some of you headaches.

Cannot use Braintree package I've installed

I want to integrate Braintree php libary to my Laravel project (https://developers.braintreepayments.com/start/hello-server/php)
I've installed it with composer
require-dev{
"braintree/braintree_php" : "3.11.0"
}
But when I try to copy in the code such as I get this error in editor "Undefined class BrainTree_Configuration".
I've tried to require full path, put "\" before Braintree_Configuration: , "use" different paths
Braintree_Configuration::environment('sandbox');
It appears that both the Laravel and the Braintree docs are incorrect at the moment. In v 3.13.0 of the Braintree PHP package, the configuration class is \Braintree\Configuration, not \Braintree_Configuration.
I've just found this out and haven't completely tested it yet, but it looks like the only difference is the class name and namespace.
You can have a look at the current Configuration class on GitHub.
Also, if you're using Braintree with Laravel, it might help you to use their Cashier package, which will automatically include the Braintree package as well.
Just a note of warning, that the reference to the Braintree_Configuration class on this page is currently incorrect, as I mentioned above.

google-api-php-client's autoloader deprecated

As a test, I ran one line of code in PHP to implement the PHP Client Library.
indexTest.php
<?php
require_once realpath(dirname(__FILE__).'/google-api-php-client/autoload.php');
return;
I get this error:
Deprecated: google-api-php-client's autoloader was moved to src/Google/autoload.php in 1.1.3. This redirect will be removed in 1.2. Please adjust your code to use the new location. in C:\Users\NoName\Documents\academic-being-90217\google-api-php-client\autoload.php on line 25
What do I need to do to fix this?
Quick Fix:
require_once realpath(dirname(__FILE__).'/google-api-php-client/src/Google/autoload.php');
Google need to update their documentation on the API. (I spent quite a while getting myself in a fuddle recently, trying to get it working using their documentation).
What is you final aim on using the API? Is it just for Login? If you need any help with it, I'll be more than happy to talk you through anything that isn't working as it should be

php, laravel, log4php: Class 'Apache\Log4php\Hierarchy' not found error

I'm new to php and i'm using log4php with laravel.
My project structure is
->Root
->Laravel
->app
->folderx
->abc.php
->otherfolders
.
.
->vendor
->composer.json (contains log4php and laravel)
->logconfig.xml
I'm trying to initialize the logger from inside abc.php,
Logger::configure('../../../logconfig.xml');
but it gives the error message
Class 'Apache\Log4php\Hierarchy' not found
I verified that the class Hierarchy.php exists in the vendor/apache/log4php/src under Root folder. Also, if I open Logger.php and go to the line where Hierarchy in initialized and ctrl+click(in eclipse) on Hierarchy, it takes me to Hierarchy.php.
I'm trying to figure out why php is not able to find that class.
Any help/suggestions would be greatly appreciated.
Thanks
That error message shows that your PHP is looking for a namespaced class. Log4PHP has never been released until now with namespaces (up to version 2.3.0), so this is definitely weird.
If you accidentially use the development branch of the 3.0 version, I must currently suggest not to use it. It barely got any significant commits in the last year, and should be considered work in progress (very slow progress unfortunately).

Categories