douzhang8144 2017-02-12 16:28
浏览 759
已采纳

在不等待响应的情况下执行HTTP GET请求[关闭]

I have a PHP script where users send OTP as SMS. The SMS gateway I use takes around 5-8 seconds to respond. I cannot wait that long. I need to make the request and immediately send an HTML response to the user.

I used curl and it takes too long, and a short timeout drops the connection and makes the gateway unpredictable. I need a way to make the request, execute some routines and preferably have the option to verify the request.

  • 写回答

1条回答 默认 最新

  • dongqie2028 2017-02-12 20:51
    关注

    You use stream_socket_client(). Do your GET request then get the results later.

    Make Request with stream_socket_client()

        $host = 'www.example.com';
        $path = '/';
        $http = "GET $path HTTP/1.0
    Host: $host
    
    ";
        $stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); 
        if ($stream) {
          $sockets[] = $stream;  // supports multiple sockets
          fwrite($stream, $http);
        }
        else { 
          $err .=  "$id Failed<br>
    ";
        }  
    

    Get Responses with stream_select()

    $timeout = 120;
    $buffer_size = 8192;
    while (count($sockets)) {
      $read = $sockets; 
      stream_select($read, $write = NULL, $except = NULL, $timeout);
      if (count($read)) {
        foreach ($read as $r) { 
          $id = array_search($r, $sockets); 
          $data = fread($r, $buffer_size); 
          if (strlen($data) == 0) {  // done
            fclose($r); 
            unset($sockets[$id]);
          } 
          else {
            $result[$id] .= $data; // append buffer to result
          }
        }
      }
      else { 
     //   echo 'Timeout: ' . date('h:i:s') . "
    
    
    ";
        break;
      }
    }
    

    UPDATE

    You can make a request at anytime, and get the response anytime after the request. When a socket is created the $sockets array's key is the $id.

    You do not have to have the while loop if you want to use some other control method. The buffer in the example is 8K. If the response is more than 8K it will take multiple reads.

    If you do not want to retrieve the response then just close the socket and do not use the $sockets array. You may or may not need a delay before the fclose(). It depends upon how the host responds to a dropped connection.

    $host = 'www.example.com';
    $path = '/?param=value';
    $http = "GET $path HTTP/1.0
    Host: $host
    
    ";
    $stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); 
    
    if ($stream) {
      fwrite($stream, $http);
      fclose($stream); 
    
    }
    else { 
      $err .=  "$id Failed<br>
    ";
    }     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?