Home » Php » PHP PhantomJS not loading in class through Composer

PHP PhantomJS not loading in class through Composer

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve followed the installation guide for PHP PhantomJS. When running a test script using PHP PhantomJS, I’m getting the error:

PHP Fatal error: Class 'JonnyW\PhantomJs\Client' not found in ...

I haven’t used Composer before, so I maybe overlooking something. I’m running this from MAMP, so there may be some specifics to do with that that are not mentioned in the documentation. If I open the test script in a browser I get a blank screen. It’s only from running the php from Terminal that I get the Fatal error.

The line the script fails on is:

$client = Client::getInstance();

I therefore presume that it is not loading properly from Composer. I can verify that in /bin are both phantomjs and phantomloader.

What steps should I take to get the PHP PhantomJS script loaded correctly?

–update–

test.php (taken directly from PHP PhantomJS example)

use JonnyW\PhantomJs\Client;

$client = Client::getInstance();

$request  = $client->getMessageFactory()->createRequest();
$response = $client->getMessageFactory()->createResponse();

$request->setMethod('GET');
$request->setUrl('http://google.com');

$client->send($request, $response);

if($response->getStatus() === 200) {
    echo $response->getContent();
}
How to&Answers:

You will have to include the composer autoloader in your script if you dont use one yourself.

require 'vendor/autoload.php';

This is an autogenerated autoload script by composer.
See here https://getcomposer.org/doc/01-basic-usage.md#autoloading

I’m afraid the Use statement will not take care of the autoloading, it will just define the namespace to look in for the Client class.