例如以下提要:http ://blog.optimizely.com/feed/ 有一个标题元素,如下所示:
<title>
Lessons in Marketing from Daft Punk (Hint: It’s
Not Just About Getting Lucky)
</title>
这个标题最终出现在我的数据库中,如下所示:
愚蠢朋克的营销课程(提示:这不仅仅是为了幸运)
这些表设置为使用 UTF-8
$rss = simplexml_load_file($item['Rss']);
$items=$this->getItems($rss);
foreach($items as $article){
if($this->shouldInsert($article)){
$this->insertItem($article,$item["id"]);
}else{
var_dump("skipping:");
continue 2;
}
}
//the getItems function:
public function getItems($xml){
if(count($xml->channel)!==0){
return $xml->channel->item;
}
if(count($xml->feed)!==0){
return $xml->feed->entry;
}
if(count($xml->entry)!==0){
return $xml->entry;
}
return [];
}
//insertItem:
public function insertItem($item,$sourceID){
$dbh = new PDO('mysql:host=localhost;dbname=db', 'usr', 'pwd');
$title=(string) $item->title;
$url=$this->getLink($item);
$pubDate=$this->getPubDate($item);//pubdate is optional
if($pubDate!==false){
$pubDate=new DateTime($pubDate);
$pubDate=date('Y-m-d H:i:s e',$pubDate->getTimestamp());
}
$now=new DateTime();
$inserted=date('Y-m-d H:i:s e',$now->getTimestamp());
$query="insert into tblItems (title,url,clicks,pubdate,inserted,sourceid)" .
"values ("
.$dbh->quote($title)
.",".$dbh->quote($url)
.",0"
.",".$dbh->quote($pubDate)
.",".$dbh->quote($inserted)
.",".$sourceID.")";
$stm=$dbh->query($query);
$dbh=null;
}