How to require a package installed via Composer - php

I installed emanueleminotto/simple-html-dom via composer.
How can I use classes from the package without getting an error?
Note: I use XAMPP to run PHP scripts.
Error Message:
PHP Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3
Stack trace:
0 {main}
thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3
Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3
Stack trace:
0 {main}
thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3

After running
$ composer install
require the autoloader generated in vendor/autoload.php at the top of your script file (or, for a web application, in the front controller).
Then you will have all autoloaded classes available in your script.
<?php
require_once __DIR__ . '/vendor/autoload.php';
$htmlDom = new simple_html_dom_node();
For reference, see https://getcomposer.org/doc/01-basic-usage.md#autoloading.

apparently emanueleminotto/simple-html-dom doesn't use a namespace so by default uses the global namespace. the clean solution would be to include the vendor/autoload.php (created/generated/updated by composer) and use the classes/functions by prepending \, to indicate the global namespace ... unless you work in the global namespace yourself, in which case you don't have to prepend.

You should be able to just use them. If I see that right, the whole package is really only one file which is autoloader by composer.
If you include the vendor/autoload.php file in your PHP Script, you should be good to go with the classes in the package.

Related

I used the TwitterOAuth library and got this error

Fatal error: Uncaught Error: Class 'Composer\CaBundle\CaBundle' not found in
C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php:572 Stack trace:
#0 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(620): Abraham\TwitterOAuth\TwitterOAuth->curlOptions()
#1 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(557): Abraham\TwitterOAuth\TwitterOAuth->request('https://api.twi...', 'GET', 'Authorization: ...', Array, false)
#2 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(490): Abraham\TwitterOAuth\TwitterOAuth->oAuthRequest('https://api.twi...', 'GET', Array, false)
#3 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(467): Abraham\TwitterOAuth\TwitterOAuth->makeRequests('https://api.twi...', 'GET', Array, false)
#4 C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php(232): Abraham\TwitterOAuth\TwitterOAuth->http('GET', 'h in C:\xampp\htdocs\CompleteWDC\cha10_APIs\twitter-api\twitteroauth\src\TwitterOAuth.php on line 572
I'm using it for the first time, so checked the TwitterOAuth.php file and line 572 then vscode says the CaBundle is undefined Type Undefined type 'Composer\CaBundle\CaBundle'. , I tried downloading the file over and over again but the same error.
I had the exact same problem following the instructions on here.
It turns out I wasn't including the right autoload. For those coming across the same problem:
Install composer locally on your directory as per these instructions.
Run php composer require abraham/twitteroauth.
Run php composer install.
Make sure to include the right path to the composer autoloader file, not the package autoloader, in your PHP code require "vendor/autoload.php";.
In each file that you will be using the package, don't forget to include the line use Abraham\TwitterOAuth\TwitterOAuth as TwitterOAuth;, so that you can use $foo = new TwitterOAuth();
I hope this helps those who might be new to composer like me.

How to use logger without composer and namespaces?

I am trying to install Mangopay https://github.com/Mangopay/mangopay2-php-sdk, but the recommended solution of installing it without composer doesn't work. I used this line:
require_once "mango/mangopay2-php-sdk-2.11.0/MangoPay/Autoloader.php";
$api = new MangoPay\MangoPayApi();
The file get's loaded, but I get the following error:
Fatal error: Uncaught Error: Class 'Psr\Log\NullLogger' not found in
/usr/local/apache2/htdocs/MangoPay/MangoPayApi.php:223 Stack trace:
#0 /usr/local/apache2/htdocs/pay.php(5): MangoPay\MangoPayApi-
>__construct() #1 {main} thrown in
/usr/local/apache2/htdocs/MangoPay/MangoPayApi.php on line 223
The issue is with the logger, which I manually downloaded and copied into the directory but it still doesn't work. I am only asking here because I am not getting any response from the developers.
Any help with manually installing the logger and getting the namespace to work with mangopay is welcome.

composer can not find classes and load them

