simplexml_load_file无法打开流E_WARNING:类型2

时间:2016-07-21 00:22:51

标签: php xml

$song = "The Buzzcocks - Orgasm Addict";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
             //echos 'The Buzzcocks'
echo $song;
             //echos 'Orgasm Addict'

$lyricxml1 = simplexml_load_file('http://api.chartlyrics.com/apiv1.asmx/SearchLyric?artist='.$artist.'&song='.$song.'');
$lyricchecksum2 = $lyricxml1->SearchLyricResult[0]->LyricChecksum;
$lyricid = $lyricxml1->SearchLyricResult[0]->LyricId;

echo $lyricchecksum2;
             //echos 'c58a88a5cd5550b2793a408d17193af6'
echo $lyricid;
             //echos '5497'

到目前为止一切都很好。

但是一旦我到了这里

$lyricxml2 = simplexml_load_file('http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId='.$lyricid.'&lyricCheckSum='.$lyricchecksum2.'');
print_r($lyricxml2);

我收到此错误:

E_WARNING : type 2 -- simplexml_load_file(http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId=5497&lyricCheckSum=c58a88a5cd5550b2793a408d17193af6): failed to open stream: HTTP request failed! -- at line 15
E_WARNING : type 2 -- simplexml_load_file(): I/O warning : failed to load external entity "http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId=5497&lyricCheckSum=c58a88a5cd5550b2793a408d17193af6" -- at line 15

即使url / xml文件正确(手动在浏览器中工作) http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId=5497&lyricCheckSum=c58a88a5cd5550b2793a408d17193af6

格式完全相同$ $ lyricxml1(回音输出:http://api.chartlyrics.com/apiv1.asmx/SearchLyric?artist=The Buzzcocks& song = Orgasm Addict)

1 个答案:

答案 0 :(得分:1)

您可以尝试使用file_get_contents()加载文件,然后使用simplexml_load_string()将其转换为对象。

如果要加载外部文件,就像在代码示例中一样,您可以使用curl获取数据,然后使用响应上的simplexml_load_string()将其转换为对象。