Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从phantomJS获取数据_Php_Phantomjs_Php Phantomjs - Fatal编程技术网

如何在PHP中从phantomJS获取数据

如何在PHP中从phantomJS获取数据,php,phantomjs,php-phantomjs,Php,Phantomjs,Php Phantomjs,我有一个自定义的phantomJS过程,我正在用它调用。通过这个过程,我想加载内容并提取呈现页面的一些信息 从命令行调用过程并将“找到的信息”记录到控制台时,执行工作正常。但是现在我想在我的php脚本中获取这些信息,以便进一步处理 如何将过程中的信息返回给调用php脚本 PHP $location = '/var/www/vhosts/boos/procs'; $serviceContainer = ServiceContainer::getInstance(); $procedureLoa

我有一个自定义的phantomJS过程,我正在用它调用。通过这个过程,我想加载内容并提取呈现页面的一些信息

从命令行调用过程并将“找到的信息”记录到控制台时,执行工作正常。但是现在我想在我的php脚本中获取这些信息,以便进一步处理

如何将过程中的信息返回给调用php脚本

PHP

$location = '/var/www/vhosts/boos/procs';

$serviceContainer = ServiceContainer::getInstance();

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

$client = Client::getInstance();
$client->getEngine()->setPath('/usr/local/share/phantomjs/bin/phantomjs');
$client->getEngine()->addOption('--web-security=false');
$client->setProcedure('extractor');
$client->getProcedureLoader()->addLoader($procedureLoader);

$request  = $client->getMessageFactory()->createRequest();
$request->setUrl('http://website.de');

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

$client->send($request, $response);
// GIVE ME THE RESPONSE HERE
程序

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

// settings
page.settings.loadImages = false;
page.settings.localToRemoteUrlAccessEnabled = true;
page.settings.webSecurityEnabled = false;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 FirePHP/4Chrome';
page.viewportSize = {
  width: 1440,
  height: 900
};

t = Date.now();

page.open('{{ input.getUrl() }}', function(status) {
  if (status == 'success') {
        // Evaluate page
        var links = page.evaluate(function() {
            // GET ALL LINKS FROM DOM 
        }

        // RETURN THE LINKS TO THE PHP SCRIPT

        phantom.exit();

  }
});

我认为这是对图书馆的极端利用,它的目的只是

通过PhantomJS headless浏览器加载页面并返回页面响应

模板设计用于设置参数,同时您尝试在那里实现一些逻辑

如果确实需要这样做,可以使用未记录的
response
对象。在
$client->send($request,$response)之后,将在
$response
(php)中提供分配给
响应的属性(js过程)


查看默认模板中如何使用它,例如..

我最近发布了一个项目,该项目允许PHP访问浏览器。在这里获取:

就像您当前使用的库一样,它也依赖于PhantomJS

下载并安装后,只需使用以下代码:

$myUrl          = "http://website.de";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//set the size of the window
$windowObj->setSize(1440, 900);

//now you can either retrive the DOM and parse it, like this:
$domData    = $windowObj->getDom();

//or you could write a function in javascript and then trigger it, like this:
$script = "function getLinks() {
                var array = [];
                var links = document.links;
                for(var i=0; i<links.length; i++) {
                    array.push(links[i].href);
                }
                return JSON.stringify(array);
            }";
//load the script on the page
$windowObj->loadJS($script);
//call the function
$scriptReturn   = $windowObj->callJSFunction("getLinks");
//convert the Json to an array of links
$linkArr        = json_decode($scriptReturn, true);
$myUrl=”http://website.de";
$windowObj=\MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);
//设置窗口的大小
$windowObj->setSize(1440900);
//现在,您可以检索DOM并对其进行解析,如下所示:
$domData=$windowObj->getDom();
//或者您可以用javascript编写一个函数,然后触发它,如下所示:
$script=“函数getLinks(){
var数组=[];
var links=document.links;

for(var i=0;i)显示了一些代码。问题出在哪里还不太清楚。在响应Classis中没有get()方法。我很确定,但我明白你的意思。不知怎的,它变成了。
$myUrl          = "http://website.de";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//set the size of the window
$windowObj->setSize(1440, 900);

//now you can either retrive the DOM and parse it, like this:
$domData    = $windowObj->getDom();

//or you could write a function in javascript and then trigger it, like this:
$script = "function getLinks() {
                var array = [];
                var links = document.links;
                for(var i=0; i<links.length; i++) {
                    array.push(links[i].href);
                }
                return JSON.stringify(array);
            }";
//load the script on the page
$windowObj->loadJS($script);
//call the function
$scriptReturn   = $windowObj->callJSFunction("getLinks");
//convert the Json to an array of links
$linkArr        = json_decode($scriptReturn, true);