- Creating the Form To Make the Request
- Creating the Custom PHP Object
- GET and POST with AJAX
- Parsing the Response
- Whats Next?
Creating the Custom PHP Object
I created a small RSS class with PHP, which creates a copy of a requested feed on the local server so that it can be accessed by the XML HTTP Request object that we’ll create shortly. Typically, you can’t request a file cross-domain, meaning that the file that you’re requesting needs to be located on the local server. This class is a workaround to the cross-domain issue because it creates a copy of the feed that was requested on the local server and returns the local path to the feed, which is then accessible by the Request object.
The only method in the class is a request method, which takes one parameter, the URL to the requested RSS feed. It then checks whether a directory by the name of rss is located on the local server. If not, it creates one and sets the permission mode to 0666, which means that the directory is readable and writeable. It’s set to readable so that the directory can be accessible at a later time and set to writeable to allow a copy of the feed to be written to the directory on the local server:
// Make directory if one does not exist $dir = "rss"; if(!is_dir($dir)) { mkdir($dir, 0666); }
Before copying the feed to the server we need a unique filename. I’m using the md5 encryption method on the entire URL to ensure that the names of all feeds are unique. With this new filename, concatenate a string that represents the path to the file; this will be used when creating the copy of the feed:
// Create unique names $file = md5($rss_url); $path = "$dir/$file.xml";
Using the path that was defined above and the reference to the original URL of the requested feed, we can now create a copy of the file. Finally, return the path to the new file as the response to the request:
// Copy feed to local server copy($rss_url, "$path"); return $path;
Following is the small, yet powerful RSS class in its entirety:
<?php class RSS { function get($rss_url) { if($rss_url != "") { // Make directory if one does not exist $dir = "rss"; if(!is_dir($dir)) { mkdir($dir, 0666); } // Create unique names $file = md5($rss_url); $path = "$dir/$file.xml"; // Copy feed to local server copy($rss_url, "$path"); return $path; } } } ?>
In order to access the request method in the PHP class, there’s a request file that acts as an interface to the class, which is the file that we’ll be requesting. This file first verifies a password variable from the requesting query and either returns a message stating that the requester is not an authorized user or responds with the path to the RSS feed that was copied to the local server after it has been processed by the request method. To respond with the RSS feed, the RSS object needs to be included and instantiated, and the request method needs to be triggered with the URL of the feed requested as a parameter:
<? if($password == "mypassword") { require_once(’classes/RSS.class.php’); $rss = new RSS(); echo $rss->get($request); } else { echo "You are an unauthorized user"; } ?>