Jump to content

rss_reader class problem.


Xdega

Recommended Posts

So... I have the following code for my rss_reader class:

 


<?php
class rss_reader {
    const USE_CURL = TRUE;
    
    private $_userAgent = '';
    private $_url = '';
    
    public function __construct($url, $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1') {
        $this->_url = $url;
        $this->_userAgent = $userAgent;
    }
    
    public function writePOST() {
$postval= $this->getPOST();
echo $postval;

    }

    public function getPOST($format = '<b>%s</b>%s<i>%s</i>') {
        $xml = $this->getXML();
        $script1 = $xml->channel->item->title;
        $script2 = $xml->channel->item->description;
$script3 = $xml->channel->item->pubDate;

        return sprintf($format, $script1, $script2, $script3);
    }
    
    private function getXML() {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->_userAgent);
        curl_setopt($ch, CURLOPT_TIMEOUT ,3);
        
        $data = curl_exec($ch);
        curl_close($ch);
        return new SimpleXMLElement($data);
    }
}

 

Basically, atm it writes the newest post found at the RSS feed.

 

What I wan't to do is extend this to create an array of all of the posts that I can iterate over to "write" ALL of the posts. I will then later work on a method for pagination.

 

Atm though, I am kinda lost and don't really know where to start.

 

Anyone know how I could accomplish this?

 

thanks much.

 

ps: everything else with the rss reader works as intended.

Link to comment
Share on other sites

You can pretend that $xml->channel->item is an array*, which means you can (among other things) foreach over it.

public function getPOSTS($format = '%s%s%s') {
$xml = $this->getXML();
$posts = array();
foreach ($xml->channel->item as $item) {
	$posts[] = sprintf($format, $item->title, $item->description, $item->pubDate);
}
return $posts;
}

$reader = new rss_reader("http://www.example.com/rss");
echo "Items:
\n* ", implode("
\n* ", $reader->getPOSTS());

 

* I say "pretend" because it isn't actually an array. It's an object.

Link to comment
Share on other sites

I c. Interesting. One question though, Would you also suggest me moving the "echo" of the posts to the "view" instead of using my WritePost() method? Or does it not matter?

 

this is the current client code (markup that I am using in my index.php) that I am using:

<div class="blog_post">

	<p><?php $post= new rss_reader("http://lhockley.tumblr.com/RSS");
	$post->writePOST();?></p>

</div>

 

Thanks.

 

 

Link to comment
Share on other sites

You should separate the model (the rss_reader class) from the presentation (index.php). It's not a requirement but a very good practice to get into.

 

I would do something like

public function getPOSTS() {
return $this->getXML()->channel->item; // nothing else to do, really
}

$feed = new rss_reader("http://www.example.com/rss");

?>
...


getPOSTS() as $post) { ?>

%s%s%s", $post->title, $post->description, $post->pubDate); ?>

(And actually I would abstract it out some more, but, well, baby steps.)

Link to comment
Share on other sites

I see. Yeah. The problem I seem to be having the most... Is knowing how much code to put in the model(class), and how much to put in the presentation(index.php).

 

My initial state of thinking is to have the class do most of the work, then create an object in the presentation(index.php) with a few parameters, to produce the result.

 

Are there any resources on this particular topic that I can check out?

 

Link to comment
Share on other sites

No offense, but Google is one of the best resources you can find. Large sites with articles or tutorials (except w3schools, avoid them) are good, forums are helpful, blogs are hit-or-miss...

 

The MVC design pattern is a good place to start. One of its core principles is three layers: the model (database) which holds the information, the view (PHP page) which shows data, and the controller (code) which lets the other two layers communicate.

My last post even uses MVC: the model is the rss_reader class, the view is the foreach loop, and the controller is the code that gets the RSS feed into $feed. Well kinda, "true" MVC is a little harder to pull off in PHP without some framework, but it's the principles that really matter.

Link to comment
Share on other sites

No offense, but Google is one of the best resources you can find. Large sites with articles or tutorials (except w3schools, avoid them) are good, forums are helpful, blogs are hit-or-miss...

 

The MVC design pattern is a good place to start. One of its core principles is three layers: the model (database) which holds the information, the view (PHP page) which shows data, and the controller (code) which lets the other two layers communicate.

My last post even uses MVC: the model is the rss_reader class, the view is the foreach loop, and the controller is the code that gets the RSS feed into $feed. Well kinda, "true" MVC is a little harder to pull off in PHP without some framework, but it's the principles that really matter.

 

I agree Google is amazing. But hard to know what to search with regards to my very specific problem, and can't compare to direct discussion with someone who knows what they are talking about. (hope you don't mind)

 

The MVC design pattern sounds like the kind of info I need to find. So in a nutshell basically the View and Controller are both put in to the index.php? One thing that worries me a little is the expandability to provide pagination support. Would this be more code that I would put in the View (alongside the foreach loop?) or would it be better practice to somehow separate the pagination system (as I could see pagination being a hefty amount of code to dump in the index.php).

 

