2017-03-03 176 views
1

我正在学习PhantomJS和PHP-PhantomJS。我想通过一个脚本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(); 
    } 

我得到一个空$response对象调用$client->send($request, $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(); 
}); 
+0

看起来很有趣,但我会尽早使用反引号 – pguardiario

+0

@pguardiario,对此使用反引号的正确方法是什么? – VikR

+0

'<?php \'/ usr/bin/phantomjs /path/to/script.js \'?>'或[exec](http://php.net/manual/en/function.exec.php) – Vaviloff

回答

1

我想这一定是在文档相关页面:

在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正在工作http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

这里是代码:

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); 
}); 
+0

请将链接中的信息添加到您的答案中(如果该页面稍后消失) – Vaviloff

+0

@Vaviloff它是一整页的信息。太多张贴在这里? – VikR

+0

当然不是,只需粘贴代码示例即可从该库运行自定义脚本。必须有办法这样做,对吧? – Vaviloff

相关问题