PHP 获取 HTTP 400 响应的内容
我正在将 PHP 与 Amazon Payments Web 服务结合使用。我的一些请求遇到了问题。亚马逊按其应有的方式返回了错误,但是它的处理方式给我带来了问题。
Amazon 返回 XML 数据以及有关错误的消息,但它也会抛出 HTTP 400(有时甚至是 404)。这使得 file_get_contents() 立即抛出错误,并且我无法获取内容。我也尝试过使用 cURL,但从未得到它的回复。
我确实需要一种方法来获取 XML 返回,而不管 HTTP 状态代码如何。它有一个重要的“消息”元素,可以为我提供有关计费请求失败原因的线索。
有谁有 cURL 示例或其他可以让我这样做的吗?目前我的所有请求都使用 file_get_contents() 但我不反对更改它们。其他人似乎都认为 cURL 是“正确”的方式。
I am using PHP with the Amazon Payments web service. I'm having problems with some of my requests. Amazon is returning an error as it should, however the way it goes about it is giving me problems.
Amazon returns XML data with a message about the error, but it also throws an HTTP 400 (or even 404 sometimes). This makes file_get_contents() throw an error right away and I have no way to get the content. I've tried using cURL also, but never got it to give me back a response.
I really need a way to get the XML returned regardless of HTTP status code. It has an important "message" element that gives me clues as to why my billing requests are failing.
Does anyone have a cURL example or otherwise that will allow me to do this? All my requests currently use file_get_contents() but I am not opposed to changing them. Everyone else seems to think cURL is the "right" way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用 ignore_errors 选项打开。
You have to define custom stream context (3rd argument of function file_get_contents) with ignore_errors option on.
作为 DoubleThink 帖子的后续内容,这里是一个工作示例:
As a follow-up to DoubleThink's post, here is a working example: