Archive for the ‘PHP’ Category

PHP 5, Twitter & XMLReader

Wednesday, October 22nd, 2008

I was interested in using and experimenting with the XMLReader class that is part of PHP 5, and thought why not test it out using my Twitter feed. Turns out, the feed contained more information than I really wanted, so I had to find a way to extract only what I needed. To that, I present the following, simple and basic script to extract just the text of the latest update on Twitter.

A basic Twitter feed might appear like mine below:




http://twitter.com/southplatte

Twitter updates from southplatte / southplatte.
en-us
40

southplatte: working on database stuff with MySQL

Tue, 21 Oct 2008 05:35:21 +0000 http://twitter.com/southplatte/statuses/968543230

http://twitter.com/southplatte/statuses/968543230


As you can see we get a channel name with related information, and then the items, or Tweets. In this case, Twitter has used some duplicate XML tags, namely title, description and link in both the channel element and item elements, so our script should pull only the item information, unless we wanted to publish the channel information, which for this we will not.


1. 2. $reader = new XMLReader();
3. $reader->open(‘http://twitter.com/statuses/user_timeline/twitterusernamehere.rss?count=1/’);
4.
5. while ($reader->read())
6. {
7. if ($reader->nodeType == XMLREADER::ELEMENT)
8. {
9. $name = $reader->name;
10.
11. if($name == “item”)
12. {
13. while($reader->read())
14. {
15. if($reader->nodeType == XMLREADER::ELEMENT)
16. {
17. $noName = $reader->name;
18. if($noName == “description” || $noName == “pubDate” || $noName == “link”)
19. {
20. $reader->read();
21. $result .= $reader->value.”
“;
22. }
23.
24. }
25. }
26.
27. }
28.
29. }
30.
31. }
32.
33.
34. $reader->close();
35.
36. ?>

What the script does:
Line 2 creates a new instance of an XMLReader object
Line 3 opens the RSS feed from Twitter – the username will be your Twitter user name (or what ever Twitter user name you want to get updates for) and the count URL parameter tells it to only retrieve the latest Tweet. You can ommit the count and get all, or specify another number here.
Line 5 starts a while loop that will loop through the entire contents of the XML file returned from our open command.
Line 7 checks for the node type and if it is equal to an ELEMENT, takes action.
Line 9 sets the value of the Element.
Line 11 Validates the element to be equal to item, item being the element that contains our tweet.
Line 13 begins a new while loops (almost a recursive action here) and moves through the file again, to get the actual Tweet text from within the Item element.
Line 15 again checks that we are working with elements
Line 17 assigns each element to the variable
Line 18 checks the variable to be equal to the elements that we want, description, pubDate and link.
Line 20 tells the object to read the XML/RSS stream
Line 21 appends the $result variable with the values from our selected elements.
Line 34 closes our XMLReader stream.

Now all we need to do is include this into a PHP page, echo or print the $result variable and we will get an output like the following:


southplatte: working on database stuff with MySQL
Tue, 21 Oct 2008 05:35:21 +0000

http://twitter.com/southplatte/statuses/968543230

southplatte: Another interview setup for Wednesday, still waiting on the one from last week to decide, it is between myself and one other person!
Sat, 18 Oct 2008 15:28:30 +0000

http://twitter.com/southplatte/statuses/965216528

As you can see, we have some formatting issues to deal with, such as double spacing between Tweets, maybe formatting the date string, and encasulating the entire tweet with the link so it could be clicked and take the visitor to the individual Tweet on the Twitter web site.

The final page I have it in on my testing server is:






$reader = new XMLReader();
$reader->open(‘http://twitter.com/statuses/user_timeline/southplatte.rss?count=1/’);

while ($reader->read())
{
if ($reader->nodeType == XMLREADER::ELEMENT)
{
$name = $reader->name;

if($name == “item”)
{
while($reader->read())
{
if($reader->nodeType == XMLREADER::ELEMENT)
{
$noName = $reader->name;
if($noName == “description” || $noName == “pubDate” || $noName == “link”)
{
$reader->read();
$result .= $reader->value.”
“;
//break;
}

}
}

}

}

}

$reader->close();

?>



This could definitely not be used for much, but you could also set up a very similar and though not quite as simplistic page to read any RSS feed, thus creating your own PHP RSS/XML reader. It could definitely be extended and enhanced to perform some very advanced functionality as well.

Available for Freelance and other work

Monday, October 6th, 2008

I am currently accepting new projects, either on a freelance basis or on an employment basis. The employment basis may be either a temporary, temp-to-hire, contract-to-hire or direct hire situation. At this time I am not available for relocation, so local opportunities (those that are not available via telecommuting/offsite work) must be within the Front Range to Southern Colorado area.

Use my contact page if you are interesting in discussing any potential opportunities.

PHP – Creating Effective User Acces: Database Design

Wednesday, October 31st, 2007

Okay, when we look at creating effective user access, the first thing we have to look at is the database design. As I mentioned in the overview post of this series, we will not look in-depth at how to connect to a MySQL database, the code will be shown, but not explained. However, we do need to explain a decent table layout, as well as define what tables are needed in our example here.

One thing I should mention is some specifics that I will be basing the rest of this series off of, in others here is the list of requirements I am going to meet. While the list is not extensive or exhustive, it provides a good base to which you should be able to extend to fit your requirements list.

  1. The site needs to log users in and out.
  2. The site needs to keep users logged in for a minimum of 15 minutes.
  3. The site needs to keep users logged longer if they desire to choose “Remember Me”.
  4. The site needs to authenticate on all pages.
  5. The site needs to logout users by session timeout, cookie timeout and user forced logout.
  6. The site should log all login attemps, whether successful or not for security purposes.
  7. The site should be able to provide the user with their last login date and time.
  8. The site should operate over a secure connection.
  9. The site should lock an account after three unsuccessful login attempts, if the account login name is correct and the password is wrong, and it should stop accepting submissions if the user name is not found within three attemps and log the error.

On to the MySQL database definition…..

(more…)

PHP – Creating Effective User Access

Wednesday, October 31st, 2007

This tutorial requires that several basic sets of knowledge be known by the user already. One is you must be proficient with HTML/XHTML, two is that you already know some basic PHP programming in an object-oriented fasion and three that you know how to connect to a MySQL database using PHP. Why are these required? Becuase this tutorial is not about teaching PHP basics, nor is it about how to connect to a MySQL database. It is not about teaching you HTML/XHTML nor is it about object-oriented program development.

There are several items that you need to figure out before we can continue with our discussion of user access. These items will determine how you implement the user access control. I have listed them below:

  • Do all pages on the site need to have user authentication, or only a subset of them?
  • Will the site use SSL connections or just basic plain text authentication?
  • Will the site offer to “remember” users after the leave the site and come back or automatically log them out when they leave the site or follow the logout button/link?
  • Will the site offer content only to those logged in, such as a user control panel where the user may add/change/remove their information or a special section that is dependent upon their access level?

Once those questions are answered, you will find that the tutorials step through first generating the user database that is universal to cover all those needs and more, then we will setup basic authentication using an SSL encrypted page, then we will setup sessions, session time limits, cookies for user rememberance and finally discuss restricting access to pages via user login authorization level.

This is the overview of a several part series that will be posted over the coming days.

XHTML, PHP, Validation and Browsers

Thursday, September 27th, 2007

Recently for a school project I was working on, I had to develop an XTHML 1.1 Strict validated page and a validated CSS to style the page. This was not a problem in and of itself, however there were several problems that I encountered while trying to serve the pages to different browsers and to the W3C’s validator.

What this post will attempt to do is cover several items that I found necessary to accomplish a page that displayed properly in Internet Explorer 7, Firefox 2, Netscape 9, SeaMonkey, Opera and Safari for Windows. I have not fully tested the resulting pages on the Linux or Mac platforms yet, so I will attempt to do that when time permits. I did however use BrowserCamp to get a screen shot of what it looks like in Safari 2.0.4 and there were some CSS issues (I assume anyway).

(more…)


View in: Mobile | Standard