PHP Howto: scrape an RSS feed

Posted on 29th March 2011 in Tutorials

Since I’m building my own homepage, I recently learned how to scrape an RSS feed in order to dynamically create content for a website. The idea is that I would have separate feeds from my twitter, tumblr, and this blog all in one place. The tumblr and twitter feeds are offered by those companies in the form of API calls, so using those two is very straightforward in both cases. When self-hosting a wordpress blog, though, as I do, there is no readymade option for a feed that one can just call and have ready to go. So I had to make my own.

An RSS feed is essentially just an XML markup document that browsers interpret and show you in some type of feed form. What I wanted to do with this document (which wordpress produces for me) is essentially the same as what the browser does with it when you click an RSS link – it parses the tags in the XML document and applies predefined visual styles to make the information accessible to humans.

PHP can perform this process quite simply, via the simplexml_load_file function, which provides a simple framework for parsing XML documents.


$feedUrl = 'http://emmettbutler.com/threestegosaurusmoon/?feed=rss2';
$ret = array();

// retrieve search results
if($xml = simplexml_load_file($feedUrl)) { //load xml file using simplexml
$result["item"] = $xml->xpath("/rss/channel/item"); //divide feed into array elements

foreach($result as $key => $attribute) {
$i=0;
foreach($attribute as $element) {
if($i < 3){
$ret[$i]['title'] = (string)$element->title; //assign the desired elements to array entries
$ret[$i]['timestamp'] = (string)$element->pubDate;
$ret[$i]['summary'] = (string)$element->description;
$ret[$i]['link'] = (string)$element->guid;
$i++;
}
}
}
}

After the initial call, this code examines each unit of the divided document and assigns the contents of certain tags to elements of the $ret array. For example, there is a line in each item of the feed that is denoted by the pubDate tag, which contains the date that a certain post was published. The line $ret[$i]['timestamp'] = (string)$element->pubDate; finds those tags and assigns their contents to the $ret array. Once this loop is complete, you’ll have an array full of all the pertinent data for your feed. You can loop through the array and print each element between the appropriate tags, style with a bit of CSS, and you have yourself a homemade and very professional-looking RSS feed widget on your website.

comments: 0 » tags: , , , , , , , ,

My Online Life Expands

Posted on 25th March 2011 in Something Daily

Today involved my first use of an API in any real capacity, as I used some websites I visit as examples of how to add Twitter and Tumblr feeds to a page. (By the way, I started a Tumblr.) It occurred to me today what I really want emmettbutler.com to be: ideally, it will be the hub of all aspects of my internet life. It’ll have this blog, my twitter, and my tumblr all represented in the form of feeds, as well as my resume, bio, and a gallery of stuff that I’ve made. In terms of these feeds I’m talking about, they’re surprisingly easy to implement – a lot more so than I expected. In the case of the tumblr feed, it’s actually just one javascript call that returns your ten most recent posts, and the twitter one is just two calls. Very nice. This is quite helpful in getting ready for the HackNY hackathon on April 9th, as I was looking for an excuse to learn practical applications of APIs like these (who am I kidding…I really don’t need an excuse). But to make a long story short here, as of today I’m starting to pick up steam on this personal website project. Also everything that I previously had on my roommate’s web host is now here on my own, so it’s certainly a good thing that I don’t have to keep directing people to emmett.ericsluyter.com. That was a little embarrassing. Not really though.

I once again haven’t really had a time since Tuesday in which I wasn’t doing anything productive, and that is continuing, as I’ve been either web designing, writing html, drawing a triceratops with aviators, or doing studio maintenance all day today, and I plan to continue this at work until I go to bed tonight. I will eventually burn myself out and be forced to take a break, probably. I’ll just watch a few more episodes of Dragon Ball Z. I’m going to see Eric’s show tomorrow at Gallatin, which should be super rad (and also something that’s not ‘work’).

Starting to Build a Message Board

Posted on 6th March 2011 in Something Daily

It just occurred to me that life is too short to not write about whatever you want on your blog.

I’ve been teaching myself MySQL, PHP, HTML, and CSS over the last handful of days, originally starting with the W3Schools tutorial. That very quickly grew into the seed of an idea for an introductory project which, when it’s complete, should give me a command of the skills I need to have for the hackathon in April. So the idea is that I’m going to put together a message board, with 4chan as a template/inspiration. I want to have nice looking page layouts (not looking for anything horribly fancy, as I’ve never really used CSS in my life), as well as the functionality of viewing/starting/replying to comment threads and attaching images to comments. Eric gave me a subdirectory on his web host, so I’m using that to practice uploading files. It’s going well so far, I’ve figured out how to use a text file to store the information on submitter name, image, and comment content, and to display them in something vaguely resembling a thread format. The next step is to learn how to do that with MySQL databases instead of text files. That shouldn’t be too much of an issue (at least I hope not).

One other thing – this morning I downloaded Odd Future’s demo tape from their website and it’s some of the most evil rap I’ve ever heard, and the rhymes are so good. Also, Tyler the Creator just celebrated his 20th birthday today, and Earl Sweatshirt is 16. Check out OFWGKTA (this video is not safe for work or small children or anyone who doesn’t especially like profanity).

Preparing for Hackathon 2

Posted on 3rd March 2011 in Something Daily

