Web scraping 通过PHP PhantomJS将脚本传递给PhantomJS的正确方法?

Web scraping 通过PHP PhantomJS将脚本传递给PhantomJS的正确方法?,web-scraping,phantomjs,php-phantomjs,Web Scraping,Phantomjs,Php Phantomjs,我正在学习PhantomJS和PHP PhantomJS。我想向PhantomJS传递一个脚本 目前我正在尝试: $client->getEngine()->addOption('/Applications/myWebApp/js/phantomtest.js'); $request = $client->getMessageFactory()->createRequest('http://www.jonnyw.me/', 'GET'); $res

我正在学习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();
    }
在调用
$client->send($request,$response)
之后,我得到了一个空的
$response
对象


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

下面是正在工作的代码:

在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
中:

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

看起来很有趣,但我还是用backticks@pguardiario,使用反勾号的正确方法是什么?
或确保您捕捉到值:
我不知道您可以这样做。请将链接中的信息添加到您的答案中(以防该页稍后消失)@Vaviloff这是一整页信息。在这里发布的内容太多了?当然不是,只是粘贴代码示例以从该库运行自定义脚本。一定有办法做到这一点,对吗?@Vaviloff昨天我没有让它起作用,即使有文档——文档似乎遗漏了一个步骤,或者至少没有足够明显地表现出来。但从那以后我就开始工作了。根据你的要求,我正在用工作代码更新我的答案。很高兴你找到了答案并在这里展示了它,投了赞成票!希望将来对此感到疑惑的人都能找到你的答案。。。但我的天啊,这个图书馆是不是设计过度了!
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);
});