PHP:如何检查响应代码?

发布于 2024-09-01 09:11:40 字数 482 浏览 3 评论 0原文

我是一个相对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦旅人picnic 2024-09-08 09:11:40

它是 get_headers($url),而不是 get_headers($fp)。除非我完全错误地读取它(并且还有一些我从未见过的其他模式),否则您需要向它传递您正在读取的 URL,而不是套接字句柄。实际上,它显然会执行自己的 GET,因此它对于您当前的任务来说毫无用处。

fsockopen(...) 是一个较低级别的 (TCP/IP) 函数。它返回一个套接字句柄,而不是一个 CURL 句柄。这就是为什么你不能在上面使用curl_getinfo(...)。你想要这样的东西......

$fp = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($fp, CURLOPT_POST, true);
curl_setopt($fp, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($fp);

$response_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);

除了我记得的,你需要将“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...

$fp = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($fp, CURLOPT_POST, true);
curl_setopt($fp, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($fp);

$response_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文