PHP DomDocument无法处理网址中的引号

时间:2014-04-17 12:21:44

标签: php domdocument

当我尝试打开这样的网址时:

http://api.anghami.com/rest/v1/GETsearch.view?sid=11754134061397734622103190992&query=Can't Remember to Forget You Shakira&searchtype=SONG&ook&songCount=1

包含浏览器的引用一切正常,输出效果很好,只需要xml

但是当我尝试从php文件中调用它时:

        $url = "http:/api.anghami.com/rest/v1/GETsearch.view?sid=11754134061397734622103190992&query=Can't Remember to Forget You Shakira&searchtype=SONG&ook&songCount=1"

//using DOMDocument for parsing.
$data = new DOMDocument();

// loading the xml from Anghami API.
 if($data->load("$url")){// Getting the Tag song.

 foreach ($data->getElementsByTagName('song') as $searchNode)
       {
       $count++;
       $n++;
        //Getting the information of Anghami Song from the XML file.
        $valueID = $searchNode->getAttribute('id'); 
    $titleAnghami = $searchNode->getAttribute('title');
    $album = $searchNode->getAttribute('album');
    $albumID = $searchNode->getAttribute('albumID');
    $artistAnghami = $searchNode->getAttribute('artist');
    $track = $searchNode->getAttribute('track');
    $year = $searchNode->getAttribute('year');
    $coverArt = $searchNode->getAttribute('coverArt');
    $ArtistArt = $searchNode->getAttribute('ArtistArt');
    $size = $searchNode->getAttribute('size');
    }
 }

我收到此错误:

 'Warning: DOMDocument::load(): I/O warning : failed to load external entity    /var/www/html/http:/api.anghami.com/rest/v1/GETsearch.view?sid=11754134061397734622103190992&query=Can't Remember to Forget You Shakira&searchtype=SONG&ook&songCount=1" in /var/www/html/search.php on line 93'

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

@Fracsi是正确的:网址需要以http://而不是http:/

开头

另一个问题是XML有一个默认命名空间(在根元素上使用xmlns属性定义),所以你需要使用

$data->getElementsByTagNameNS('http://api.anghami.com/rest/v1', 'song')

选择所有“歌曲”元素。