【发布时间】:2020-03-03 11:41:57
【问题描述】:
在我的 php 脚本中,我通过 shell_exec() 调用另一个 php scipt。
(第二个脚本只有在 shell 中执行时才有效,我不知道为什么,但在这里应该没关系。)
这是调用另一个的主脚本:
$result = shell_exec( 'php /html/wp-content/plugins/neuhof/includes/sync/load_products.php' );
$data = json_decode($result, true);
foreach($data as $product) {
if($product['ID'] !== NULL) {
$wc_product = wc_get_product( $product['ID'] );
?>
<tr id="<?php echo $product['Xhartid']; ?>">
<td class="title column-title has-row-actions column-primary page-title"><strong><?php echo $wc_product->get_name(); ?></strong>
<div class="row-actions">ID: <?php echo $product['ID']; ?> | <span class="view"> <a href="<?php echo get_permalink( $product['ID'] ); ?>" target="_blank"> Anschauen </a> </span></div>
</td>
<td><?php echo $product['operation']; ?></td>
<td class="state">Warten auf WP-Cron...</td>
</tr>
<?php
} else {
?>
<tr id="<?php echo $product['Xhartid']; ?>">
<td class="title column-title has-row-actions column-primary page-title"><strong><?php echo $product['Xhartbez']; ?></strong>
<div class="row-actions">Noch keine ID vergeben</div>
</td>
<td><?php echo $product['operation']; ?></td>
<td class="state">Warten auf WP-Cron...</td>
</tr>
<?php
}
}
这是应该在 shell 中执行的脚本:
<?php
ini_set('display_errors', 0);
require_once( 'databases.php' ); // Notwendige Datenbankverbindungen
define('SHORTINIT', true); // load minimal WordPress
require_once '/html/wp-load.php'; // WordPress loader
// Mit dieser Funktion werden die Datenbank IDs aus dem ERP nutzbar gemacht
function decodeID($id) {
$unpacked = unpack('Va/v2b/n2c/Nd', $id);
return sprintf('%08X-%04X-%04X-%04X-%04X%08X', $unpacked['a'], $unpacked['b1'], $unpacked['b2'], $unpacked['c1'], $unpacked['c2'], $unpacked['d']);
}
$sql = "
SELECT
Xhartid,
Xbearbdat,
Xhartbez
FROM cms.dbo.xHauptartikel
WHERE Xinternet = '1'
ORDER BY Xhartid ASC
";
$erp_ids = $GLOBALS['erp']->query($sql)->fetchALL();
// Code für Abfrage mit WordPress ID
$sql = "
SELECT distinct
A.post_id as 'ID',
A.meta_value as 'Xhartid',
B.meta_value as 'Xbearbdat'
FROM
wp_postmeta A,
wp_postmeta B
WHERE
A.meta_key = 'Xhartid'
AND
B.meta_key = 'Xbearbdat'
AND
A.post_id = B.post_id
order by Xbearbdat
asc
";
$b = $GLOBALS['cms']->query($sql)->fetchALL();
foreach( $erp_ids as $keya => $a ) {
foreach( $b as $key => $row ) {
if(decodeID($a['Xhartid']) == $row['Xhartid'] ) {
if($a['Xbearbdat'] == $row['Xbearbdat']) {
// Ist akutell
} else {
// Aktualisieren
$list[decodeID($a['Xhartid'])]['operation'] = 'Aktualisieren';
$list[decodeID($a['Xhartid'])]['ID'] = $row['ID'];
$list[decodeID($a['Xhartid'])]['Xhartid'] = $row['Xhartid'];
}
unset($b[$key]);
unset($erp_ids[$keya]);
}
}
}
// Erstellen
foreach($erp_ids as $row) {
$list[decodeID($row['Xhartid'])]['operation'] = 'Erstellen';
$list[decodeID($row['Xhartid'])]['ID'] = NULL;
$list[decodeID($row['Xhartid'])]['Xhartid'] = decodeID($row['Xhartid']);
$list[decodeID($row['Xhartid'])]['Xhartbez'] = $row['Xhartbez'];
}
// Löschen
foreach($b as $row) {
$list[$row['Xhartid']]['operation'] = 'Löschen';
$list[$row['Xhartid']]['ID'] = $row['ID'];
$list[$row['Xhartid']]['Xhartid'] = $row['Xhartid'];
}
if($argv[1] == 'count') {
$ids = 0;
foreach( $list as $product ) {
$ids++;
}
echo $ids;
} elseif ($argv[1] == 'list') {
$ids = NULL;
foreach( $list as $product ) {
$ids[] = $product['Xhartid'];
}
echo json_encode($ids, JSON_UNESCAPED_UNICODE);
} else {
echo json_encode($list, JSON_UNESCAPED_UNICODE);
}
脚本返回NULL,即使我尝试exec() 并得到错误,它们也是NULL。
我不知道为什么这不起作用,因为 shell_exec('ls') 和简单的“hello world”脚本运行良好!
【问题讨论】:
-
请问为什么要通过shell执行PHP文件而不是仅仅包含文件并执行所需的功能?
-
它仅在 shell 中运行时有效,因为我需要连接到 MSSQL 服务器,而该连接只能在 shell 中工作。我不知道为什么,房东也不能告诉我... :-/