首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过PHP-PhantomJS将脚本传递给PhantomJS的正确方式是什么?

通过PHP-PhantomJS将脚本传递给PhantomJS的正确方式是什么?
EN

Stack Overflow用户
提问于 2017-03-04 05:39:59
回答 1查看 2.1K关注 0票数 1

我正在学习PhantomJS和PHP-PhantomJS。我想把一个脚本传给PhantomJS。

目前我正在尝试这样做:

代码语言:javascript
复制
   $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对象。

代码语言:javascript
复制
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();
});
EN

回答 1

Stack Overflow用户

发布于 2017-03-04 06:59:48

我想这一定是文档中的相关页面:http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/

以下是正在运行的代码:

在PHP中:

代码语言:javascript
复制
    $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

代码语言:javascript
复制
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);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42588879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档