javascript – 在CLI和node.js脚本中运行时,执行PHP脚本会产生不同的结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在CLI和node.js脚本中运行时,执行PHP脚本会产生不同的结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4238字,纯文字阅读大概需要7分钟。
内容图文
![javascript – 在CLI和node.js脚本中运行时,执行PHP脚本会产生不同的结果](/upload/InfoBanner/zyjiaocheng/766/974868aa65ec4f44b8295ea2e0f1f126.jpg)
我完全迷失了,为什么我的PHP脚本在通过CLI运行时产生不同的结果(产生正确的结果)与通过exec函数在Node.js中运行时产生不同的结果.
这是我的PHP.注意fopen(‘blah.txt’,’w’)行,这是我用来检查脚本是否到达那个地方的行.
Calculator.php中
class calculator{
public static $_var = array();
public function calculate($xml, $xsl, $frequency){
$reader = new DOMDocument;
if(!$reader->load($xsl)) throw new Exception('XSL Could not be loaded: ' . $xsl);
$processor = new XSLTProcessor;
$processor->importStyleSheet($reader);
$processor->registerPHPFunctions();
$processor->setParameter('', 'frequency', $frequency);
$dictionary = new DOMDocument;
if(!$dictionary->load($xml)) throw new Exception('XML Could not be loaded: ' . $xml);
$processor->transformToXML($dictionary);
}
public static function connectNodes($name, $dataset, $processor, $destination, $frequency){
$var = self::$_var;
self::$_var = array();
$test = fopen('blah.txt', 'w');
fclose($test);
ob_start();
require 'templates/' . $processor . '.php';
$xsltSheet = new DOMDocument;
$xsltSheet->loadXML(ob_get_clean());
$xsltProc = new XSLTProcessor;
$xsltProc->importStyleSheet($xsltSheet);
$dataSet = new DOMDocument;
if(is_file('../dataFiles/xml/' . $dataset . '.xml') && calculator::makedirs('../dataFiles/json/' . $destination . '/' . $frequency)){
//proceed only if the dataset exists and the folders are created correctly
$dataSet->load('../dataFiles/xml/' . $dataset . '.xml');
if(!is_file('../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json')){
$filemaker = fopen('../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json', 'w');
fclose($filemaker);
}
$xsltProc->transformToURI($dataSet, '../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json');
} else {
throw new Exception('Either dataset does not exist('. $dataset .'), or directories could not be created (dataFiles/json/'. $destination . '/' . $frequency .')');
}
}
public static function populateVarWithXPath($var){
self::$_var[] = $var;
}
public static function makedirs($dirpath, $mode=0777) {
return is_dir($dirpath) || mkdir($dirpath, $mode, true);
}
}
当我通过CLI运行它时,会在运行脚本的目录中创建blah.txt.但是当我通过Node.js运行它时,它不会创建blah.txt!
这是我的节点脚本:
脚本跑步者
var ipc = require('./ipc');
var exec = require('child_process').exec;
ipc.listen(function(frequency){
if(frequency.toString().match(/integrator /)){
var freq = frequency.toString().replace(/integrator /, '');
console.log('Received message: ' + freq);
var cmd = exec('php use_calculator.php metricsDictionary.xml metricsXmlReader.xsl ' + frequency);
cmd.on('close', function(exitCode, stdout, stderr){
if(exitCode === 0){
console.log('exitcode: ' + exitCode + ' ; stdout: ' + stdout + ' ; stderr: ' + stderr);
} else {
console.log('Try again?');
}
});
cmd.on('error', function(err){
throw err;
});
}
});
这里有趣的是,如果我将我的blah.txt移动到calculate()函数(它将首先运行),CLI和节点都将创建该文件.
更多背景:
use_calculator.php文件将调用calculate()函数并将三个必需参数传递给它.然后,在calculate()中,它将执行XML和XSL文件合并.
在XSL中有关于如何处理XML数据的规则,它的作用是调用connectNodes()函数 – 它将处理新的XML和XSL.这就像XML / XSL的开始.
关键是有一个XML文件列出了应该处理其他XML数据的内容和方式的所有细节.该文件在calculate()中读取.然后,当它计算每个数据时,它以类似inception的方式调用connectNode()函数.
为了让事情更清楚(我希望),这里是调用connectNodes()的metricsXmlReader.xsl
metricsXmlReader.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="metrics">
<xsl:for-each select="metric">
<xsl:if test="frequency = $frequency">
<xsl:for-each select="xpath/var">
<xsl:variable name="pusher" select="php:functionString('calculator::populateVarWithXPath', .)" />
</xsl:for-each>
<xsl:variable name="trigger" select="php:functionString('calculator::connectNodes', name, dataset, processor, destination, frequency)" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
解决方法:
我的第一个猜测是,无论PHP在哪里尝试将文件写入,它都不能.您可以尝试使用/ tmp等所有人的权限的绝对路径.
您还应该确保可以处理并行请求.因为这不规模.如前所述,管道是一种解决方案.
生命值
PS:你试过吗?
内容总结
以上是互联网集市为您收集整理的javascript – 在CLI和node.js脚本中运行时,执行PHP脚本会产生不同的结果全部内容,希望文章能够帮你解决javascript – 在CLI和node.js脚本中运行时,执行PHP脚本会产生不同的结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。