使用PHP解析RSS提要以插入到我的SQL中

时间:2014-05-25 17:36:06

标签: php mysql sql rss

我可以让我的脚本解析项目并在使用Channel->项目时插入mySQL OK。我现在需要插入缩略图的URL,该缩略图位于项目中但在子项目#34;文件夹中#34;由于不知道该怎么称呼它,但是因为它是另一个级别的项目而无法达到它的目标" media:thumbnail" rss Feed是:http://feeds.bbci.co.uk/sport/0/audiovideo/rugby-league-av/rss.xml#

libxml_use_internal_errors(true);
$RSS_DOC = simpleXML_load_file($feed_url);
if (!$RSS_DOC) {
    echo "Failed loading XML\n";
    foreach(libxml_get_errors() as $error) {
        echo "\t", $error->message;
    }
}


/* Get title, link, managing editor, and copyright from the document  */
$rss_title = $RSS_DOC->channel->title;
$rss_link = $RSS_DOC->channel->link;
$rss_description = $RSS_DOC->channel->description;
$rss_copyright = $RSS_DOC->channel->guid;
$rss_date = $RSS_DOC->channel->pubDate;


//Loop through each item in the RSS document

foreach($RSS_DOC->channel->item as $RSSitem)

{

    $item_id    = md5($RSSitem->title);
    $fetch_date = date("Y-m-j G:i:s"); //NOTE: we don't use a DB SQL function so its database independent
    $item_title = $RSSitem->title;
    $item_description = $RSSitem->description;
    $item_date  = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
    $item_url   = $RSSitem->link;
    $image_url   =($RSSitem->media:thumbnails)->url;

    echo "Processing item '" , $item_id , "' on " , $fetch_date     , "<br/>";
    echo $item_title, " - ";
    echo $item_date, "<br/>";
    echo $item_description, "<br/>";
    echo $item_url, "<br/>";
    echo $image_url, "<br/>";
    // Does record already exist? Only insert if new item...

    $item_exists_sql = "SELECT item_id FROM $db_database.`rssingest` where item_id = '" . $item_id . "'";
    $item_exists = mysqli_query($db,$item_exists_sql);
    if(mysqli_num_rows($item_exists)<1)
    {
        echo "<font color=green>Inserting new item..</font><br/>";
        $item_insert_sql = "INSERT INTO $db_database.`rssingest`(item_id, feed_url, item_title, item_date, item_url, fetch_date,image_url) VALUES ('" . $item_id . "', '" . $feed_url . "', '" . $item_title . "', '" . $item_date . "', '" . $item_url . "', '" . $fetch_date . "', '" . $image_url . "')";
        $insert_item = mysqli_query($db,$item_insert_sql);
    }
    else
    {
        echo "<font color=blue>Not inserting existing item..</font><br/>";
    }

    echo "<br/>";
}

0 个答案:

没有答案