PHP 获取 HTTP 400 响应的内容

发布于 2024-09-19 10:08:55 字数 393 浏览 3 评论 0原文

我正在将 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 技术交流群。

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

发布评论

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

评论(2

無心 2024-09-26 10:08:55

您必须使用 ignore_errors 选项打开。

You have to define custom stream context (3rd argument of function file_get_contents) with ignore_errors option on.

许久 2024-09-26 10:08:55

作为 DoubleThink 帖子的后续内容,这里是一个工作示例:

$url = 'http://whatever.com';

//Set stream options
$opts = array(
  'http' => array('ignore_errors' => true)
);

//Create the stream context
$context = stream_context_create($opts);

//Open the file using the defined context
$file = file_get_contents($url, false, $context);

As a follow-up to DoubleThink's post, here is a working example:

$url = 'http://whatever.com';

//Set stream options
$opts = array(
  'http' => array('ignore_errors' => true)
);

//Create the stream context
$context = stream_context_create($opts);

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