Quantcast
Channel: Active questions tagged feed - Stack Overflow
Viewing all articles
Browse latest Browse all 547

Extract image source from wordpress rss feed [closed]

$
0
0

Hi dear and professional friends. I'm in trouble! This is my stupid code that must extract some data from wordpress rss feed such as titles, links, pubdate and also image sources but it can't!Finally, it saves the information in a json file.Do you have any idea to resolve it?

<?php// Loop through the items in the RSS feedforeach ($xml->channel->item as $item) {    // Get the title, link and pubDate from the item    $title = (string) $item->title;    $link = (string) $item->link;    $pubDate = (string) $item->pubDate;    // Use SimplePie to get all the IMG-tags in the item    $img_tags = $item->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'IMG');    // Loop through the IMG-tags and get their attributes    foreach ($img_tags as $img_tag) {        // Get the src attribute of the IMG-tag        $src = (string) $img_tag['attribs']['']['src'];        // Get the srcset attribute of the IMG-tag        $srcset = (string) $img_tag['attribs']['']['srcset'];        // Use a regular expression to match all the width and height values in the srcset attribute        preg_match_all('/(\d+)w,\s*(\d+)h/', $srcset, $matches);        // Convert the width and height values to integers        $widths = array_map('intval', $matches[1]);        $heights = array_map('intval', $matches[2]);        // Find the highest image size by multiplying the width and height values        $sizes = array_map(function($w, $h) { return $w * $h; }, $widths, $heights);        // Get the index of the highest image size        $index = array_search(max($sizes), $sizes);        // Get the corresponding image URL from the srcset attribute        $image = explode(', ', $srcset)[$index];        // Format the pubDate as a valid RSS date        $pubDate = date(DATE_RSS, strtotime($pubDate));        // Convert the pubDate to Persian time ago using the function        $pubDate = persian_time_ago($pubDate);        // Create an associative array with the title, link, image and pubDate        $article = array("title" => $title,"link" => $link,"image" => $image,"pubDate" => $pubDate        );        // Append the article array to the data array        array_push($data, $article);    }}$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);echo $json;$file = "JVgP4cGa.json";file_put_contents($file, $json);?>

Any solution to extract image source url from wordpress feed


Viewing all articles
Browse latest Browse all 547

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>