繁体   English   中英

解析RSS供稿并更新/插入/删除行

[英]parse an rss feed and update/insert/delete rows

我正在尝试解析多个RSS提要,如果它们发生变化,请更新MySQL表中的记录。

当前,我有一个脚本,用于插入RSS Feed的项目(只需以表格形式在URL中发布并提交)。 这会将以下内容插入到我的表中:标题,rss_url,描述,价格,折扣,总计

这一切都很好。

下一部分是一个脚本,如果RSS中的行发生更改,该脚本将更新这些行,但是唯一的更改是价格或折扣发生更新。 这也很棒

我还想做的是:如果RSS提要中的一个项目被删除,那么我的脚本需要检测到这一点并删除该行,或者在表中插入一个标记以说它已被删除...

我的代码很长一段时间:

$result = mysql_query("SELECT * from easy_contents");
while($row = mysql_fetch_array($result))
{

$articles = array();
$easy_url = $row['rss_url'];

$rawFeed = file_get_contents($easy_url);
$xml = new SimpleXmlElement($rawFeed);


$channel = array();
$channel['title']       = $xml->channel->title;
$channel['link']        = $xml->channel->link;
$channel['description'] = $xml->channel->description;


foreach ($xml->channel->item as $item)
{
$article = array();
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['description'] = (string) trim($item->description);

//strip out all the HTML tags
$item->description = str_replace('<table><tr><td width="110">','', $item->description);
$item->description = str_replace('</table>','', $item->description);
$item->description = str_replace('</td>','', $item->description);
$item->description = str_replace('<td>','', $item->description);
$item->description = str_replace('<br />','', $item->description);
$item->description = str_replace('<b>','', $item->description);
$item->description = str_replace('</b>','', $item->description);
$item->description = str_replace('</tr>','', $item->description);

//find all url encoded £ signs and find the string after
//string will be a price
preg_match_all('/&#xA3;([0-9.]+)/', $item->description, $results);
foreach ($results as $k => $v) {
}

//find the url encoded £ sign and append the price
$all = '&#xA3;'.$v[0];
$price_stripped = str_replace($all, '', $item->description);
$desc = preg_match('/&#xA3;([0-9.]+)/', $item->description);

//find the discount deleviry cost from the rss using the ~#&pound;NUMBER
//this is the discount
preg_match_all('/~#&pound;([0-9.]+)/', $item->description, $discount);
foreach ($discount as $d => $disc) {
str_replace("~#&pound;","", $disc[0]);
}

//find the remaining £PRICE and this is the delivery cost
//this is the delivery_cost
preg_match_all('/&pound;([0-9.]+)/', $item->description, $delivery_cost);
foreach ($delivery_cost as $del => $deliv) { 
}

 //find the | char and find the string after it
//this is the retailer_message
preg_match_all('/\|(.*?)\./',$item->description,$match);           
foreach ($match as $rel => $retail) { 
$retail[0] = str_replace("| ","", $retail[0]);
$retail_mess = str_replace(" On","On", $retail[0]);

 }   

 $total = $v[0] + $deliv[0] - $disc[0];

 $sql = "UPDATE easy_contents SET delivery_cost = '$deliv[0]', price = '$v[0]', total = '$total' WHERE rss_url = '$row[rss_url]' AND title = '$item->title' AND description = '$price_stripped' ";
 if(!$query = mysql_query($sql)) {
     echo "Error on line ".__LINE__.". ".mysql_error().".<br />\nQuery: ";
     exit;
 }
 echo "Query OK. <br />\nUpdated rows: ".mysql_affected_rows().".<br />\nQuery: ";
   }   
  }

这将根据rss项目是否更改来更新数据库中的行。

谁能提供我如何检测rss中的一项是否被删除以及php / mysql以便从我的表中删除此类行的摘要?

谢谢

如果简单地用RSS提要中的新数据替换数据对您不起作用,则可以执行一些步骤:

  1. 从数据库查询全部。 解析为ID数组
  2. 将RSS解析为ID为Array的数组。
  3. 比较数组。 区别在于从数据库中删除的ID。
  4. 遍历差异数组并删除。

我在编写的应用上做了类似的事情。 这是一个很长的解决方案,但是一旦发现了错误,它就可以很好地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM