Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PhantomJS not loading in class through Composer

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();
}
like image 972
Fisu Avatar asked Mar 09 '15 09:03

Fisu


1 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.

like image 87
ivoba Avatar answered Oct 03 '22 16:10

ivoba