PHP Cron作业进程,URL中有标记


PHP Cron job process with marker in URL

我想在服务器上使用cron每分钟处理一个脚本,但我需要在URL或其他方式中传递一个变量。我已经对此进行了研究,并在cron中看到了使用参数的解决方案,但我认为这与我正在做的不一致。

以下是我要做的:

script.php(每分钟运行一次)

<?php
$marker = $_GET['marker'];
$accountObj = new etAccounts($consumer);
    $request_params = new TransactionHistoryRequest();
    $request_params->__set('count', 50); //how many will be shown
    if($marker_get != ''){
    $request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
    }
        $json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
echo $json; //shows most recent 50 transactions starting from marker value
//process json data here...
//included in json is a marker variable that will be used to return the next 50 json results
//after data is processed reload the page with marker in URL
header('Location: script.php?marker=14293200140265');
?>

我知道cron是服务器端的CLI,它不能处理重定向或标头位置,但这怎么可能呢。我看到有人提到使用CURL,这是怎么回事?实例

将post变量发送到url的简单示例:

$fields = array(
    'id' => $id, 
    'mail' => $mail, 
);
$url = "yourdomain.com"; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);

基于注释,您不需要重定向或使用查询变量。

您可以使用在marker变量不为空时运行的循环:

$your_marker_variable = '';
do {
    $accountObj = new etAccounts($consumer);
    $request_params = new TransactionHistoryRequest();
    $request_params->__set('count', 50); //how many will be shown
    if($marker_get != '') {
      $request_params->__set('marker', $marker_get); //starting point ex. 14293200140265
    }
    $json = $accountObj->GetTransactionHistory($account, $request_obj, $request_params );
    echo $json; //shows most recent 50 transactions starting from marker value
    //process json data here...
    //included in json is a marker variable that will be used to return the next 50 json results
    // set the new value of $your_marker_variable
    $your_marker_variable = ...
} while (!empty($your_marker_variable));

请注意,您发布的脚本中有未定义和未使用的变量,很难看出什么变量用于什么,因此您需要对此进行一些调整。