fgets():SSL:远程主机强行关闭了现有连接 [英] fgets(): SSL: An existing connection was forcibly closed by the remote host

查看:229
本文介绍了fgets():SSL:远程主机强行关闭了现有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PayPals示例IPN代码进行测试,该代码应返回validinvalid进行交易.我正在使用PayPals IPN模拟器进行测试,该模拟器应发送一些虚拟数据,然后对其进行验证(返回有效").

I am testing with PayPals example IPN code which should return valid, or invalid for a transaction. I am testing with PayPals IPN simulator which should send some dummy data, and then validate it (returning "Valid").

我正在使用两个单独的Web服务器进行测试,两个Web服务器均已安装并启用了OpenSSL.

I am testing with two separate web servers, both have OpenSSL installed and enabled.

在我们的本地Web服务器上,我们收到此错误消息.

On our local web server, we get this error message.

fgets(): SSL: An existing connection was forcibly closed by the remote host.

在我们的客户端Web服务器上,使用相同的代码,我们得到以下信息:

On our clients web server, with the same code, we get this:

fgets() [<a href='function.fgets'>function.fgets</a>]: SSL: Connection reset by peer in ...../paypal_ipn.php on line 43

PayPal似乎不再具有此版本的非SSL版本.

PayPal doesn't seem to have a non-SSL version of this anymore.

paypal_ipn.php :

    <?php

    ini_set("log_errors", 1);
    ini_set("error_log", "error.log");

       // Send an empty HTTP 200 OK response to acknowledge receipt of the notification 
       header('HTTP/1.1 200 OK'); 

       // Assign payment notification values to local variables
       //$item_name        = $_POST['item_name'];
       //$item_number      = $_POST['item_number'];
       $payment_status   = $_POST['payment_status'];
       $payment_amount   = $_POST['mc_gross'];
       $payment_currency = $_POST['mc_currency'];
       $txn_id           = $_POST['txn_id'];
       $receiver_email   = $_POST['receiver_email'];
       $payer_email      = $_POST['payer_email'];

         // Build the required acknowledgement message out of the notification just received
      $req = 'cmd=_notify-validate';               // Add 'cmd=_notify-validate' to beginning of the acknowledgement

    $req .= '&'.http_build_query($_POST);


      // Set up the acknowledgement request headers
      $header  = "POST /cgi-bin/webscr HTTP/1.1\r\n";                    // HTTP POST request
      $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

      // Open a socket for the acknowledgement request
  //$fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
  //$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); 
  $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);   
    if ($fp === FALSE) {
        error_log("Could not open socket");
        exit("Could not open socket");
    }

      // Send the HTTP POST request back to PayPal for validation
      fputs($fp, $header . $req);

      while (!feof($fp)) {                     // While not EOF
        $res = fgets($fp, 1024);               // Get the acknowledgement response
        if (strcmp ($res, "VERIFIED") == 0) {  // Response contains VERIFIED - process notification

          // Send an email announcing the IPN message is VERIFIED
          $mail_From    = "IPN@example.com";
          $mail_To      = "Your-eMail-Address";
          $mail_Subject = "VERIFIED IPN";
          $mail_Body    = $req;
          file_put_contents("log.txt", "valid: " . $req, FILE_APPEND | LOCK_EX);


          // Authentication protocol is complete - OK to process notification contents

          // Possible processing steps for a payment include the following:

          // Check that the payment_status is Completed
          // Check that txn_id has not been previously processed
          // Check that receiver_email is your Primary PayPal email
          // Check that payment_amount/payment_currency are correct
          // Process payment

        } 
        else if (strcmp ($res, "INVALID") == 0) { //Response contains INVALID - reject notification

          // Authentication protocol is complete - begin error handling

          // Send an email announcing the IPN message is INVALID
          $mail_From    = "IPN@example.com";
          $mail_To      = "Your-eMail-Address";
          $mail_Subject = "INVALID IPN";
          $mail_Body    = $req;
            file_put_contents("log.txt", "invalid: " . $req, FILE_APPEND | LOCK_EX);

        }
      }

      fclose($fp);  // Close the file
    ?>

我将不会使用CURL,因为这还有很多其他问题!谁能看到是什么原因导致这两个(单独的)错误?

I am not going to be using CURL, as that is whole other lot of problems! Can anyone see what could be causing these two (separate) errors?

我刚刚在另一台运行XAMPP的服务器上进行了测试(几乎启用了所有功能),现在出现了这个错误":

I've just tested on another server running XAMPP (nearly everything enabled), and I now get this 'error':

PHP Warning: fgets(): SSL: The operation completed successfully.

但是,交易完全没有得到验证.

Yet, the transaction doesn't get validated at all.

推荐答案

经过一天的苦苦挣扎,我回到家,决定今天早上解决这个问题.

Right well after a day of struggling with this, I went home, and decided to tackle it this morning.

使用fget/fputs似乎存在问题.我可以使用浏览器中的发布数据浏览到验证URL,并且可以看到我使用的URL工作正常.

It looked like there was an issue with using fget / fputs. I could browse to the verification URL using the post data in my browser and could see that the URL I was using was working fine.

由于其他一些问题,我没有使用CURL,而且没有足够的时间来解决它们.

I couldn't use CURL due to some other issues and not enough time to solve them.

*解决方案 *:

改为使用file_get_contents().这使事情变得更容易,并且不需要发送标头或其他任何内容.这完美无瑕!

Use file_get_contents() instead. This made things easier, and no need to send headers or anything else. This works flawlessly!

  $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?' . $req;

  $res = file_get_contents($url);

这篇关于fgets():SSL:远程主机强行关闭了现有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