gpt4 book ai didi

php - 在php中调用node.js(nightmarejs)脚本

转载 作者:行者123 更新时间:2023-12-03 13:23:42 25 4
gpt4 key购买 nike

我的问题很安静,我从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中搜索文件,可以使用,其中命令:

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

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

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

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

在Linux下,您可以 找到找到命令。

关于php - 在php中调用node.js(nightmarejs)脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com