使用curl响应为null时在数据中循环循环

时间:2018-10-31 06:44:56

标签: php curl

Am使用curl从支付网关获取数据。我正在curl函数中编写一些逻辑,从而当响应为null时,我想继续尝试5次以上到达curl函数中的支付网关,直到状态更改,但仍无法正常工作

卷曲功能

 public function global_Curl_payStat($data, $url, $try = 1)
    {
        //dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //Prevents usage of a cached version of the URL
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
        //Listen for HTTP errors
        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        //dd($statusCode); 
        $response = json_decode(curl_exec($ch));
        dd($response);
        curl_close($ch);

        if($response == null && $try < 5) {
                //Wait a 3 seconds before trying again
                sleep(3);
                return $this->global_Curl($data, $url, ++$try);
        }

        return $response;
    }

函数调用下面的curl函数

//Set maximum execution time
    ini_set('max_execution_time ', 75);
    set_time_limit(75);
    //Loop through the code 3 times
    for($try=1; $try<=3; $try++) {
        sleep(15);
        $payStat = $this->global_Curl_payStat($data, 'api/payment/status')->data;
         //Call the curl function below
        //$payStat = $this->global_Curl($data, 'api/payment/status?cachebuster='. microtime(true))->data;
        var_dump($payStat);
        dd($payStat);
        if ($payStat->status === 1) {
            return 'true';
        }
    }

0 个答案:

没有答案