test3cron.php
:
<?php
@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:[email protected]
相同的脚本,通过 shell 脚本运行它(输出)没有显示任何内容(空)。
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$
答案1
php 的 cli 版本与 cgi 有几个不同之处。其中之一就是会打印 headers。
由于您的代码启动了一个不存在的会话,然后检查 $_SESSION['state'] 的给定值,因此它将始终以header('Location: ' ...)
不输出任何内容的代码结束,因为 cli 没有执行标题。