duanke1286 2018-05-21 10:38
浏览 194
已采纳

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

Having to modify existing code to consume a new asynchronous API call. The original uses cURL to send a request and consume the results. The new method requires me to send an initialisation request, wait 10-20 seconds or until http 200 returns and then send a query cURL and consume the results, but not before checking the status is 'Completed'.

I'm new to looking at cURL, and struggling to get my head around the many post relating to it, any help most welcome. The code is :

function check_boomi($service_id) {
$status="";

Initialize new Northbound API call

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

Wait for http 200 response before running the 'query'

sleep(20);
$result = curl_exec($ch);

Run Northbound API query

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

Check result 'status':

If 'Processing' wait 20 seconds and try again
If 'Completed' use the return data

curl_close ($ch);
$data = json_decode($result);
return $data;}
  • 写回答

1条回答 默认 最新

  • dryb38654 2018-05-21 14:26
    关注

    I omitted some lines so you have a better overview of the logic and seperated the code into 2 methods:

    • initApi - calls initialization api request and waits max 20 seconds for response
    • runQuery - calls query api request - returns data when available otherwise waits for e.g. 20 seconds and try it again

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

    You could also descrease the sleep timeout - then the query is called more often and will check if the result is available (i assume the query request is not blocking)

    And dont't forget to set the max_execution_time - default is probably 30 seconds - if your script runs longer the request will fail.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题