I am using a Dropbox api package and installed it with Composer.When I try to use classes that gives me a fatal error that can not find classes.
This is my composer.json
{
"require": {
"kunalvarma05/dropbox-php-sdk": "^0.2.1"
}
}
This is my php file
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
$app = new DropboxApp("client_id", "client_", 'access_token');
//Configure Dropbox service
$dropbox = new Dropbox($app);
//Get File Metadata
$fileMetadata = $dropbox->getMetadata("/helloworld.txt");
//File Name
// $fileMetadata->getName();
printf($fileMetadata->getName());
My php version is 7.2.4 and the error is:
Fatal error: Uncaught Error: Class 'Kunnu\Dropbox\DropboxApp' not found in D:\MeHDi\Projects\DropBox Api\Upload.php:6
Stack trace:
#0 {main}
thrown in D:\MeHDi\Projects\DropBox Api\Upload.php on line 6
Have you remembered to import composers autoload file using require "path/to/vendor/autoload.php";
This is required in order to initialize and use the different composer libraries. Remember to change the filepath to where your vendor/autoload.php file is located.

Google api php client

I'm trying to implement the google analytics API on localhost site using xampp on Mac OS.
but Im getting this error:
Fatal error: Uncaught Exception: This library must be installed via composer or by downloading the full package. See the instructions at https://github.com/google/google-api-php-client#installation. in /Applications/XAMPP/xamppfiles/htdocs/ga-api/google-api-php-client/autoload.php:14 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/ga-api/index.php(4): require_once() #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/ga-api/google-api-php-client/autoload.php on line 14
Can anyone help me with this?
Given that you installed the library using Composer, it will be installed to vendor and will be available in the autoload.php that Composer generates.
I will require_once DIR. '/vendor/autoload.php'; in index.php (preferred) or where I need to instantiate the GoogleClient class.
Ensure that the path to client credentials are rightly referenced when you set auth config like so:
Google_Client->setAuthConfig($pathToCredentials)
Reference:
Setting auth credentials:
https://github.com/google/google-api-php-client#authentication-with-oauth
Autoloading classes:
https://getcomposer.org/doc/01-basic-usage.md#autoloading

PHPunit in zendframework 2

i'm trying to use phpunit with zendframework and i'm follwing the tutorial in
https://media.readthedocs.org/pdf/zf2/latest/zf2.pdfhere is my
bootstrap.php
<?php
chdir(dirname(__DIR__));
include __DIR__ . '/../init_autoloader.php';
here is my IndexControllerTest.php
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/C:/wamp/www/zf2/config/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/'); // this is line 20
$this->assertResponseStatusCode(200);
$this->assertModule('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
and i'm getting the follwing errors
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
Fatal error: Class 'Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase'
not found in C:\wamp\www\zf2\module\Application\test\ApplicationTest\Controller
\IndexControllerTest.php on line 8
i think that's a path probleme (auloading)but i don't know how to fix
any one can help me please ?
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
This warning is telling you that it can't find the location of your init_autoloader.php file. Assuming that file is located in the root of your ZF2 project (so C:\wamp\www\zf2) as is convention, you need to change:
include __DIR__ . '/../init_autoloader.php';
to
include __DIR__ . '/../../../init_autoloader.php';
EDIT Continued...
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to
load ZF2. Run php composer.phar install or define a ZF2_PATH environment
variable.' in C:\wamp\www\zf2\init_autoloader.php:48
Your init_autloader.php file is having trouble finding your ZF2 library autoloader. As you're using composer. Add
"zendframework/zendframework": "2.1.*",
to your "require" section in composer.json if its not already there. Run composer and update your vendor libraries with
php composer.phar update
Try run the application again and see if it works. It may do depending what is included in your init_autoload.php file. If you're still having problems add the following to init_autoloader.php
if(file_exists('vendor/autoload.php'))
{
$loader = require 'vendor/autoload.php';
}
Here's the Fix for the fatal error.
You must be missing the 'zend-test' package in your application.
$ composer require zendframework/zend-test

Categories