PHP获取HTTP 400响应的内容

joc*_*ull 10 php curl http-status-codes file-get-contents

我正在使用PHP与Amazon Payments Web服务.我的一些要求有问题.亚马逊正在返回一个错误,但它的方式是给我带来问题.

Amazon返回XML数据并显示有关错误的消息,但它也会抛出HTTP 400(有时甚至是404).这使得file_get_contents()立即抛出错误,我无法获取内容.我也尝试过使用cURL,但从来没有让它给我回复.

无论HTTP状态代码如何,我真的需要一种方法来返回XML.它有一个重要的"消息"元素,可以为我提供有关我的结算请求失败的原因的线索.

有没有人有cURL示例或其他允许我这样做?我的所有请求目前都使用file_get_contents(),但我不反对更改它们.其他人似乎认为cURL是"正确"的方式.

小智 18

您必须使用ignore_errors选项定义自定义流上下文(函数file_get_contents的第三个参数).


joc*_*ull 18

作为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);
Run Code Online (Sandbox Code Playgroud)