Datahopa

General Category => General Discussion => Topic started by: sybershot on April 22, 2012, 01:44:13 AM

Title: php rss
Post by: sybershot on April 22, 2012, 01:44:13 AM
Anyone have know of any good php script tutorial for a RSS feed.
I was able to get a java script one working for my site, but would like to use a php script for the seo benefits.
Title: Re: php rss
Post by: Freddy on April 22, 2012, 14:57:17 PM
Not off the top of my head and I tried my bookmarks but could not find much.

I built my RSS feeds myself in PHP, pulling data from the database.  I think it was fairly easy.

I suggest first googling for RSS + PHP it brought this up for me :

http://www.ibm.com/developerworks/library/x-phprss/ (http://www.ibm.com/developerworks/library/x-phprss/)

Which looks to me the kind of thing you want.

Once you know the standard and the structure it's pretty easy to work out where everything goes.
Title: Re: php rss
Post by: sybershot on April 22, 2012, 18:05:11 PM
Thanks Freddy, I will give that a read thru in a bit  :thumbsup:
Title: Re: php rss
Post by: sybershot on April 23, 2012, 00:06:50 AM
Okay I got most of what I need to work, except one line of php.

$xml = simplexml_load_file('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$channel);

Anyone good with php, that might be able to help me?

you can view the code here within the page source http://www.scriptedintelligence.com/test/testor.html (http://www.scriptedintelligence.com/test/testor.html)
and you can view the results here http://www.scriptedintelligence.com/test/testor.php (http://www.scriptedintelligence.com/test/testor.php)

Title: Re: php rss
Post by: sybershot on April 23, 2012, 00:52:56 AM
I tried tutorial which is smaller in code but I still get errors
here is the short tutorial php version

<?php
$username 
"ScriptedIntell";
$feed "http://search.twitter.com/search.atom?q=from:" $username "&rpp=1";

function 
parse_feed($feed) {
    
$stepOne explode("<content type=\"html\">"$feed);
    
$stepTwo explode("</content>"$stepOne[1]);
    
$tweet $stepTwo[0];
$tweet htmlspecialchars_decode($tweet,ENT_QUOTES);
    return 
$tweet;
}

$twitterFeed file_get_contents($feed);
echo(
'&quot;'.parse_feed($twitterFeed).'&quot;');
?>


and here is where you can see the errors
http://www.scriptedintelligence.com/test/testor2.php (http://www.scriptedintelligence.com/test/testor2.php)
Title: Re: php rss
Post by: sybershot on April 23, 2012, 01:45:57 AM
I found a viable solution, using a java widget, it works so I am pleased to a point for now. would like a php method for seo reasons though  :scratch-head:
my home page now displays my latest tweets ;D
Title: Re: php rss
Post by: DaveMorton on April 23, 2012, 05:42:59 AM
Your hosting provider has either configured PHP to run as CGI, or has altered the settings in the "common" php.ini file to disallow PHP's file functions to open URLs; or possibly even both. What this means, basically, is that PHP functions, such as file_get_contents() can only open files that exist within your "web space". There's no "direct" way around this, I'm afraid; at least, not with respect to the script that you're using. I've run into the same problem with the Program O project that I'm working on, for exactly the same reason. Let me see if I can dig up the code I used to open off-site RSS feeds, and I'll post it here.
Title: Re: php rss
Post by: DaveMorton on April 23, 2012, 06:29:28 AM
Ok, here's what I use currently. This function relies on the server's PHP library supporting cURL, but that describes over 90% of all PHP installs for web servers. If your hosting provider doesn't include cURL support with their PHP install, then you can probably ask them to include it.

Anyway, without further ado:


  function getRSS($feedURL) {
    $out = '';
    if (function_exists('curl_init')) {
      $ch = curl_init($feedURL);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      $data = curl_exec($ch);
      curl_close($ch);
      $rss = new SimpleXmlElement($data, LIBXML_NOCDATA);
      if($rss) {
        $items = $rss->channel->item;
        foreach ($items as $item) {
          $title = $item->title;
          $link = $item->link;
          $published_on = $item->pubDate;
          $description = $item->description;
          $out .= "<h3><a href=\"$link\">$title</a></h3>\n";
          $out .= "<p>$description</p>";
        }
      }
    }
    else $out = 'RSS Feed not available';
    return $out;
  }


Feel free (of course) to make whatever edits to the function code that you need to, to make this compatible with your site. And if you have any questions, just let me know. :)
Title: Re: php rss
Post by: Freddy on April 23, 2012, 12:51:06 PM
Nice one Dave.  Usually the bug bear for me is FSOCKET not being allowed and then I have to use CURL.  I've not had to do that for RSS, so I will pinch this too for the future. Cheers :)
Title: Re: php rss
Post by: sybershot on April 23, 2012, 16:41:52 PM
Thanks Dave, your wisdom is greatly appreciated. I really thought it my code that was the issue, it is nice to know it is not the case. not knowing php well or errors that follow sure can be daunting. I can't thank you enough in words for all your help.

just started learning php beyond copy and pasting and changing code to suit my needs, now I have to learn cURl  :'(  however It looks similar to php, so hopefully it should not be too hard  ;)

Thanks for the code I'll play around with it, and see if I can get it connect to my twitter account, and if my server provider supports it. From a quick look at your code, it looks like I just need to set some variables.

Title: Re: php rss
Post by: DaveMorton on April 23, 2012, 16:43:38 PM
Just yell if you get stuck, or have questions. One of my favorite things in life is helping others. :)
Title: Re: php rss
Post by: sybershot on April 23, 2012, 17:23:03 PM
I love helping others as well  :thumbsup: :thumbsup:
1 quick question is there a way trace

so I test what part works or where something might be broken or not working?

Flash as3 example:
trace($ch);
Title: Re: php rss
Post by: DaveMorton on April 23, 2012, 17:56:35 PM
Not really, but a tracing function could certainly be created. I just don't have enough time right now to make one. What I usually do is place the function into it's own PHP file, and use that file to test and refine the function until it does what I want it to, then migrate the function to it's "home". :)
Title: Re: php rss
Post by: Freddy on April 23, 2012, 18:46:45 PM
Or use an IDE with debugging features.  Something like NetBeans will do that kind of thing :

http://netbeans.org/index.html (http://netbeans.org/index.html)
Title: Re: php rss
Post by: Freddy on April 23, 2012, 18:50:19 PM
And this is quite nice : http://www.php-debug.com/www/ (http://www.php-debug.com/www/)
Title: Re: php rss
Post by: sybershot on April 23, 2012, 19:35:54 PM
Thanks again  Dave and Freddy,
If I need to trace I will probably use Freddy's second suggestion, I like idea that it places the trace in a div within the html.

I had to do a few errands, but there done now time to play :)
Title: Re: php rss
Post by: Freddy on April 23, 2012, 19:52:52 PM
Glad to be of service :)  Hitchhikers....  ;)

Take a look at NetBeans some time though, it's a very nice thing.  I tend to do all my code in editors but sometimes running it though the IDE helps finds problems I might have overlooked.

Title: Re: php rss
Post by: sybershot on April 23, 2012, 19:57:31 PM
will do Freddy, just can't do on this machine atm for my hard drive space is near full.
once I build my new case I will be using this ssd for my other mobo.
Title: Re: php rss
Post by: sybershot on April 23, 2012, 21:05:30 PM
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, 0);

what  parameters are true and 0 setting? I tried to do a search but unable to find
Title: Re: php rss
Post by: sybershot on April 23, 2012, 22:01:49 PM
I had to cut the code down Dave and change some things, but thanks to you I got a ruff working php twitter feed on my site  :worship:

here is what I'm using atm
<?php

function 
Twitter_feed($path){
        
$ch curl_init();
        
curl_setopt($chCURLOPT_URL,$path);
        
curl_setopt($chCURLOPT_FAILONERROR,1);
        
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
        
curl_setopt($chCURLOPT_TIMEOUT15);
        
$retValue curl_exec($ch);                      
        
curl_close($ch);
        return 
$retValue;
}
$sXML Twitter_feed('http://twitter.com/statuses/user_timeline/ScriptedIntell.rss');
$oXML = new SimpleXMLElement($sXML);
foreach(
$oXML->channel->item as $oDocuments){
        
        
$published_on =$oDocuments->pubDate;
$description $oDocuments->description;

        echo 
$published_on "<br>" $description "<br>" .  "<br><br>" ;
}
?>