jonnnnyw / php-phantomjs

Execute PhantomJS commands through PHP
MIT License
1.44k stars 431 forks source link

Twig_Error_Runtime? #162

Open VikR0001 opened 7 years ago

VikR0001 commented 7 years ago

I'm learning php-phantomjs and am gettting a Twig_Error_Runtime. Here's my PHP:

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

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

        $request = $client->getMessageFactory()->createRequest();
        $request->setType('phantomjsTest');

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

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

...and here's my .proc file:

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

phantom.exit(1);

What am I missing? Thanks in advance to all for any info.

VikR0001 commented 7 years ago

The docs appear to leave out a required step.

The docs show this as the code required for procedureLoader:


use JonnyW\PhantomJs\Client;
    use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;

    $location = '/path/to/your/script/directory';

    $serviceContainer = ServiceContainer::getInstance();

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

    $client = Client::getInstance();
    $client->getProcedureLoader()->addLoader($procedureLoader);

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

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

But it appears to be necessary to add a call to setProcedure:

$client->setProcedure('phantomjsTest');

With the setProcedure call added, the code is now working!

jonnnnyw commented 7 years ago

Apologies. I will update the documentation.