2017-08-01 20:29
浏览 199


I'm trying to run a headless browser and run JS scripts (a bot) within it using but want to run it using php. Searching on Google, I found a implementation / wrapper of PhantomJS as php-phantomjs. Please bear with me, I'm very new to this stuff.

Here what I'm trying to do is to take screenshot of alert window (this is not necessary, but just to test if JS is executed and then the screenshot is taken.)

Here is my code:

// file: app.php

$client = PhantomJsClient::getInstance();

$location = APP_PATH . '/partials/';

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


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

$request->setViewportSize(1366, 768);
    'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'


$request->setOutputFile(APP_PATH . '/screen.jpg');

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

Tried two custom scripts as given in the list here: http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

// file: page_on_resource_received.partial, page_open.partial


OUTPUT: It just shows the page, not the alert window.

I repeat, Its not about taking screenshot, that's just to be sure that JS is executing. I just want to execute my JS scripts (better say bots) like:

var form = document.getElementById('form');
var event = new Event('submit');

or maybe using jQuery and then return the output of that page to php as response or so. So, if there is any other way to run bots using php in a headless browser please mention that in your answers or comments.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试运行无头浏览器并在其中运行JS脚本(机器人),但想要运行 它使用PHP。 在Google上搜索,我发现PhantomJS的实现/包装为php-phantomjs。 请耐心等待我,我对这些东西很新。

这里我要做的就是截取警报窗口的截图(这不是必需的,只是为了测试 如果执行JS然后截取屏幕截图。)


  // file:app.php \  n 
 $ client = PhantomJsClient :: getInstance(); 
 $ client-> isLazy(); 
 $ location = APP_PATH。  '/ partials''; 
 $ serviceContainer = ServiceContainer :: getInstance(); 
 $ procedureLoader = $ serviceContainer-> get('procedure_loader_factory') - > createProcedureLoader($ location); 
 $  client-> getProcedureLoader() - > addLoader($ procedureLoader); 
 $ request = $ client-> getMessageFactory() - > createCaptureRequest(); 
 $ response = $ client-> getMessageFactory(  ) - > createResponse(); 
 $ request-> setViewportSize(1366,768); 
 $ request-> setHeaders([
'User-Agent'=>'Mozilla / 5.0(X11)  ; Linux x86_64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 57.0.2987.98 Safari / 537.36'
 $ request-> setTimeout(5000); 
 $ request-> setUrl(  'https://www.google.co.in'); 
 $ request-> setOutputFile(APP_PATH。'/ screen.jpg'); 
nnnnclient-> send($ request,  $ response); 

尝试了两个自定义脚本,如下表所示: http://jonnnnyw.github.io/php-phantomjs/4.0/4- custom-scripts /

  // file:page_on_resource_received.partial,page_open.partial 
  <  / pre> 


我再说一遍,它不是关于截屏, 这只是为了确保JS正在执行。 我只想执行我的JS脚本(更好的说是机器人),如:

  var form = document.getElementById('form'  ); 
var event = new Event('submit'); 

或者可能使用jQuery然后返回该输出 页面到PHP作为响应左右。 因此,如果在无头浏览器中使用php运行机器人有任何其他方式,请在您的答案或评论中提及。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpjs2005 2017-08-02 05:15

    PhantomJS is headless, that is it doesn't have GUI. Therefore no window dialogs can be seen.

    Try writing custom text to an element instead of alert, like

    document.getElementById("#title").innerHTML = "It works!";
    打赏 评论

相关推荐 更多相似问题