-1

I want to convert this JSON Feed to RSS in php, and get the following information

title, date, and images

I have get the data like this, it gives me an array

<?php
function convert_jsonfeed_to_rss($content = NULL, $max = NULL){

 $jsonFeed = json_decode($content, TRUE);
 echo "<pre>";
 print_r($jsonFeed);
 echo "</pre>";

}
$content = @file_get_contents("shorturl.at/gDHY9");
convert_jsonfeed_to_rss($content);

Kindly educate me in the right direction to get the rss feed from the given json, I don't know how to do that Thanks

mehmood khan
  • 497
  • 1
  • 7
  • 20
  • There are a ton of examples on the internet. An RSS feed is just a XML file. Have a look at https://www.w3schools.in/php/php-rss-feed – Uwe Dec 25 '22 at 20:13
  • Alternatively search for questions referring to your question. Like [PHP convert Array to XML](https://stackoverflow.com/questions/37618094/php-convert-array-to-xml) – Uwe Dec 25 '22 at 20:16
  • How do i download the resulted xml in a file? – mehmood khan Dec 26 '22 at 07:15
  • That is mentioned in at least two answers to the linked stackoverflow question. In the code are even comments which describe what is happening. Have a look. Only important thing is, that you set the values for the root element when creating the dom document. – Uwe Dec 26 '22 at 08:07
  • It only gives me the xml in the browser, it's not saving the xml in the file – mehmood khan Dec 26 '22 at 08:12
  • I'm using this one https://stackoverflow.com/a/37629590/7014609 – mehmood khan Dec 26 '22 at 08:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250651/discussion-between-mehmood-khan-and-uwe). – mehmood khan Dec 26 '22 at 08:17
  • It gives you a 1 because that is the return value for that operation. It wrote to a file and assigned the return value to $result. Check the path you want to write your xml. The file should exist now. --- Please edit your question and add your current code. – Uwe Dec 26 '22 at 12:33

1 Answers1

0

Here is an example of a PHP function that you can use to convert a JSON feed to an RSS feed, This function takes a URL of a JSON feed as input and converts it to an RSS feed:

function convert_json_to_rss( $json_feed_url ) {
    // Parse the JSON feed
    $json_feed = file_get_contents( $json_feed_url );
    $json      = json_decode( $json_feed );

    // Create the RSS feed object
    $rss = new SimpleXMLElement( '<rss version="2.0"></rss>' );

    // Add the channel element to the RSS feed
    $channel = $rss->addChild( 'channel' );

    // Add the channel data to the RSS feed
    $channel->addChild( 'title', $json->title );
    $channel->addChild( 'description', $json->description );
    $channel->addChild( 'link', $json->link );

    // Add the items to the RSS feed
    foreach ( $json->items as $item ) {
        $rss_item = $channel->addChild( 'item' );
        $rss_item->addChild( 'title', $item->title );
        $rss_item->addChild( 'description', $item->description );
        $rss_item->addChild( 'link', $item->link );
    }

    // Output the RSS feed as a string
    header( 'Content-Type: application/rss+xml' );
    echo $rss->asXML();
}
Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16
  • Hi Yassine Zouaoui thanks for taking the time to answer this question. It is kind of a duplicate and @mehmoodkhan has already a working code to get XML and needs the part to save his xml as a file, which your code would not do. Please read the comments before you answer a question. – Uwe Dec 26 '22 at 17:38