设置在php中执行的phantomjs的超时

时间:2017-03-24 18:27:32

标签: javascript php phantomjs

我正在通过像这样的幻影收集很多网页:

var webPage = require('webpage');
var page = webPage.create();

var system = require('system');
var args = system.args;
var url=args[1];

page.open(url, function(status) {
    console.log(page.content);
    phantom.exit();
});

以下是执行幻像的代码:

exec ('phantomjs ' . $phantom_script. ' '.$in,$out);

这个exec是big for的一部分,有时它会停止执行幻像。我的问题是:是否有可能为此exec设置超时的解决方案?

类似的东西:

setTimeout(x);
exec ('phantomjs ' . $phantom_script. ' '.$in,$out);
if(timeout){
    echo "error\n";
}
else{
    echo "success\n";
}

1 个答案:

答案 0 :(得分:1)

好吧我明白了。我在javascript中暂停了phantomjs,而不是在PHP中,但它可以工作,这就是我需要的。

这是幽灵的代码:

var webPage = require('webpage');
var page = webPage.create();

var system = require('system');
var args = system.args;
var url=args[1];

page.settings.resourceTimeout = 10000;   //wait 10 seconds

page.onResourceTimeout = function(e) {
    console.log(false);
    phantom.exit(1);
};

page.open(url, function(status) {
    console.log(page.content);
    phantom.exit();
});