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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度