Something exciting is on my mind since Tuesday: the next HackNY hackathon has been announced. I attended my first in October, but I didn’t know enough about anything that was going on there to really participate at all. I spent about five hours coding Spaceratops there, which I was in fact working on back in October. Side note: this is why I want to be done with that project. It’s taking too long. But anyway, the idea with the hackathons is that a bunch of representatives from startups come and demonstrate what their company’s API can do and then they give you a key and tell you to go nuts for the next twenty hours and hack together some sweet app from the tools that everyone’s given you. In October, this really was my first exposure to PHP, HTML (in recent years) and the idea of using API calls to build apps. So I didn’t join a team, I didn’t really meet anyone, and I didn’t build anything. I did make a lot of progress on Spaceratops that night. The experience of hanging out until all hours coding is extremely appealing to me, though (as is the prospect of free snacks, soda, and burritos all night), so I’m doing everything I can to prepare myself for the upcoming event. Whether I go by myself or with a friend, I’m going to introduce myself to some people, join their team, and help build something awesome. It’ll be awesome.

So to that end I’m going through as much of the W3Schools PHP tutorial as I think is necessary, and then I’m going to make something cool on my own. That’s the only way to learn this stuff: by actually building something. It may very well end up being something that uses Google maps or a calendar API – the choice is all mine. I just got PHP and MySQL running on my local server (for about the fourth time) and I’m tutorialing like crazy. Also Eric gave me subdirectory on his website, so I have some actual hosting. This may allow me to make Spaceratops into an applet that would be playable in a browser…that would be super rad. We’ll see.

And guess what I’m doing this weekend!? That’s right, more homework and coding. If I have my way, I’ll also finally finish Chrono Trigger. I’m at the last battle and it’s so nuts. Let’s do this.

Eric Sluyter owes me some Dibs

Posted on 4th November 2010 in Something Daily

So around this time last year, my suitemate Eric promised me that if I ever finished Ikaruga, he’d buy me some of these:

For those of you who don’t know, Ikaruga is an arcade-style shooter that was released on Gamecube in 2001, known for its high degree of difficulty and innovative polarity-switching mechanic. People have called it a “bullet hell” game, in that at some points there are so many enemies and projectiles on the screen that it’s extremely difficult to find the safe areas. It’s a cult favorite these days, mainly because of how hard it is to finish. I got it about 2 years ago from a Gamestop (for 40 bucks used, it’s apparently rare), and I’ve been playing it fairly regularly since then trying to get past the especially difficult 4th level. I eventually did that and recently have been working on the fifth.

And I just finished it.

So now I can finally shut up about it on the blog, and to everyone I talk to in person.

So Eric, whenever’s good for you, you know? There’s no rush. Really.

-&-

Here’s another render that I did a few weeks ago with Blender (haha, blender render), this time of one of the iconic blocks that Mario has punched in so many games over the years. A little better with the lighting here.

Mario '?' Block, rendered 10/2/10

I finished my initial foray into PHP today, after learning how it’s used to grab data from MySQL databases, and how it makes HTML really easy. PHP is awesome. Also, I’m going home this weekend! I’ll probably still post, but it’ll be from Pennsylvania! Check it out!

Speaking of Blender, here’s the famous iPad Blend. Yes, it’s what it sounds like.

Vegeta! What does the scouter say about his room number?

Posted on 2nd November 2010 in Something Daily

My room at NYU

It’s over NINE THOUSAAAAAAAND! This is my dorm room door at school. Vegeta’s pretty angry about how high the number is; so much so, in fact, that he smashed his scouter over it.

Paying attention in class is tough when there’s so much you want to learn (ironic, isn’t it?) I spent a good portion of this morning going over a PHP tutorial and learning how it’s used to generate HTML. Having been discouraged from writing HTML in the past by how apparently daunting the process was, the knowledge that PHP can be used to speed up the process is quite encouraging. I learned from the a tutorial on devzone.zend.com the basic syntax, as well as how to use PHP in HTML pages to dynamically generate the dimensions of a table. I think it’s awesome to have the two so well integrated that all it takes to make a bit of content if for your program’s output to be in HTML format – and way less work than it could be!

It’s exciting to learn a new language. I remember being probably 8 or 9 and waking up really early in the morning to sneak to the downstairs desktop computer and page through my dad’s copy of “HTML & XHTML: The Definitive Guide” from O’Reilly. I think I built a website explaining how to do all kinds of different card tricks. It never went live, but it might still be on that home computer. I’ll dig up the source for it if it’s still around and put it up here…it’ll probably be comedy gold. Unfortunately, maybe due to my years with the School of Rock, I pretty much completely forgot how to write HTML and any other basic coding knowledge that I may have amassed; so when I started taking programming classes last year, it was all essentially new to me. The point is, I’m excited to get into making websites again, with much more enthusiasm this time! Hopefully this works out…

I just got within 23 seconds of finishing Ikaruga again. Before, I would feel pretty good about finishing around that area of the game, because it meant I was generally improving. But now, with success within my grasp, every time I make it to the challenge stage and fail is just a slap in the face. I know I’ll get it eventually, but the sting of defeat is a new and unwelcome sensation in my gaming experience. (In Ikaruga at least – in Team Fortress 2 it’s completely the norm, for example).

I listened to CSNY’s Deja Vu this morning – that is a great album. You probably already knew that. But they just have an awesome, classic sound. Check it out.

This is another webcomic that Justin turned me on to a few days ago. It’s called “Axe Cop“: written by a 5-year-old and illustrated by his 29-year-old brother. It’s so ridiculous (some would say…random?), and the fact that it’s written by a five year old may be what makes it funny, but hey….it’s funny. Check it out.

Thanks for reading!