PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 17, 2022

[FIXED] PHP PhantomJS not loading in class through Composer

 March 17, 2022     composer-php, mamp, php     No comments   

Issue

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();
}

Solution

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.



Answered By - ivoba
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing