RSS and fopen();

Discussion in 'PHP' started by profs77, Jan 3, 2006.

  1. #1
    My PHP hosting does not support fopen(); necessary to read the RSS XML file. Is there any other way to include an RSS feed into your site without using fopen(); ?
     
    profs77, Jan 3, 2006 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    If they support fsockopen(), you can do this:

    "www.serverwithfeed.com" will need to be set to the host and "/feedurl.rss" to the URL.

    $handle = fsockopen('www.serverwithfeed.com', 80, $errno, $errstr, 30);
    fwrite ($handle, "GET /feedurl.rss HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n");
    while (!feof ($handle)) {
    	$string = fgetc ($handle);
    	if ($string == '<') break;
    }
    while (!feof($handle)) {
    	$string .= fread($handle, 40960);
    }
    fclose($handle);
    return $string;
    PHP:
     
    digitalpoint, Jan 3, 2006 IP
  3. profs77

    profs77 Well-Known Member

    Messages:
    441
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    118
    #3
    I'm trying that method you just mentioned, it seems to display a blank page. Below is just an example i'm trying to work with. It shows a blank page when I open the page that handles the PHP code.

    
    <?php
          $handle = fsockopen('www.craigslist.org', 80, $errno, $errstr, 30);
          fwrite ($handle, "GET http://www.craigslist.org/cpg/index.rss HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n");
          while (!feof ($handle)) {
              $string = fgetc ($handle);
              if ($string == '<') break;
          }
          while (!feof($handle)) {
              $string .= fread($handle, 40960);
          }
          fclose($handle);
          return $string; 
    ?>
    
    PHP:
     
    profs77, Jan 3, 2006 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    take out the "http://www.craigslist.org" string out of the fwrite line. Also, instead of return $string on the last line, it should be echo $string; (sorry, my bad).
     
    digitalpoint, Jan 3, 2006 IP
  5. profs77

    profs77 Well-Known Member

    Messages:
    441
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    118
    #5
    The php script seems to only return the raw XML feed. This is the php test page.

    This is the script I'm working with:

    
    <?php
    
          $handle = fsockopen('www.craigslist.org', 80, $errno, $errstr, 30);
    
          fwrite ($handle, "GET /cpg/index.rss HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n");
    
          while (!feof ($handle)) {
    
              $string = fgetc ($handle);
    
              if ($string == '<') break;
    
          }
    
          while (!feof($handle)) {
    
              $string .= fread($handle, 40960);
    
          }
    
          fclose($handle);
    
          echo $string; 
    
    ?>
    
    PHP:
     
    profs77, Jan 4, 2006 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    Right... that's all it's supposed to do. That will get you the feed (what your host is blocking by not allowing use of URLs in fopen(). Parsing the XML or whatever else you are going to need to do on your own (or find something to do it).
     
    digitalpoint, Jan 4, 2006 IP
  7. MaxPowers

    MaxPowers Well-Known Member

    Messages:
    264
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #7
    use cURL if it's allowed... it's WAY faster anyway
     
    MaxPowers, Jan 4, 2006 IP
  8. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #8
    if you are allwoed to use shell_exec()

    simply do shell_exec("wget www.site.com/feed.xml");

    then you would execute $content = shell_exec("cat feed.xml");

    all data would be in $content now.

    then shell_exec("rm -rf feed.xml");
     
    legend2, Jan 6, 2006 IP