php - 从 RSS 导入时如何避免重复结果

标签 php mysql rss

我每 x 小时使用核心 php 将数据从 RSS 导入到 mysql,但我正在努力处理重复的条目。

$rss_url = 'https://promograd.bg/feed/agg/common.xml?a=143';
    $xml = simplexml_load_file($rss_url);
    foreach($xml->ITEM as $item) {
        $title = mysqli_real_escape_string($link, $item->TITLE);
        $offerUrl = $item->URL;
        $description = mysqli_real_escape_string($link, $item->DESCRIPTION);
        $offerTerms = mysqli_real_escape_string($link, $item->TERMS);
        $originalPrice = $item->ORIGINAL_PRICE;
        $finalPrice = $item->FINAL_PRICE;
        $offerDiscount = $item->DISCOUNT;
        $offerSales = $item->SALES;
        $offerEnds = $item->DEAL_END;
        $lat_coordinates = $item->LAT;
        $lng_coordinates = $item->LNG;
        $city = mysqli_real_escape_string($link, $item->CITY);
        $category = mysqli_real_escape_string($link, $item->CATEGORY);

        $img = $item->IMAGE;

        $query = mysqli_query($link, "
        INSERT INTO......       
        }

我的问题是,当我运行此脚本时,它将导入相同的结果,但没有太多新结果。如何避免重复的结果?

最佳答案

例如,如果您要检查标题是否重复,您可以尝试以下操作:-

$rss_url = 'https://promograd.bg/feed/agg/common.xml?a=143';
$xml = simplexml_load_file($rss_url);
$tempRecords = array(); // temp array store titles
foreach($xml->ITEM as $item) {
    $title = mysqli_real_escape_string($link, $item->TITLE);
    if(in_array($title, $tempRecords)){ //skip if exists
        continue;
    }else{ // else insert
        //$title = mysqli_real_escape_string($link, $item->TITLE);
        $tempRecords[] = $title; //assign to temp array

        $offerUrl = $item->URL;
        $description = mysqli_real_escape_string($link, $item->DESCRIPTION);
        $offerTerms = mysqli_real_escape_string($link, $item->TERMS);
        $originalPrice = $item->ORIGINAL_PRICE;
        $finalPrice = $item->FINAL_PRICE;
        $offerDiscount = $item->DISCOUNT;
        $offerSales = $item->SALES;
        $offerEnds = $item->DEAL_END;
        $lat_coordinates = $item->LAT;
        $lng_coordinates = $item->LNG;
        $city = mysqli_real_escape_string($link, $item->CITY);
        $category = mysqli_real_escape_string($link, $item->CATEGORY);

        $img = $item->IMAGE;

        $query = mysqli_query($link, "
        INSERT INTO......  
    }

    }

您也可以使用mysql查询来完成,请引用链接

https://ypereirareis.github.io/blog/2016/03/22/mysql-insert-ignore-alternatives/

关于php - 从 RSS 导入时如何避免重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55412671/

相关文章:

php - 使用 PHP 和 MySQL 查询的简单交叉表

php - 如果我有 <search input> 而不是 <account/password input>,我是否需要使用 mysql_real_escape_string?

PHP/SQL : Un-md5 a string

javascript - IFTTT Pinterest RSS 源的过滤代码

Javascript 在本地运行良好,但在我的服务器上运行不佳

php - 仅使用 php 和 mysql 或 javascript 单击链接 <a href.. > 时从数据库检索数据(如果需要)

php - 获取数组最高值查询mysql,php

Phpunit骨架生成器找不到扩展类

php - MySQL 插入问题不大

javascript - PHP RSS 提要爬虫