我正在学习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();
});发布于 2017-03-04 06:59:48
我想这一定是文档中的相关页面:http://jonnnnyw.github.io/php-phantomjs/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文件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);
});https://stackoverflow.com/questions/42588879
复制相似问题