donglan9517 2018-10-31 06:44
浏览 60

使用curl在null响应时发出循环数据

Am using curl to fetch data from a payment gateway. Am writing some logic inside the curl function whereby when the response is null,, I want to continue trying for 5 more times reaching out to the payment gateway inside the curl function until the status changes but it aint working

Curl Function

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

Function calling the curl function below

//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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法