返回400 Bad Request状态页面上的DOMDocument负载 [英] DOMDocument load on a page returning 400 Bad Request status

查看:192
本文介绍了返回400 Bad Request状态页面上的DOMDocument负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用我创建应用程序的API Last.fm,但我有一些问题的验证。

I'm trying to use the Last.fm API for an application I'm creating, but am having some problems with validation.

如果API请求给它返回的响应XML这样的code和消息的错误:

If an API request gives an error it returns a code and message in the response XML like this:

<lfm status="failed">
<error code="6">No user with that name</error>
</lfm>

然而,该请求还返回400(或在某些情况下,403),其DOM文档认为一个错误,所以再拒绝解析XML HTTP状态

However, the request also returns an HTTP status of 400 (or in some cases 403) which DOMDocument considers an error and so then refuses to parse the XML.

有没有办法解决这,让我可以检索错误code和信息?

Is there any way round this, so that I can retrieve the error code and message?

感谢

皮特

推荐答案

一个解决办法是你的操作分为两个步骤分开:

A solution could be to separate your manipulations in two steps :


  • 首先,获取XML字符串,使用 卷曲 ,例如

  • 然后,该字符串工作 DOM文档

  • First, get the XML string, using curl, for example
  • Then, work on that string with DOMDocument.

结果
还有就是如何能够在 <$使用卷曲的例子C $ C> curl_exec 手册;增加了一些有用的选项,你可以使用这样的事情,我想:


There is an example of how you can use curl on the curl_exec manual page ; adding a few useful options, you could use something like this, I suppose :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "YUR_URL_HERE");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_string = curl_exec($ch);
curl_close($ch);

// You can now work with $xml_string

和,以获得更多选项的(也有很多他们的^^)的,你可以去看看的 curl_setopt

And, for more options (there are a lot of them ^^ ), you can take a look to the manual page of curl_setopt.

这篇关于返回400 Bad Request状态页面上的DOMDocument负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