热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP获取HTTP400响应的内容-PHPGetContentofHTTP400Response

IamusingPHPwiththeAmazonPaymentswebservice.Imhavingproblemswithsomeofmyrequests.A

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.

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

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.

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

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.

无论HTTP状态代码如何,我都需要一种方法来返回XML。它有一个重要的“消息”元素,为我提供了解决我的结算请求失败的原因的线索。

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.

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

2 个解决方案

#1


17  

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

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

#2


16  

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

作为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);

推荐阅读
author-avatar
蔚蓝的希望_674
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有