2

I'm learning PhantomJS and PHP-PhantomJS. I want to pass a script to PhantomJS.

Currently I'm trying this:

   $client->getEngine()->addOption('/Applications/myWebApp/js/phantomtest.js');
    $request = $client->getMessageFactory()->createRequest('http://www.jonnyw.me/', 'GET');

    $response = $client->getMessageFactory()->createResponse();
    $client->send($request, $response);
    if ($response->getStatus() === 200) {
        echo $response->getContent();
    }

I'm getting an empty $response object back after the call to $client->send($request, $response).

Here's the contents of my test script ('phantomtest.js'):

var page = require('webpage').create();
page.open('http://www.jonnyw.me', function(status) {
  console.log("Status: " + status);
  if(status === "success") {
    page.render('example.png');
  }
  phantom.exit();
});
VikR
  • 4,818
  • 8
  • 51
  • 96

1 Answers1

1

I think this must be the relevant page in the docs: http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

Here is code that is working:

In PHP:

    $location = '/Applications/myWebApp/js/';
    $serviceContainer = ServiceContainer::getInstance();

    $procedureLoader = $serviceContainer->get('procedure_loader_factory')
            ->createProcedureLoader($location);
    $client->getProcedureLoader()->addLoader($procedureLoader);

    $request = $client->getMessageFactory()->createRequest();
    $client->setProcedure('phantomJStest');

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

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

    if (($response->getStatus() === 200) || ($response->getStatus() == 'success')){
        // Dump the requested page content
        echo $response->getContent();
    }

In the proc file phantomJStest.proc:

phantom.onError = function (msg, trace) {
    console.log(JSON.stringify({
      "status": msg
    }));
    phantom.exit(1);
};

var system = require('system');
var uri = "http://www.jonnyw.me";

var page = require('webpage').create();
page.open(uri, function (status) {
    console.log(JSON.stringify({
      "status": status
    }));

    if (status === "success") {
        page.render('example.png');
    }
    phantom.exit(1);
});
VikR
  • 4,818
  • 8
  • 51
  • 96
  • Please add info from the link to your answer (in case that page is gone later) – Vaviloff Mar 04 '17 at 03:06
  • @Vaviloff it's a whole page of info. Too much to post here? – VikR Mar 04 '17 at 04:38
  • Of course not, just paste the code sample to run a custom script from that library. There must be a way to do so, right? – Vaviloff Mar 04 '17 at 10:37
  • @Vaviloff Yesterday I didn't have it working, even with the docs-- the docs appear to be missing a step, or at least not making it evident enough. But since then I have gotten it working. Per your request, I am updating my answer with the working code. – VikR Mar 04 '17 at 18:03
  • It's great that you found the answer and presented it here, upvoted! Hope whoever will wonder about this in the future will find your answer. ...But my god, is this library overengineered! – Vaviloff Mar 04 '17 at 18:09