Paypal IPN脚本,feof和fgets的问题

时间:2011-11-29 16:17:05

标签: php paypal paypal-ipn fgets feof

我的Paypal IPN监听器脚本问题已经有几天了。对于那些不熟悉Paypal IPN系统的人来说,Paypal基本上会向您的脚本发送一条有关交易的消息,您可以通过添加几个位来发送回来。如果Paypal收到正确的回复,它将回复'已验证',如果没有,它会说'无效'。

我最初认为我遇到的问题是'fsockopen'命令:     $ fp = fsockopen('ssl://sandbox.paypal.com',443,$ errno,$ errstr,30); 但是,将我的所有代码简化为这一行,它似乎连接好了。问题在于'feof'和'fgets'命令。脚本挂断了,我不知道为什么。我基本上复制了Paypal IPN Listener网站上建议的代码,所以我认为它会起作用!如果你能帮助我理解为什么feof或fgets导致它失速,那么你的帮助将会非常受欢迎。

这是完整的脚本:

$postback = 'cmd=_notify-validate'; //doesn't matter what these include for now
$header='abc';

//Script has been activated, create debug
$filename = 'debug/debug1_script.txt';
$filehandle=fopen($filename, 'w');
fwrite($filehandle,$postback);
fclose($filehandle);


$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//open the connection

//no connection, create debug file
if(!$fp){
    $filename = 'debug/debug2_fpfail.txt';
    $filehandle=fopen($filename, 'w');
    fwrite($filehandle, $errstr.'('.$errno.')');
    fclose($filehandle);
    die();
}


//post data back
fputs($fp, $header . $postback);

//create debug file
$filename = 'debug/debug3_postback.txt';
$filehandle=fopen($filename, 'w');
fwrite($filehandle, $header.$postback);
fclose($filehandle);


//script hangs with either of the two following lines included
while(!feof($fp)){
    $res=fgets($fp,1024);
}

非常感谢提前!

4 个答案:

答案 0 :(得分:6)

所以我认为我找到了一个解决方案,而不是使用

while(!feof())

fgets()

组合,我用过这个:

$res=stream_get_contents($fp, 1024);

第一次工作!现在我可以继续我的生活。

答案 1 :(得分:1)

对于通过Google抵达此处的任何人,请不要忘记在标题中加入“关闭:关闭”!否则主机将保持连接打开,直到它超时!

答案 2 :(得分:1)

如果其他人有同样的问题,CURL似乎是PayPal推荐的IPN选择。

在Github上查看代码示例:https://github.com/paypal/ipn-code-samples/blob/master/paypal_ipn.php

答案 3 :(得分:0)

$fp套接字返回到paypal的连接必须是http POST。 feof()挂起,因为paypal从未听到完整的HTTP请求,因此它永远不会发回任何内容 - 只是一直在倾听,直到你放弃为止。

this paypal IPN page上的示例代码中需要额外的内容(已完成的$header$req变量)。

如果可以的话,我会重写这个以使用CURL,而不是原始套接字,因此您不必格式化完整的http请求并读取原始的http响应。