php - 具有 fsockopen 的 PayPal IPN 返回 NULL

标签 php codeigniter paypal fsockopen

我正在尝试在我的 Web 应用程序上设置 PayPal IPN,我从 PayPal 的文档中复制了一个示例 PHP 片段,该片段可在 here 中找到。 .

但是,当我使用 PayPal 的沙箱进行测试时,使用找到的模拟器发送 IPN here .

现在,当 PayPal 发送 IPN 时,我会记录 IPN 的操作和数据,当我尝试使用 fsockopen 打开连接时,它是 NULL 当我这样做时var_export 就可以了。

我不明白为什么当 fsockopen 连接为 NULL 时它不再继续处理代码。

我正在为我的应用程序使用 Codeigniter,这是失败的代码部分:

if($this->uri->segment(3) == 'ipn')
{
    $error_msg = '';

    $error_msg .= " initiated ";

    $req = 'cmd=_notify-validate';

    $error_msg .= " \n\n req: " . var_export($req, true);

    foreach($this->input->post() as $key => $value) 
    {
        $value = urlencode(stripslashes($value));
        $req .= "&" . $key . "=" . $value;
    }

    $error_msg .= " \n\n req: " . var_export($req, true);

    $header = '';
    $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    $error_msg .= " \n\n headers: " . var_export($header, true);

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

    $error_msg .= " \n\n fp: " . var_export($fp, true);

我使用 $error_msg 来记录数据,这是记录内容的示例:

 initiated  

 req: 'cmd=_notify-validate' 

 req: 'cmd=_notify-validate&test_ipn=1&payment_type=echeck&payment_date=17%3A30%3A40+Jan+03%2C+2012+PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123%2C+any+street&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=TESTSELLERID1&residence_country=US&item_name=something&item_number=DX4WYSur44CQICgO2lC%2FB10NmdaiPNH3xPZXQNAlfrEqpse0xnime22zaNXDFgbRrOL4Xsz4emkhqFw4JhOSHzCtaHt9%2B0p9p8xW6R71PVbFXNyEVjkPeHNdQm32PJg&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&txn_type=web_accept&txn_id=4014130&notify_version=2.1&custom=xyz123&invoice=abc1234&charset=windows-1252&verify_sign=An5ns1Kso7MWUdW4ErQKJJJ4qi4-AN8d2a.xggmx9Dn4AgHpvPHJHTAp' 

 headers: 'POST /cgi-bin/webscr HTTP/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 969

' 

 fp: NULL

如您所见,$fp 在记录数据的最后一行返回了 NULL。知道为什么会这样吗?

我可以确认我在我的服务器上启用并安装了 OpenSSL:

enter image description here

编辑:刚刚在 google.com 的端口 80 上测试了 fsockopen,我仍然得到 NULL,没有错误编号或消息。所以这个问题出现在每个 URL 上。

编辑#2:通过这样做在我的服务器上测试:

fsockopen('ssl://www.paypal.com/cgi-bin/webscr', 443, $errno, $errstr, 30)

A PHP Error was encountered

Severity: Warning

Message: fsockopen(): unable to connect to ssl://www.paypal.com/cgi-bin/webscr:443 (php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known)

最佳答案

如果其他人遇到同样的问题,请尝试使用 HTTPS 而不是 SSL

$fp = fsockopen ('https://www.paypal.com', 443, $errno, $errstr, 30);

如果您在 Paypals Sandbox 上的测试使用:

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

关于php - 具有 fsockopen 的 PayPal IPN 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8721368/

相关文章:

php - .htaccess 正在重命名我的 css、img 和 js

php - 复选框 css 仅适用于第一个循环项

php - ExpressCheckout IPN 无效响应状态 403

Paypal 沙盒 - 商户配置无效错误 10501?

php - 谁能告诉我 paypal 按钮之间的区别

php - 如何修复我的 "PHP Warning: A non-numeric value encountered"错误?

javascript - 如何使用 Codeigniter 从数据库填充下拉列表

codeigniter - 为什么使用 "stick"窗口登录时,登录 session "ionic serve"不起作用,但当我将浏览器指向 www 文件夹时,登录 session 0x104567910 可以工作?

mysql - Codeigniter mysql将汉字存储为问号

php - 如何在codeigniter中正确设置flash_message?