php - 在终端中运行 php 脚本时得到空输出

标签 php shell rest cron sh

以下代码在 netbeans 中运行良好,但在使用 shell 脚本的终端中运行不正常。如果有人有任何想法,请分享。

test3cron.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

@ob_start();
session_start();
$callbackUrl = "http://mgstore/test3cron.php";
$temporaryCredentialsRequestUrl = "http://mgstore/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://mgstore/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://mgstore/oauth/token';
$apiUrl = 'http://mgstore/api/rest';
$consumerKey = 'd9a371ca7661bee18de8d75f50b7e386';
$consumerSecret = '51486d1b2bd688a927a3eced64ed803e';
error_reporting(E_ALL ^ E_NOTICE);

if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}
try {
    $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
    $oauthClient->enableDebug();
    $oauthClient->disableSSLChecks();


    if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);

        exit;
    } else if ($_SESSION['state'] == 1) {

        $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
        $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $accessToken['oauth_token'];
        $_SESSION['secret'] = $accessToken['oauth_token_secret'];
        header('Location: ' . $callbackUrl);

        exit;

    } else {

        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
        $resourceUrl = $apiUrl . "/customers?page=400&limit=1";
        $oauthClient->fetch($resourceUrl, array(), 'GET', array("Content-Type" => "application/json", "Accept" => "*/*"));
        $customerList = json_decode($oauthClient->getLastResponse());

    }
    if (is_array($customerList) || is_object($customerList)) {
        foreach ($customerList as $object) {
            $entityid = $object->entity_id;
            echo "ENTITY ID:" . "$entityid" . "<br>";

            $name1 = $object->firstname;
            $name2 = $object->lastname;
            $customersince = $object->created_at;
            echo "Created at:$customersince<br>";
            $name = $name1 . " " . $name2;
            echo "NAME:" . "$name" . "<br>";
            $email = $object->email;
            echo "EMAIL:" . "$email" . "<br>";
        }
    }
} catch (OAuthException $e) {
    print_r($e);
}

当我在 netbeans(shift+f6) 中运行上面的脚本时,我得到以下输出:

ENTITY ID:167
Created at:2015-07-31 12:37:04
NAME:shahid md
EMAIL:shahid@gmail.com

同样的脚本,我尝试在终端中运行如下,但它显示空输出。

cron.sh

#!/bin/bash

SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
PHP='/usr/bin/php'
# tailf /var/log/cron

cd $SCRIPTPATH
# TO RUN ALL CORN JOBS

$PHP -q test3cron.php

以下命令用于运行脚本文件: 但我在这里什么也没得到。

rizwan@rizwan-Inspiron-3520:~/PHP-workspace/mgstore$ ./cron.sh

rizwan@rizwan-Inspiron-3520:~/PHP-workspace/mgstore$

我还尝试了以下链接:

https://magento.stackexchange.com/questions/75414/running-the-magento-rest-api-php-example

  rizwan@rizwan-Inspiron-3520:~/PHP-workspace/mgstore$ php -S
 localhost:8088 test3cron.php

**Response as follows:**

 PHP 5.5.9-1ubuntu4.11 Development Server started at Tue Aug 11
 10:32:54 2015 Listening on http://localhost:8088 Document root is
 /home/rizwan/PHP-workspace/mgstore Press Ctrl-C to quit.

最佳答案

您已经通过@ob_start(); 启用了输出缓冲,但您永远不会结束输出缓冲(ob_end_flush();)。此外,设置 error_reporting(E_ALL ^ E_NOTICE); 会禁用报告 NOTICE 错误(只需将其设置为 -1)。

您还从 CLI 脚本访问 $_SESSION。这可以正常工作,但它可能无法达到您的预期。

但是,在我看来,这里实际发生的情况是,您只是进入了一条不产生任何输出的执行路径。看这里:

if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);

        exit;

因为这不是一个 HTTP 请求,$_GET['oauth_token'] 将不会被设置,并且 $_SESSION['state'] 将是 0 因此是错误的。它所做的只是获取 token 、输出 header 并退出脚本。我敢打赌,如果您在代码的那个分支中放置一个 echo,您会看到它被打印出来。我推荐的调试操作过程是在整个过程中添加 echo 语句并跟踪代码的执行路径。

关于php - 在终端中运行 php 脚本时得到空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31869822/

相关文章:

php - 带查询的日期数组

php - 合并2个表字段并插入到同一记录中的另一个字段

最后添加一个新列的 shell 脚本

macos - 从 OSX Automator shell 脚本运行 Grunt

java - 将 Jparepository 的 FindAll 方法与 ModelAndView 结合使用

javascript - 使用 codeigniter 从下拉列表中填充文本框

Php 框架或模板引擎或其他东西?

linux - 如何在 bash shell 中重定向命令输出?

azure - 如何使用 postman 的多个参数触发Azure数据工厂管道

java - Spring MVC中JSON body如何转换为POJO