I can't seem to be able to display a response from my custom .proc file. I'm running php-phantomjs on laravel 5.6 on ubuntu. I've got a URL endpoint I'm using to invoke the custom file as follows:
public function getLinks()
{
$location = base_path('phantom-scripts');
$serviceContainer = ServiceContainer::getInstance();
$procedureLoader = $serviceContainer->get('procedure_loader_factory')
->createProcedureLoader($location);
$client = Client::getInstance();
$client->getEngine()->setPath(\Config::get('phantomPath'));
$client->setProcedure('my_procedure');
$client->getProcedureLoader()->addLoader($procedureLoader);
$request = $client->getMessageFactory()->createRequest();
$response = $client->getMessageFactory()->createResponse();
$client->send($request, $response);
dd($response->getContent());
}
Here's the .proc file
var page = require('webpage').create();
var fs = require('fs');
console.log("loading...");
var url = 'http://www.example.com';
page.open(url, function (status) {
if (status === "success") {
var contentNeeded = page.evaluate(function () {
return document.querySelector("h1").outerHTML;
});
console.log(contentNeeded);
}
});
phantom.exit();
I'd like to display the 'contentNeeded' on my browser.
PhantomJS exits too early in your script.
page.opencalls are asynchronous (who knows how much time opening that url will take), sophantom.exit()runs right after the browser's only started opening that url. Instead exit PhantomJS after getting the info, in callback: