我正在学习phantomjs和php-phantomjs。我想将脚本传递给phantomjs。
目前我正在尝试:
$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();
}
呼叫$client->send($request, $response)
后,我会收到一个空的$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();
});
我认为这必须是文档中的相关页面:http://jonnnnyw.github.io/php-phantomjs/4.0/4.0/4-custom-scripts/
这是有效的代码:
在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();
}
在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);
});