php - parse an rss feed and update/insert/delete rows -
i'm trying parse multiple rss feeds , if change, update records in mysql table.
currently, have script inserts items of rss feeds (just post in url in form , submit). inserts following table: title, rss_url, description, price, discount, total
this works well.
the next part script updates rows if change in rss, changes if price or discount update. works great
what i'm looking is: if item in rss feed removed, script needs detect , delete row or insert flag table been deleted...
my code quite long winded:
$result = mysql_query("select * 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 $item) { $article = array(); $article['title'] = $item->title; $article['link'] = $item->link; $article['description'] = (string) trim($item->description); //strip out 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 url encoded £ signs , find string after //string price preg_match_all('/£([0-9.]+)/', $item->description, $results); foreach ($results $k => $v) { } //find url encoded £ sign , append price $all = '£'.$v[0]; $price_stripped = str_replace($all, '', $item->description); $desc = preg_match('/£([0-9.]+)/', $item->description); //find discount deleviry cost rss using ~#£number //this discount preg_match_all('/~#£([0-9.]+)/', $item->description, $discount); foreach ($discount $d => $disc) { str_replace("~#£","", $disc[0]); } //find remaining £price , delivery cost //this delivery_cost preg_match_all('/£([0-9.]+)/', $item->description, $delivery_cost); foreach ($delivery_cost $del => $deliv) { } //find | char , find string after //this retailer_message preg_match_all('/\|(.*?)\./',$item->description,$match); foreach ($match $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' rss_url = '$row[rss_url]' , title = '$item->title' , 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: "; } }
this updates row in database depending if rss item changes.
can provide snippet of how i'd detect if item in rss deleted , php/mysql delete such row table?
thank you
if replacing data new data rss feed won't work you, run through few steps:
- query db. parse array id
- parse rss array id.
- compare arrays. difference id's delete db.
- loop through difference array , delete.
i similar on app wrote. it's long solution, once bugs out works well.
Comments
Post a Comment