Cross-Domain Rss Feed Request?

Ok, so for about a week now I’ve been doing tons of research on making xmlhttprequests to servers and have learned a lot about CORS, ajax/jquery request, google feed api, and I am still completely lost.

The Goal:
There are 2 sites in the picture, both I have access to, the first one is a wordpress site which has the rss feed and the other is my localhost site running off of xampp (soon to be a published site when I’m done). I am trying to get the rss feed from the wordpress site and display it on my localhost site.

Read More

The Issue:
I run into the infamous Access-Control-Allow-Origin error in the console and I know that I can fix that by setting it in the .htaccess file of the website but there are online aggregators that are able to just read and display it when I give them the link. So I don’t really know what those sites are doing that I’m not, and what is the best way to achieve this without posing any easy security threats to both sites.

I highly prefer not to have to use any third party plugins to do this, I would like to aggregate the feed through my own code as I have done for an rss feed on the localhost site, but if I have to I will.

UPDATE:

I’ve made HUGE progress with learning php and have finally got a working bit of code that will allow me to download the feed files from their various sources, as well as being able to store them in cache files on the server. What I have done is set an AJAX request behind some buttons on my site which switches between the rss feeds. The AJAX request POSTs a JSON encoded array containing some data to my php file, which then downloads the requested feed via cURL (http_get_contents copied from a Github dev as I don’t know how to use cURL yet) link and stores it in a md5 encoded cache file, then it filters what I need from the data and sends it back to the front end. However, I have two more questions… (Its funny how that works, getting one answer and ending up with two more questions).

Question #1: Where should I store both the cache files and the php files on the server? I heard that you are supposed to store them below the root but I am not sure how to access them that way.

Question #2: When I look at the source of the site through the browser as I click the buttons which send an ajax request to the php file, the php file is visibly downloaded to the list of source files but also it downloads more and more copies of the php file as you click the buttons, is there a way to prevent this? I may have to implement another method to get this working.

Here is my working php:

//cURL http_get_contents declaration
<?php
function http_get_contents($url, $opts = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_USERAGENT, "{$_SERVER['SERVER_NAME']}");
    curl_setopt($ch, CURLOPT_URL, $url);
    if (is_array($opts) && $opts) {
        foreach ($opts as $key => $val) {
            curl_setopt($ch, $key, $val);
        }
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if (false === ($retval = curl_exec($ch))) {
        die(curl_error($ch));
    } else {
        return $retval;
    }
}

//receive and decode $_POSTed array
$post = json_decode($_POST['jsonString'], true);
$url       = $post[0];
$xmn       = $post[1]; //starting item index number (i.e. to return 3 items from the feed, starting with the 5th one)
$xmx       = $xmn + 3; //max number (so three in total to be returned)
$cache     = '/tmp/' . md5($url) . '.html';
$cacheint  = 0;  //this is how I set if the feed will be downloaded from the site it is from, or if it will be read from the cache file, I will implement a way to check if there is a newer version of the file on the other site in the future

//if the cache file doesn't exist, download feed and write contents to cache file
  if(!file_exists($cache) || ((time() - filemtime($cache)) > 3600 * $cacheint)) {
      $feed_content = http_get_contents($url);
      if($feed_content = http_get_contents($url)) {
          $fp = fopen($cache, 'w');
          fwrite($fp, $feed_content);
          fclose($fp);
      }
  }

//parse and echo results
$content = file_get_contents($cache);
$x = new SimpleXmlElement($content);
$item = $x->channel->item;
echo '<tr>';
for($i = $xmn; $i < $xmx; $i++) {
    echo '<td class="item"><p class="title clear">' . 
        $item[$i]->title . 
        '</p><p class="desc">' . 
        $desc=substr($item[$i]->description, 0, 250) . 
        '... <a href="' . 
        $item[$i]->link . 
        '" target="_blank">more</a></p><p class="date">' . 
        $item[$i]->pubDate . 
        '</p></td>';
}
echo '</tr>';
?>

Related posts