Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP cURL http 200,等待并运行新URL_Php_Http_Curl_Wait - Fatal编程技术网

PHP cURL http 200,等待并运行新URL

PHP cURL http 200,等待并运行新URL,php,http,curl,wait,Php,Http,Curl,Wait,必须修改现有代码以使用新的异步API调用。原始版本使用cURL发送请求并使用结果。新方法要求我发送一个初始化请求,等待10-20秒或直到http 200返回,然后发送一个查询cURL并使用结果,但在检查状态为“已完成”之前不这样做 我对cURL还不太熟悉,我很难理解与它相关的许多帖子,欢迎任何帮助。代码是: function check_boomi($service_id) { $status=""; 初始化新的北向API调用 $ch = curl_init(); curl_setopt($c

必须修改现有代码以使用新的异步API调用。原始版本使用cURL发送请求并使用结果。新方法要求我发送一个初始化请求,等待10-20秒或直到http 200返回,然后发送一个查询cURL并使用结果,但在检查状态为“已完成”之前不这样做

我对cURL还不太熟悉,我很难理解与它相关的许多帖子,欢迎任何帮助。代码是:

function check_boomi($service_id) {
$status="";
初始化新的北向API调用

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://testurl.com/init? 
ServiceID=".$service_id."");
在运行“查询”之前,请等待http 200响应

sleep(20);
$result = curl_exec($ch);
运行北行API查询

curl_setopt($ch, CURLOPT_URL, "http://testurl.com/query? 
ServiceID=".$service_id."");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Cache-Control: no-cache";
$headers[] = "Postman-Token: d2d57c3e-7c5d-df1b-4b61-dacb6c44b7cg";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} 
检查结果“状态”:

如果“正在处理”,请等待20秒,然后重试
如果“完成”,则使用返回数据

curl_close ($ch);
$data = json_decode($result);
return $data;}

我省略了一些行,以便更好地了解逻辑,并将代码分为两种方法:

  • initApi-调用初始化api请求并最多等待20秒以获得响应
  • runQuery-调用查询api请求-在可用时返回数据,否则等待20秒,然后重试

    //set max execution time to e.g. 2 minutes
    ini_set('max_execution_time', 120);
    
    initApi($service_id);
    $data = runQuery();
    
    function initApi($service_id){
        $ch = curl_init();
        //set init-url and all relevant curl headers
        curl_setopt($ch, CURLOPT_URL, "http://testurl.com/init?ServiceID=".$service_id."");
        //set request timeout to 20 seconds - when taking longer we check with runQuery
        curl_setopt($ch, CURLOPT_TIMEOUT, 20)
    
        $result = curl_exec($ch);
    }
    
    function runQuery(){
        $ch = curl_init();
        //set query url and all relevant headers
    
        $result = curl_exec($ch);
        if (!curl_errno($ch)) {
            //check the response if it's still "processing" or "completed" - i'm not sure how the api returns that
            $status = 
            if($status == "processing") {
                //wait for 20 seconds and call query again
                sleep(20);
                return runQuery();
            }
            else{
                //return data
            }
        }
    }
    
您还可以减少睡眠超时-然后查询被更频繁地调用,并将检查结果是否可用(我假设查询请求没有阻塞)

不要忘记设置
max\u execution\u time
-默认值可能是30秒-如果脚本运行时间更长,请求将失败