Parsing RSS feed using PHP to insert into my SQL

大城市里の小女人 提交于 2020-01-24 00:29:10

问题


I am able to get my script to parse the items and insert into mySQL OK when using the Channel->items. I now need to insert the url of the thumbnail which is located in items but in a sub-item "folder" for lack of knowing what to call it, but cannot get to it since its a in another level of the item "media:thumbnail" the rss feed is: 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/>";
}

来源:https://stackoverflow.com/questions/23858119/parsing-rss-feed-using-php-to-insert-into-my-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!