PHP定时器+CURL完成定时任务

//任务启动状态
public static $open = true;
//任务执行间隔时间 分钟
public static $doTime = 5;
​​​​​​​//定时推送订单任务,需要启动服务后访问以激活此任务。
public function timerSendOrderJobs()
{
    ignore_user_abort();
    set_time_limit(0);
    do{
        if(self::$open == true){
            /*调用方法*/
            $this->pushDatas();
        }
        $interval = self::$doTime*60;
        sleep($interval);
    }while(true);
}

//进行推送
public function pushDatas()
{
    try{
        //查询未推送的订单
        $result = MO::pushQuery();
        //查询活动ID
        $list = M::select($result);
        //判断是否有活动
        if (!empty($list)) {
            foreach ($result as $key => $val) {
                $secret = $val["UserId"].$val["ActivityId"].$list[0]["give_meter"]."doubleonetwo_list";
                $arr = [
                    "Token" => $result["Token"] = "",
                    "V" => $result["V"] = md5($secret),
                    "Type" => $result["Type"] = "",
                    "ReqData" => [
                        "GiveRiceAmount" => $list[0]["give_meter"],
                        "UserId" => $val["UserId"],
                        "ActivityId" => $val["ActivityId"],
                    ],
                ];
                    $url = "http://zb.list.com";
                    $header = array("Content-type: application/json");
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_VERBOSE, '1');
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arr));
                    $result = curl_exec($ch);
                    $data = (array) json_decode($result);
                    if ($data["Success"] == "true") {
                        $upt = MO::upts($val["UserId"]);
                    }
                    curl_close($ch);
            }
            $interval = self::$doTime*60;
            //延迟代码执行
            sleep($interval);
        }
    } catch (Exception $e){
        /*写入日志*/
        file_put_contents("ErrorLog.txt", $e, FILE_APPEND);
    }

}

 

你可能感兴趣的:(PHP定时器+CURL完成定时任务)