thx again.

 

ps: I guess I could always cop out and show the latest 5 posts, then a url link to the original tumblr. As much as a pagination system would be nice to create, my level of expertise is very very novice :(

ps2:

I am looking forward to taking ProgrammingII (next semester) and study JAVA. I think it will be some great practice at OO Programming. Atm I am stuck with ProgrammingI which is basically procedural C++ (yuck!). I am thinking that my PHP skills will vastly improve once I have learnt a lot of JAVA.

 

 

 

 

Link to comment
Share on other sites

Disclaimer: I take the mentality that design patterns (such as MVC) are suggestions, not rules. Many people disagree with that.

 

 

It doesn't actually matter where the code for each part is. What matters is what the code does and how it communicates with the other code.

"Traditional" PHP mixes the M/V/C all together into one mess. The next step from there is separating out the model (eg, using object-oriented programming) but the view and controller are still typically meshed together. Over time, many programmers discover that putting complicated code at the beginning of the file and the simpler code towards the end greatly helps, and that's typically how PHP MVC ends up: a set of classes somewhere (=model) that are include()d by various .php scripts, and within these scripts there are parts that load and transform data (=controller) and then there are parts that display the data (=view).

 

One step beyond that is to use other design patterns, like the Front Controller (no relation to MVC!), to develop a framework. This Controller pulls in requests to various scripts and decides how they should be handled. Via some configuration, requests for pages like "/rss" get thrown to something like a BlogController class's RssAction function. These two form the controller (the MVC one) which loads up whatever information it needs via the model. Execution then goes on to a view which takes that loaded information and displays it however it wants - like in an RSS feed.

 

Side note: Ruby on Rails is considered one of the best implementations of MVC around. Having seen Zend Framework, I'm quite a fan of ASP.NET's MVC 3.

 

ps: I guess I could always cop out and show the latest 5 posts, then a url link to the original tumblr. As much as a pagination system would be nice to create, my level of expertise is very very novice :(

As far as I'm concerned, pagination can go in the controller or the view. When you put it in the controller you're being very specific with the information to use, but it requires the controller knowing what the view will do. When you put it in the view you're being totally open to how the information is displayed, but it requires the controller potentially passing a lot of information that won't be used.

 

More often than not it happens in the controller because, after all, the controller has to know how the view works to some degree to be able to do its job - like handle form POSTs. So go with that.

define("PERPAGE", 5);

$feed = new rss_reader("http://www.example.com/rss");
if (!empty($_GET["page"]) && ctype_digit($_GET["page"])) {
// paginate
$pagedfeed = array();
for ($i = ($_GET["page"] - 1) * PERPAGE; $i >= 0 && $i 	$feed = $pagedfeed;
}

?>

 

ps2:

I am looking forward to taking ProgrammingII (next semester) and study JAVA. I think it will be some great practice at OO Programming. Atm I am stuck with ProgrammingI which is basically procedural C++ (yuck!). I am thinking that my PHP skills will vastly improve once I have learnt a lot of JAVA.

It will help you understand object-oriented programming, but it will not improve your PHP skills in general. Learning programming is about learning how to use languages to accomplish tasks. Even if you know what code to write in Java, you can't take that code and transcribe it to PHP and expect it to be perfect - or even correct. Instead, take the reason you wrote that code and convert that meaning into PHP.

Otherwise you'll find yourself running into the limitations and quirks of PHP's OOP implementation (LSB, inheritance rules), and even missing out on some of its cool features (traits, magic methods).

Link to comment
Share on other sites

Thank you so much for all of that information. I have definitely been told many a time that in programming concept is the most important thing. Although I have learnt that there is a lot of "shared syntax", for example I find assignment operators particularly interesting and they appear to be used in php, c++ and java in very similar syntactical forms.

 

I am definitely getting a solid grasp for OO Programming, and very much love the way it works. Design patterns are something that I definitely need to study though I think.

 

Anyways, thank you much for the information you provided. Once I can fully understand the pagination code (I NEVER copy/paste code unless I fully understand how it works and what it is doing), I should be able to use it in my rss/blog reader and it will likely be a class that I will use a LOT once I have it well written. I love pulling information from RSS feeds. I may even find myself creating my own RSS feeds from scratch, then pulling the feed in to blogs etc down the road. 

 

Then I am sure, once I learn a framework, there will be RSS reader classes that are written for me. As much as I like the idea of writing my own.

 

I have had in interest in Symfony2 for a whille, I actually use the class_autoloader.php concept that SF2 uses to auto-load my classes in my own code.

Maybe once I am a little more comfortable, I will buy a book on SF2 and play around with it.

 

Thanks again for your insight.

-Liam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.