Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在返回400错误请求状态的页面上加载DOMDocument_Php_Xml_Api_Domdocument_Last.fm - Fatal编程技术网

Php 在返回400错误请求状态的页面上加载DOMDocument

Php 在返回400错误请求状态的页面上加载DOMDocument,php,xml,api,domdocument,last.fm,Php,Xml,Api,Domdocument,Last.fm,我正在尝试为我正在创建的应用程序使用Last.fm API,但在验证方面遇到了一些问题 如果API请求出现错误,它将在响应XML中返回如下代码和消息: <lfm status="failed"> <error code="6">No user with that name</error> </lfm> 没有使用该名称的用户 但是,请求还返回HTTP状态400(或在某些情况下为403),DOMDocument认为该状态存在错误,因此拒绝解析XM

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

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

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

没有使用该名称的用户
但是,请求还返回HTTP状态400(或在某些情况下为403),DOMDocument认为该状态存在错误,因此拒绝解析XML

有什么方法可以绕过这个问题,这样我就可以检索错误代码和消息了吗

谢谢


Pete

您始终可以使用其他一些函数获取响应,如
文件\u获取内容
,然后使用
DOMDocument::loadXML

编辑:


一个解决方案是将操作分为两个步骤:

  • 首先,获取XML字符串,例如使用
  • 然后,使用
    DOMDocument
    处理该字符串

在手册页面上有一个使用curl的示例;添加一些有用的选项,我想您可以使用如下内容:

$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
此外,有关更多选项(有很多选项),您可以查看功能的手册页。

function getAlbum($xml,$artist,$album)
{
  $base_url = $xml;
  $options = array_merge(array(
    'user' => 'YOUR_USERNAME',
    'artist'=>$artist,
    'album'=>$album,
    'period' => NULL,
    'api_key' => 'xYxOxUxRxxAxPxIxxKxExYxx', 
  ));

  $options['method'] = 'album.getinfo';

  // Initialize cURL request and set parameters
  $ch = curl_init($base_url);
  curl_setopt_array($ch, array(
    CURLOPT_URL            => 'http://ws.audioscrobbler.com/2.0/',
    CURLOPT_POST           => TRUE,
    CURLOPT_POSTFIELDS     => $options,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_TIMEOUT        => 30,
    CURLOPT_HTTPHEADER        => array( 'Expect:' ) ,
    CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
  ));

  $results = curl_exec($ch);
  unset ($options);
  return $results;
}
用法:

// Get the XML
$xml_error = getAlbum($xml,$artist,$album);

// Show XML error
if (preg_match("/error/i", $xml_error)) {
    echo " <strong>ERRO:</strong> ".trim(strip_tags($xml_error));
}
//获取XML
$xml\u error=getAlbum($xml,$artist,$album);
//显示XML错误
if(preg_匹配(“/error/i”,$xml_error)){
echo“错误:”.trim(strip_标记($xml_错误));
}

我使用try&catch解决了这个问题。如果它能帮助某人

    function getXML($xml) {
            $dom = new DomDocument();
        try {
            @$dom->load($xml); // The '@' is necessary to hide error if it's a error 400 - Bad Request
            $root = $dom->documentElement;
            return $root;
        }
        catch(Exception $e)
        {
            return false;
        }
    }

这也是我的第一个想法,尽管遗憾的是它给出了相同的结果:警告:file\u get\u contents(someurl.com)[function.file get contents]:无法打开流:HTTP请求失败!第23行userclass.php中的HTTP/1.0 400错误请求