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

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

问题描述

我正在尝试将 Last.fm API 用于我正在创建的应用程序,但在验证方面遇到了一些问题.

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

如果 API 请求出现错误,它会在响应 XML 中返回一个代码和消息,如下所示:

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>

但是,该请求还会返回 HTTP 状态 400(或在某些情况下为 403),DOMDocument 将其视为错误,因此拒绝解析 XML.

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.

有没有办法解决这个问题,以便我可以检索错误代码和消息?

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 字符串,例如使用 curl
  • 然后,使用 DOMDocument 处理该字符串.
  • First, get the XML string, using curl, for example
  • Then, work on that string with DOMDocument.


<代码>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.

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

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