我正在尝试使用cURL和SimpleXML检索和解析RSS提要。某些项目不包含缩略图$item->featuredImage->img['src']
,因此我想向那些不包含任何缩略图但未按预期工作的项目显示简单消息(无文件)。
$thubmnail
返回文件的完整路径。
但
if (!file_exists($thubmnail)) {
echo '<img src="' . $item->featuredImage->img['src'] . '" width="100" height="100" alt="' . $item->title . '" class="' . $item->featuredImage->img['class'] . '">';
} else {
echo "No File";
}
无法检查文件并显示空图像。
以下是我的整个代码的外观:
<?php
$curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => 'http://domain.com/blog/index.php/feed/',
CURLOPT_USERAGENT => 'spider',
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
$data = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
//die('<pre>' . print_r($xml], TRUE) . '</pre>');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<ul>
<?php foreach ($xml->channel->item as $item) {
$creator = $item->children('dc', TRUE);
$thubmnail = $item->featuredImage->img['src'];
echo '<li class="news-item">';
if (!file_exists($thubmnail)) {
echo '<img src="' . $item->featuredImage->img['src'] . '" width="100" height="100" alt="' . $item->title . '" class="' . $item->featuredImage->img['class'] . '">';
} else {
echo "No File";
}
echo '<h2>' . $item->title . '</h2>';
echo '<p>Created: ' . $item->pubDate . '</p>';
echo '<p>Author: ' . $creator . '</p>';
echo '<p>' . $item->description . '</p>';
echo '<p><a href="' . $item->link . '">Read more</a></p>';
echo '</li>';
}
?>
</ul>