PHP PhantomJS自定义脚本

时间:2017-05-09 18:43:24

标签: php phantomjs

我想解析一个网站,但首先必须修改它(通过运行一些JavaScript事件,例如打开折叠框)。我尝试用PHP PhantomJS解决它。但它不起作用。

<?php

use JonnyW\PhantomJs\Client;
use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;

$location = __DIR__ . "/../web/phantomjs";

$serviceContainer = ServiceContainer::getInstance();

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

$client = Client::getInstance();
$client->isLazy();
$client->setProcedure('procedure');
$client->getProcedureLoader()->addLoader($procedureLoader);

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

$request->setTimeout(5000);
$request->setMethod('GET');
$request->setUrl('http://jonnyw.me');

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

if($response->getStatus() === 200) {
    echo $response->getContent();
}

procedure.proc

var page  = require('webpage').create();

page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) 
    // do something
    phantom.exit(1);
});

phantom.onError = function(msg, trace) {
    phantom.exit(1);
};

我错过了什么?

0 个答案:

没有答案