PHPタイマー+CURLタイミングタスク完了

2557 ワード

//      
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);
    }

}