【问题标题】:Writing multiple post requests using single connection - PHP使用单个连接编写多个发布请求 - PHP
【发布时间】:2015-10-22 12:38:45
【问题描述】:

我正在使用以下 sn-p 写入服务器。

$fp = connect();
$sent_requests = 0;
function connect() {
    $addr = gethostbyname("example.com");
    $fp = fsockopen("$addr", 80, $errno, $errstr);
    socket_set_blocking( $fp, false );
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
        exit(1);
    } else{
        echo "Connected\n";
        return $fp;
    }
}


function sendTestCalls($load){
    global $fp, $sent_requests;
    if(!$fp){
        echo "reconnecting";
        $sent_requests = 0;
        //echo stream_get_contents($fp) . "\n";
        fclose($fp);
        $fp = connect();
    }
    $data = "POST /test HTTP/2.0\r\n";
    $data.= "Host: example.com\r\n";
    $data.= "Content-Type: application/json\r\n";
    $data.= "Content-Length: ".strlen($load)."\r\n";
    $data.= "Connection: Keep-Alive\r\n";
    $data.= "xYtU87BVFluc6: 1\r\n";
    $data.= "\r\n" . $load;

    $bytesToWrite = strlen($data);
    $totalBytesWritten = 0;

    while ($totalBytesWritten < $bytesToWrite) {
        $bytes = fwrite($fp, substr($data, $totalBytesWritten));
        $totalBytesWritten += $bytes;
    }

    $sent_requests++;
}
$time = time();
for($i=0; $i<1000; $i++) {
    sendTestCalls('{"justtesting": "somevalue"}');
}
fclose($fp);
$time_taken = time() - $time;//might be a bit inaccurate
echo "Time Taken: " . $time_taken . "\n";

当我在我的服务器上检查我的访问日志时,收到的发布请求少于 1000 个(范围为 0 到 900)。我在这里做错了什么?

EDIT1 我想我的套接字正在超时。我应该怎么做才能检查它是否在这种情况下断开连接重新连接。我尝试使用stream_get_meta_data($fp),但没有效果。

【问题讨论】:

  • 你试过使用socket_set_timeout吗?请在此处查看示例php.net/manual/en/function.stream-set-timeout.php 以检查套接字超时
  • 是的,但这也只能在一段时间内有效,直到服务器关闭连接。之后一切都以同样的方式进行。
  • 您是否查看过我提供链接的超时处理示例?

标签: php fsockopen


【解决方案1】:

尝试在每个请求之前插入:

$info = stream_get_meta_data($fp); if ($info['timed_out']) { fclose($fp); $fp = connect(); }

【讨论】:

    【解决方案2】:

    当时我使用php_curlKEEP-ALIVE 找到了解决方案

    这是我的更新版本:

    function sendCall(&$curl_handle, $data){
       curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
       curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
       curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
       curl_setopt(
           $curl_handle,
           CURLOPT_HTTPHEADER,
           array(
               'Content-Type: application/json',
               'Connection: Keep-Alive',
               'Content-Length: ' . strlen($data)
           )
       );
       $response = curl_exec($curl_handle); //Check response?
       if($err = curl_error($curl_handle)) {
           error_log("Error - $err Status - Reconnecting" );
           $curl_handle = curl_init(curl_getinfo($curl_handle, CURLINFO_EFFECTIVE_URL));
           sendCall($curl_handle, $data);
       }
    }
    

    这个函数给了我一个几乎总是活跃的连接。 (超过一周从未收到错误日志)。希望它可以帮助任何寻找相同的人。

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2016-05-20
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多