dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: trouble with PHP proxy based RSS reader

  1. #1
    Join Date
    Jul 2015
    Posts
    25

    trouble with PHP proxy based RSS reader

    Posted Today, 06:13 PM
    Hello, I am trying to create a simple RSS reader using HTML 5, JavaScript+jQuery+jFeed, and now, PHP. I originally was not using PHP, but then I ran into a cross domain request issue when trying to access an RSS feed (I am using the web comic XKCD as a test) with the following script in the body:

    <!--include jQuery-->
    <script type="text/javascript" src="jquery-3.0.0.js"></script>
    <!--include jFeed-->
    <script type="text/javascript" src="jfeed.js"></script>
    <script type="text/javascript">
    jQuery.getFeed({
    url: 'http://xkcd.com/rss.xml'
    success: function(feed)
    {
    alert('jFeed test');
    alert(feed.title);
    }
    });
    </script>

    which produces the following error:

    XMLHttpRequest cannot load http://xkcd.com/rss.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. (18:01:48:198 | error, javascript)
    at public_html/index.html

    which after research, is supposedly solvable by using a PHP server proxy to forward the RSS page to the client side of my website. So I added the following PHP file to my project (my attempt at a proxy, basically just copied from examples online):

    <?php
    // Set your return content type
    header('Content-type: application/xml');

    // Website url to open
    $url = 'http://xkcd.com/rss.xml';

    // Get that website's content
    $handle = fopen($url, "r");

    // If there is something, read and return
    if ($handle) {
    while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
    }
    fclose($handle);
    }
    ?>

    And then changed the RSS URL in the HTML code to that of my PHP proxy file, so that it now reads:

    url: 'localhost/XKCD_proxy.php',

    However, when I test tthis on my local server using XAMPP, nothing happens--no jQuery dialogues, as included in the success function. I also don't get any errors so I'm pretty confused. Anyone know what I'm doing wrong?

    Thanks in advance

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,976
    Just in case (you never know), is PHP installed and running in your local test environment?

    Assuming it is, you may want to turn on all error reporting:
    PHP Code:
    ini_set('display_errors'true);
    error_reporting(E_ALL); 
    It may be that your PHP config does not allow URL access by fopen(). The work-around -- actually, in my opinion, the better way -- would be to use the cURL functions, which would also give you more control over the HTTP headers you send.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles