Class not found after installing with composer - php

I followed the instructions to install this package:
https://github.com/embedly/embedly-php#installing
In my code I did:
$api = new Embedly\Embedly(array('user_agent' => 'Mozilla/5.0 (compatible; mytestapp/1.0)'));
$objs = $api->oembed('http://www.bbc.com/news/world-latin-america-37077172?ns_mchannel=social&ns_campaign=bbc_breaking&ns_source=twitter&ns_linkname=news_central');
print_r($objs);
But I get this error:
Class 'Embedly\Embedly' not found
What am I missing? Do I have to put something in the provider/alias? If so, I don't know what. I'm using Laravel 5.2.

I guess you just followed README, which was missing line for composer.
I've added that in PR.
To explain that, Composer handles all your autoloading and dependencies. To enable that explicitly, you need to include it's autoload file, where all this happens.
Just begin your index.php (or container) file with:
<?php
require_once __DIR__ . '/vendor/autoload.php';
// your code

Just add another '\' at the beginning like this:
$api = new \Embedly\Embedly(...) ;

Related

Having real problems with Composer

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.

php composer class not found when library is installed

I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap
I created a index.php file with
<?php
require __DIR__ . '/vendor/autoload.php';
$hosts = Nmap::create()->scan([ 'example.com' ]);
$ports = $hosts->getOpenPorts();
echo $ports;
?>
This is what my composer.json file
{
"require": {
"willdurand/nmap": "^0.5.0"
}
}
When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.
When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.
You have to either add the namespace to the class defenition:
$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);
Or, add a using statement for this class at the top of your file: (under <?php ofcourse)
use Nmap\Nmap;

Class 'Google\Cloud\Storage\StorageClient' not found

use Google\Cloud\Storage\StorageClient;
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
That as my code.Here I have installed composer on windows and getting following error:-
Fatal error: Class 'Google\Cloud\StorageClient' not found in C:\xampp\htdocs\fingertips\application\controllers\teacher.php on line 214
And even after running commands with composer to use google cloud api's, then also nothing is happening.
On cmd, when I am running this, "composer require google/cloud-storage" ,I am getting this
Using version ^1.3 for google/cloud-storage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I have run so many commands to fix this but didnt get anything in success.Can somebody please help what went wrong
In regards to your second question, you didn't include the actual error you're seeing.
I can see an issue with this code block though:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$file = fopen($params['book']['tmp_name'], 'r');
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload($params['book']['name'], [
'name' => 'test.pdf'
]);
I'm missing the actual data you want to upload. The upload method needs data to upload. This should work:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload(file_get_contents($params['book']['tmp_name']), [
'name' => 'test.pdf'
]);
See the documentation for more examples of how to go about uploading a file.
Have you installed storage client? as stated here.
https://packagist.org/packages/google/cloud-storage
use
composer require google/cloud-storage

Installing Composer with autoload.php and use Aws\* says Class not found

I'm running my php in cli under my user.
I have installed composer and autoloader.php does exist. Under vendor folders and file have been downloaded (autoload.php aws, bin, composer, guzzlehttp, mtdowling and psr)
Now in my php code I do:
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('KEY', 'SECRETKEY');
$client = Route53Client::factory(array(
'credentials' => $credentials
));
When I run the script I get: PHP Fatal error: Class 'Credentials' not found in /home/user/updatedns.php on line 15
I tried running it a sudo also (in case it needs to write to the directory) and still get this error.
Just found out that use Aws\Common\Credentials\Credentials; is now under use Aws\Credentials\Credentials;

PHPUnit out of box not working on OSX

Installed PHPUnit via PEAR and copied the latest repository from https://github.com/sebastianbergmann/phpunit/
Wrote a basic test and saved it in the root directory (also tried /Tests/)
<?php
require_once 'PHPUnit/Autoload.php';
class CalculatorTest extends PHPUnit_Framework_TestCase{
public function testAdd(){
$c = New Calculator();
$result = $c->add(5, 10);
$this->assertEquals(15, $result);
}
}
Throws errors about not being able to load require_once(SebastianBergmann/Diff/autoload.php) In the PHPUnit/Autoload.php there is three lines, 69, 70 and 71.
require_once 'SebastianBergmann/Diff/autoload.php';
require_once 'SebastianBergmann/Exporter/autoload.php';
require_once 'SebastianBergmann/Version/autoload.php';
The directory SebastianBergmann doesn't even exist.... why are these lines in here?
What am I doing wrong, did I copy from the wrong place?
Also trying to run /Tests/Runner/BaseTestRunnerTest.php fails with Class 'PHPUnit_Runner_BaseTestRunner' not found
You cannot just clone it and expect to work. Either completely install via PEAR or composer.
As of those particular references - they are satisfied as composer dependencies: see here https://github.com/sebastianbergmann/phpunit/blob/master/composer.json#L32

Categories