IPN - 教程有错误

时间:2014-05-24 23:18:34

标签: php paypal

我试图按照paypal的开发者网站上的教程设置一个基本的IPN监听器,它看起来几乎像他们的例子一样的语法语法。事实上,当我开始收到我一直收到的错误时,我想我会创建一个新的ipn监听器,只使用他们在他们的示例中的代码来查看它是否是我的代码并收到相同的错误。

Warning: fgets(): supplied argument is not a valid stream resource in \supergate\ipn.php on line 42

Warning: feof(): supplied argument is not a valid stream resource in \supergate\ipn.php on line 39

这些行号的错误是针对这些行的代码:

 while (!feof($fp)) //line 39
{

 $res = fgets($fp, 1024); //line 42

这是整个代码的其余部分

<?php
//Empty Header HTTP 200 OK reponse to ack 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_ammount   =$_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 ack message of notification just received
$req = 'cmd=_notify-validate'; //add 'cmd=_notify-validate' to beginning of acknowledgement

foreach ($_POST as $key => $value) {  //loop through the notification nv pairs
  $value = urlencode(stripcslashes($value)); //encode these values
 $req .= "&$key=$value"; //add the nv pairs to the acknowledgement
}

//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('ssl://sandbox.paypal.com', 443, $errno, $errstr, 30);

//send the http post request back to paypal for validation
fputs($fp, $header . $req);

while (!feof($fp)) 
{

    $res = fgets($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) 
    {
     //WRITE TO EMAIL


    */
 }
//--------------------------------------------------------------------------------------------------------------------
 else if (strcmp($res,"INVALID") == 0) 
 {

   //write to email
}


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

好的,现在我得到的新警告就是:

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

1 个答案:

答案 0 :(得分:1)

您没有检查fsockopen()在失败时返回FALSE的结果。

$fp = fsockopen(...);
if ($fp === FALSE) {
    exit("Could not open socket");
}

文档中的一个线索是第一个参数:

  

<强>主机名   如果安装了OpenSSL支持,则可以在主机名前加上ssl://或tls://,以使用通过TCP / IP的SSL或TLS客户端连接来连接到远程主机。

请注意,您的示例正在使用ssl:// - 您的服务器可能未正确配置OpenSSL。