i am trying to check through php if xml files exists on a url (incremental names till it fails)
我试图通过PHP检查URL上是否存在xml文件(增量名称直到失败)
why is this code not working?
为什么这段代码不起作用?
<?php
for ($i = 1; $i <= 10; $i++) {
$url = "http://thetvdb.com/api/E676DF9578EF38D7/series/78901/default/".$i."/1/en.xml";
echo $url."<br />";
$xml = simplexml_load_file($url);
if ($xml) {
echo "yay"."<br />";
} else {
echo "fail"."<br />";
die();
}
}
?>
2 个解决方案
#1
1
Your main problem is die()
. This quits all execution.
你的主要问题是死()。这会退出所有执行。
I'd also try using fopen()
instead of simplexml_load_file()
unless you plan on using the XML later on, eg
我还尝试使用fopen()而不是simplexml_load_file(),除非你打算稍后使用XML,例如
$handle = @fopen($url, 'r');
if ($handle === false) {
echo 'fail<br />';
return; // check till it fails
} else {
echo 'yay<br />';
fclose($handle);
}
#2
0
You could just use curl to find out whether a file exists:
你可以使用curl来确定文件是否存在:
function does_remote_file_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) $status = true;
else $status = false;
curl_close($ch);
return $status;
}
#1
1
Your main problem is die()
. This quits all execution.
你的主要问题是死()。这会退出所有执行。
I'd also try using fopen()
instead of simplexml_load_file()
unless you plan on using the XML later on, eg
我还尝试使用fopen()而不是simplexml_load_file(),除非你打算稍后使用XML,例如
$handle = @fopen($url, 'r');
if ($handle === false) {
echo 'fail<br />';
return; // check till it fails
} else {
echo 'yay<br />';
fclose($handle);
}
#2
0
You could just use curl to find out whether a file exists:
你可以使用curl来确定文件是否存在:
function does_remote_file_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) $status = true;
else $status = false;
curl_close($ch);
return $status;
}