【问题标题】:shell_exec returns NULL but works in shellshell_exec 返回 NULL 但在 shell 中工作
【发布时间】: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 中工作。我不知道为什么,房东也不能告诉我... :-/

标签: php wordpress shell


【解决方案1】:

(第二个脚本只有在 shell 中执行时才有效,我不知道为什么,但这在这里应该无关紧要。)

这可能是因为您提到的第二个脚本的 MSSQL 连接在您的网络服务器环境/网络服务器使用的 php 配置中被阻止。

当您从 shell 运行第二个脚本时,它可能工作正常,因为它在不同的环境中工作。但是,当您在 web 服务器运行的 php 脚本中使用 shell_exec 运行第二个脚本时,它会从被阻止的相同 web 服务器环境启动。

因此,如果您尝试使用 shell_exec 作为“hack”来忽略您的网络服务器环境中的限制,那么它将无法正常工作。

您应该联系您的服务器管理员来解决此问题。

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多