簡體   English   中英

在php中調用node.js(nightmarejs)腳本

[英]Call node.js (nightmarejs) script in php

我的問題很安靜,我從nodejs / nightmarejs開始,我需要在我的php腳本中調用此js腳本以從中檢索答案。

我正在使用Wamp,並且我的腳本都在同一文件夾中:

C:\wamp64\www\nightmare\index.php
C:\wamp64\www\nightmare\index.js

index.js:

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })

nightmare
  .goto('http://yahoo.com')
  .type('form[action*="/search"] [name=p]', 'nightmare github')
  .click('form[action*="/search"] [type=submit]')
  .wait('#main')
  .evaluate(function () {
    return document.querySelector('#main .searchCenterMiddle li a').href
  })
  .end()
  .then(function (result) {
    console.log(result)
  })
  .catch(function (error) {
    console.error('Search failed:', error);
  });

index.php:

<?php

    exec("index.js", $jsOutput);
    var_dump($jsOutput);

?>

我在其他一些帖子中看到,如果我使用exec()我應該給出整個命令行以使其正常運行,例如:

exec("node index.js", $jsOutput);

我發現我必須將整個路徑都交給nodejs嗎? 但是我沒有找到任何方法從當前文件夾中檢索到nodejs的路徑。 如果有人有任何燈光,將不勝感激。 非常感謝

要在Windows中搜索文件,可以使用where命令:

<?php
    exec('where node', $nodePath);

    if ($nodePath && $nodePath[0]) {

        exec('"' . $nodePath[0] . '" index.js', $jsOutput);

        var_dump($jsOutput);
    }
    else {
        echo 'node not found.';
    }

在Linux下你可以找到find命令。

好吧,我想這是一個常見的錯誤。 安裝完node.js之后,我忘了重啟計算機,必須這樣做才能應用在Windows環境變量中所做的更改,這樣您可以在命令提示符下調用node ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM