dssjxvbv918586 2019-01-22 14:39
浏览 200
已采纳

我如何解析php中的phantom.js输出?

I can get output from a phantom.js request from the command line, and the output is what I expect. I would like to call phantom.js from a php script and then parse the output looking for certain content.

My phantom.js looks like:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';

page.open('https://www.aa.com/travelInformation/flights/status/detail?search=AA|1698|2019,1,23&ref=search', function(status) {
if (status !== 'success') {
    console.log('Unable to access network');
} else {
    var ua = page.evaluate(function() {
        return document.getElementById('aa-content-frame').innerHTML;
    });
    console.log(ua);
}
phantom.exit();
});

If I run the following from the command line:

phantomjs phantomjstest.js

I get output similar to:

The default user agent is Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1

<app-root-flight-status data="{&quot;queryString&quot;:&quot;AA|1698|2019,01,23&quot;,
&quot;bffUri&quot;:&quot;https://www.aa.com/flightinfo/v1.0/&quot;,
&quot;email&quot;:&quot;&quot;,
&quot;countryCode&quot;:&quot;&quot;,
&quot;phoneNumber&quot;:&quot;&quot;}" _nghost-c0="" ng-version="6.1.4"><router-outlet _ngcontent-c0=""></router-outlet><app-flight-details _nghost-c1=""><!----><!----><div _ngcontent-c1=""><!----><h1 _ngcontent-c1=""> Flight status</h1>

... blah blah blah ...

What I would like to do is run phantom.js from within a php script like this:

$response = exec('/usr/bin/phantomjs phantomjstest.js');

and then continue down the php script with the code that parses the output.

When I execute my php script:

<?php

$response = exec('/usr/bin/phantomjs phantomjstest.js');

$query = mysqli_query($link, "INSERT INTO test (t_test) VALUES('11 - " . $response  . "')");

echo "Response = <br /><br />".$response;

?>

It appears $response is empty. It is not displayed on the screen and only '11 - ' is added to the database. I assume this is because phantomjstest.js logs 'ua' to the console.

My question is how to get the javascript variable ua to a point where I can parse it in my php script. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongzhangnong2063 2019-01-22 14:43
    关注

    PHP's exec does not return an output.

    It fills in the second argument with command's output, like:

    <?php
    $response = [];
    exec('/usr/bin/phantomjs phantomjstest.js', $response);
    
    var_dump($response);
    

    Please have a look at the official documentation: http://php.net/manual/en/function.exec.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?