fwrite():SSL操作失败,代码为1. OpenSSL错误消息:\ error:1409F07F:SSL例程:SSL3_WRITE_PENDING:错误的写入重试在PHP中

问题描述:

我在stackoverflow中遇到了同样的问题并尝试了所有答案,但没有任何帮助。我没有得到错误。

I have got same questions in stackoverflow and tried all answers but no help. I am not getting what is the error.

这是我正在使用的函数:

Here is my function that i am using:

function sendRegistryNotification($message, $deviceToken, $deviceType, $batchcount)
        {               
            $message=$message;
            $batchcount=$batchcount;            
            $path=ABSPATH.'api'.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR.'Certificates2.pem';
            $deviceType = strtolower($deviceType);
            switch ($deviceType) {
                case 'ios':
                   $pemPath = $path; // replace with the path to PEM file
                   $ctx = stream_context_create();
                   stream_context_set_option($ctx, 'ssl', 'local_cert', $pemPath);

                   $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',
                       $err,
                       $errstr,
                       60,
                       STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
                       $ctx);
                       // Create the payload body
                       $body['aps'] = array(
                           'badge' => +$batchcount,
                           'alert' => $message,
                           'sound' => 'default'
                       );
                       $payload = json_encode($body);
                       // Build the binary notification
                       $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
                       // Send it to the server
                       $result = fwrite($fp, $msg, strlen($msg));
                       var_dump($result);
                       if (!$result)
                       {
                           fclose($fp);
                           return false;
                       }                                       
                       else
                       {
                           fclose($fp);
                           return true;                  
                       }  
                    break;
                case 'android':
                   $message = array("message" => $message,'title'=> 'eyLog','msgcnt'=>$batchcount);
                   // Set POST variables
                   $url = 'https://android.googleapis.com/gcm/send';

                   $fields = array(
                       'registration_ids' => array($deviceToken),
                       'data' => $message,
                   );
                   $headers = array(
                       'Authorization: key='.'AIzaSyDud68-R31c9SPq4dXZblt0JBPi4TBaneQ', // Key For eylog App
                       'Content-Type: application/json'
                   );
                   // Open connection
                   $ch = curl_init();
                   // Set the url, number of POST vars, POST data
                   curl_setopt($ch, CURLOPT_URL, $url);
                   curl_setopt($ch, CURLOPT_POST, true);
                   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                   // Disabling SSL Certificate support temporarly
                   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                   // Execute post
                   $result = curl_exec($ch);
                   //var_dump($result);
                   if ($result === FALSE) {
                    //   die('Curl failed: ' . curl_error($ch));
                   }
                   // Close connection
                   curl_close($ch);
                   return TRUE;
                    break;
            }
            return true;
        }

这是我得到的错误:

{
    "status": "failure",
    "uniquediary_id": "14407503971",
    "message": "fwrite(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry"
}

我不知道为什么会发生这种情况。并且代码只在我的localhost中的服务器中提供错误它正常工作

I am not getting why it is happening. and the code only gives error in server in my localhost it is working fine

当您尝试写入套接字时。如果出现问题,fwrite将返回false或0(取决于php版本)作为返回值。当它发生时,你必须管理它。

When you are trying to write to the socket. If something goes wrong, fwrite will return false or 0 (depending on the php version) as the return value. When it happens, you must manage it.

对于上述问题,原因很简单:当SSL_Write返回SSL_ERROR_WANT_WRITE或SSL_ERROR_WANT_READ时,你必须重复调用SSL_write在条件满足后(在套接字上可读/写),再次使用相同的参数。

For the above issue the reason is pretty simple: when SSL_Write returns with SSL_ERROR_WANT_WRITE or SSL_ERROR_WANT_READ, you have to repeat the call to SSL_write with the same parameters again, after the condition is satisfied (read/write available on the socket).

使用不同的参数调用它将产生1409F07F错误的写入重试错误。

Calling it with different parameters, will yield the 1409F07F bad write retry error.