我需要此代码每3小时运行一次,以进行API身份验证调用,以获取临时令牌以用于进行其他API调用。我将其存储在单独的php文件中,并在需要时进行更新
它在浏览器中有效,但在cron作业中无效。我所有其他的cron作业均正常工作,并且格式相同。我想念什么?
*编辑:这是我的计划调用-
/ usr / local / bin / php /home/mysite.com/getToken.php
<?php
Token();
function Token(){
header("Access-Control-Allow-Origin: https://example.com");
header("Access-Control-Allow-Headers: Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS");
header("Access-Control-Allow-Credentials: true");
$params = array("Username" => "fuu", "Password" => "bar", "PrivateKey" => "123456789");
$auth_url = 'https://example.com/api/Rest/authenticate';
$ch = curl_init();
$postData = http_build_query($params);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$postData");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "$auth_url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$token = $data;
curl_close($ch);
file_put_contents({$_SERVER['DOCUMENT_ROOT']} . "/Token.php", $token);
echo $token;
}
?>
答案 0 :(得分:0)
问题是public class Card
{
public string ActId { get; set; }
public string CustId { get; set; }
public string Id { get; set; }
public string Fingerprint { get; set; }
public string Last4 { get; set; }
public int Exp_month { get; set; }
public int Exp_year { get; set; }
public string Brand { get; set; }
}
public static List<Card> JsonToCSV(string JsonContent)
{
var ds = JObject.Parse(JsonContent);
var cards = new List<Card>();
foreach (var item in ds)
{
var crds= JObject.Parse(JsonConvert.DeserializeObject(item.Value["cards"].ToString()).ToString());
foreach (var crd in crds)
{
var card =JsonConvert.DeserializeObject<Card>(crd.Value.ToString());
card.ActId= item.Key;
card.CustId= item.Value["id"].ToString();
cards.Add(card);
}
}
return cards;
}
-通过shell执行PHP脚本时,此为空。
已设置且还包含路径的内容:
$_SERVER['DOCUMENT_ROOT']
但是,如果我是您,我宁愿将其放入数据库中并在其中进行读写。但这只是一个提示。