jeudi 28 janvier 2021

Displaying all available images of several RSS Feeds on a php website

first I want to apologize for that extensive description of my issue. As silent reader here I already solved some issues in my current project, which I would describe as follows: RSS feeds (xml files) from several sources I want to display ordered by date descending on a website using php. This already works fine with that first script:

function array_sort_by_column( &$array, $column, $direction = SORT_DESC ) {
    $new_array = json_decode( json_encode( $array ), true );
    if ( 'date_created' === $column ) {
        $mapping_arr = array_map( 'strtotime', array_column( $new_array, $column ) );
        array_multisort( $mapping_arr, $direction );
    } else {
        $reference_array = array();
        foreach ( $array as $key => $row ) {
            $reference_array[ $key ] = $row[ $column ];
        }
        array_multisort( $reference_array, $direction, $array );
    }}

$files= array(
'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml',
'http://feeds.foxnews.com/foxnews/latest',
'https://feeds.npr.org/1002/rss.xml',
'https://feeds.a.dj.com/rss/RSSWorldNews.xml',
'https://www.ft.com/?format=rss',
'https://rssfeeds.usatoday.com/UsatodaycomWorld-TopStories',
'https://www.washingtontimes.com/rss/headlines/news/',
'https://www.huffpost.com/section/world-news/feed',
'https://www.theguardian.com/international/rss',
'https://www.telegraph.co.uk/rss.xml',
'https://rss.politico.com/politicopulse.xml',
'https://lifehacker.com/rss', );

$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->appendChild($dom->createElement('channel'));

foreach ($files as $filename) {
  $addDom = new DOMDocument('1.0', 'utf-8');

  $addDom->load($filename);
  if ($addDom->documentElement) {
    foreach ($addDom->getElementsByTagName("item") as $node) {
      $dom->documentElement->appendChild(
        $dom->importNode($node, TRUE)
      );  }}}

$dom->save('fileout.xml', LIBXML_NOEMPTYTAG); // save as file
$xml=simplexml_load_file("fileout.xml");
$namespaces = $xml->getNamespaces(true);

$items = array();

foreach($xml->item as $item) {
    $items[] = array(
                     'title'             => (string)$item->title,
                     'link'          => (string)$item->link,
                     'description'           => (string)$item->description,
                     'pubDate'         => date('Y-m-d H:i:s',(strtotime((string)$item->pubDate))),
                     'img' => (string)$item->enclosure['url']
                    );
$unique = array_unique($items, SORT_REGULAR);             
}
array_sort_by_column($unique, 'pubDate');

echo '<div class="flex">';
for ($i = 0; $i < count($unique); $i++)
{
$str = ['http://','https://','www.','rss.','rssfeeds.'];
$rplc =['','','','',''];

$quelle = strtoupper(strstr(str_replace($str,$rplc, $unique[$i]['link']), '.', true));

  echo '<div id="artikel">';

if(isset($unique[$i]['img']) && !empty($unique[$i]['img'])) {
  echo '<a target="_blank" href="' . $unique[$i]['link'] . '"><img id="image" src="' . $unique[$i]['img'] . '" alt="' . $unique[$i]['title'] . '" title="' . $unique[$i]['title'] . '"></a>';
}
  echo '#' . $i . ' - <a id="title" target="_blank" title="' . $unique[$i]['title'] .'" href="' . $unique[$i]['link'] . '">' . $unique[$i]['title'] . '</a><br />';
  echo '<span id="quelle" class="' . $quelle . '">'. $quelle . '</span> <span id="pubDate">(' . date_format(new DateTime($unique[$i]['pubDate']), 'd. M Y H:i:s') . ')</span><br />';
  echo strip_tags($unique[$i]['description']) . '</div><br />';
}
echo '</div>';

Because I don't get that way all available images displayed that are included in this RSS-sources I managed to create a second script, which (hopefully) shows all file paths from all images. Some image paths may appear multiple times, but that's okay for me:

$files= array( /* see code snippet above */ );

...
// same code as above until 'foreach ($xml->item as $item){'...}

echo 'title: ' . $item->title . '<br />';
echo 'description: ' . strip_tags($item->description) . '<br />';
echo 'link: ' . $item->link . '<br />';
echo 'pubDate: ' . $item->pubDate . '<br />';

$imgOutput = $item->enclosure['url'];
if(!empty($imgOutput)) {
    echo 'image0: ' . $imgOutput . '<br />';
}

$bild1 = $item->children('http://search.yahoo.com/mrss/')->group->content;
if (is_array($bild1) || is_object($bild1)) {
    foreach($bild1 as $abc) {
          $imgOutput = $abc->attributes()['url'];
        echo 'imageA: ' . $imgOutput ."<br/>";
   }}

$bild2 = $item->children('http://search.yahoo.com/mrss/');
if (isset($bild2->content)) {
$imgOutput = $bild2->content->attributes()['url'];
$imgOutput_dscrptn = $bild2->description;
echo 'imageB: ' . $imgOutput .'<br />';
if(!empty($imgOutput_dscrptn)) {
echo 'imageB dscrptn: ' . $imgOutput_dscrptn . '<br />';
}}

    $bild3 = $item->children($namespaces['media']);
    foreach($bild3 as $xyz){
        $imgOutput = $xyz->attributes()->url;
        if(!empty($imgOutput)) {
        echo 'imageC: ' . $imgOutput . '<br />';
  }}

    $bild4 = $item->children('content', 'http://purl.org/rss/1.0/modules/content/');
    $html_string = $bild4->encoded;
    $dom = new DOMDocument();
if (!empty($bild4->encoded)) {
$dom->loadHTML(html_entity_decode($html_string));
$imgOutput = $dom->getElementsByTagName('img')->item(0);
if ($imgOutput != null) {
echo 'imageD: '.$imgOutput->getAttribute('src').PHP_EOL.'<br />';
}}
echo '<br />';
}

The point where I'm stuck right now is to combine those both scripts. My goal finally is a php script that displays my disered informations all entries fom the collected RSS Feeds ordered by date including all available images.

I already read this forum for hours to find a solution and I'm just an occasional programmer - so, please apologize my (noob) mistakes, my lack of understanding and that extensive description of my issue.

I already tried to find the empty (not set) 'img'-fields in my array to fill them with the matches from my second script and thought about creating a new array with completed information to display them ordered by date ... but both without success sof far.
It would be great if someone understand my issue and could find some time helping me to fix it ... and maybe, but not necessary, additionally to correct or improve my code. Thanks for reading.




Aucun commentaire:

Enregistrer un commentaire