php send curl request and wait for the response

后端 未结 2 1931
一向
一向 2021-01-05 12:28

I am sending a curl request to a server that needs a few seconds to process the request and spit out a response. I believe my php script is continuing on and not waiting, t

相关标签:
2条回答
  • 2021-01-05 12:59

    curl is blocking, which means that:

    $result = json_decode($ret,true);
    
    foreach ($result['data'] as $key => $value)
    {
          //process data from $result
    }
    

    won't execute until:

    $ret = curl_exec($curl);
    

    is complete. You can check for errors and other issues using curl_error() and by checking the HTTP response code with curl_getinfo().

    0 讨论(0)
  • 2021-01-05 13:16

    This is really weird ! , it shouldn't happen like what you are getting. But anyways try like this..

    Get a httpresponse code and then check up...

    $curl = curl_init();
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
      curl_setopt($curl, CURLOPT_USERPWD, "admin:password");
      curl_setopt($curl, CURLOPT_URL, "http://server/r/?dst_user__substr='user'");
    
    
      curl_setopt($curl, CURLOPT_VERBOSE, 1);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $ret = curl_exec($curl);
      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      if($httpcode==200)
      {
      $result = json_decode($ret,true);
      foreach ($result['data'] as $key => $value)
      {
            //process data from $result
      }
      }
    
    0 讨论(0)
提交回复
热议问题