PHP:如何检查响应代码?
我是一个相对 PHP 新手,实现了 PayPal IPN 监听器,一切似乎都工作正常,除了我真的不知道如何检查响应代码。
我用 cURL 尝试过一些丑陋的东西,但它根本不起作用(我不理解 cURL)。
我已经尝试过从网上某处获取的这段代码:
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$response_headers = get_headers($fp);
$response_code = (int)substr($response_headers[0], 9, 3);
...但它不起作用(返回 $response_code = 0)。
所以现在,我正在调试我的 IPN 代码,而不检查 Response 200。
任何更有经验的人都可以告诉我检查此问题的正确/简单方法是什么吗?
谢谢
I'm a relative PHP newbie implementing a PayPal IPN listener and all seems to be working fine, except I dont really know how to check for a response code.
I've tried something ugly with cURL but it doesn't work at all (I'm not understanding cURL).
I've tried this piece of code that I grabbed from somewhere on the net:
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$response_headers = get_headers($fp);
$response_code = (int)substr($response_headers[0], 9, 3);
... but it's not working (returns $response_code = 0).
So right now, I'm debugging my IPN code without checking for a Response 200.
Can anyone more experienced advise me on what's the proper/simple way to check this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是 get_headers($url),而不是 get_headers($fp)。除非我完全错误地读取它(并且还有一些我从未见过的其他模式),否则您需要向它传递您正在读取的 URL,而不是套接字句柄。实际上,它显然会执行自己的 GET,因此它对于您当前的任务来说毫无用处。
fsockopen(...) 是一个较低级别的 (TCP/IP) 函数。它返回一个套接字句柄,而不是一个 CURL 句柄。这就是为什么你不能在上面使用curl_getinfo(...)。你想要这样的东西......
除了我记得的,你需要将“cmd=_notify-validate”添加到帖子字段中。
不要使用 fsockopen(...)。是的,我知道,这就是 Paypal 示例代码的作用。但它意味着可以在任何地方运行,并且不能依赖于安装的 CURL。你可以,所以使用它。
It's get_headers($url), not get_headers($fp). Unless i'm reading it totally wrong (and there's some other mode i've never seen), you need to pass it the URL you're reading from, not a socket handle. Actually, it apparently does its own GET, so it'd be useless for your current task.
fsockopen(...) is a lower level (TCP/IP) function. It returns a socket handle, not a CURL handle. Which is why you can't use curl_getinfo(...) on it. You want something like this...
except as i remember, you need to add 'cmd=_notify-validate' to the post fields.
Don't use fsockopen(...). Yeah, i know, that's what the Paypal sample code does. But it's meant to run everywhere, and can't rely on CURL being installed. You can, so use it.