2017-03-03 172 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

相關問題