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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)