I had a friend ask me recently about using the Twitter API with PHP and if I could show him a way to do it. So, this is the first post of my tutorial on how to use the Twitter API. The tutorial will show you how to get a list of your friends and followers, with their statuses. Also, this tutorial assumes you have an environment with Apache and PHP running.
I am making some specific decisions with these posts:
- I am going to use the Zend_Service_Twitter class because it makes things easy
- I am NOT going to put this in full MVC, because this is for beginners and because that is alot of overhead for what I am trying to do
The Twitter API is a fairly well featured API which is easy to use, especially when you have the right tools. One of the tools that I will often use, is the Zend Framework Twitter Service class. The reason that I do this and don’t write all the code myself is, “Why skin the cat again?” – it’s cruel and unusual punishment to skin the cat the first time…
First thing’s first though, we need to set up the environment so that we can both use the Twitter API and use the Zend Framework classes without having to do too much extra work.
The Zend_Framework is an OOPHP framework with a specific naming convention for its classes. Each of the names of the classes can be mapped straight to the folder path for those classes. If your class was called A_B_C the file path would be A/B/C.php. You can handle this string conversion in PHP this way:
$string = 'A_B_C';
-
//replace the '_' with '/' in the class name
-
$path = str_replace('_', '/', $string);
So, now that we know how to convert the string, we need to get the class names as they are instantiated.
A class in PHP is instantiated using the new operator like so:
-
$classVariable = new Class_Name_Here($params);
Where:
- $classVariable is ANY variable name, typically you will name this appropriately to the class name
- ‘Class_Name_Here’ is the name of the class you are calling
- $params are the parameters you want/need to pass to the class constructor. (Some classes don’t require this at all)
When a class is instantiated, PHP will do a couple things. First, it will look to see if there are any class definitions that are local to the current scope of the script that is being run. This will ONLY happen if the class file is included, or the class is defined in the current script. Second, PHP will look for the __autoload magic function. This function will receive the name of the class that is being called and will give the programmer a chance to include the script that has the class. This is why Zend names their classes the way they do. The __autoload function we will use is here (notice the code from above):
-
function __autoload($className)
-
{
-
$path = str_replace('_', '/', $string).'.php';
-
require_once $path;
-
}
I used “require_once” here because you don’t want to include the same file multiple times, this can cause issues and is not worth the trouble or the time to troubleshoot. Also, note that I appended ‘.php’ to the end of the string, this is necessary as we are including a php script. The code above includes the class file and makes the class definition local to the current script so the object can be created. You will need this function available to every script that uses classes.
The last thing that we need in order to pull in classes dynamically as I have described above is a proper include path. The PHP include path is simply a base file path from which the server will look to see if files being included are found. So, in our example above where we have a class called “A_B_C” who has a file at path “A/B/C.php”, without an include path set the “A” directory would have to be in the same directory as the script you are running. When you are writing software you often want to compartmentalize (Read: Organize) your code into directories, which means that you will NOT have the “A” directory available and in the same directory as every script. This again, would be cruel and unusual punishment to you and anyone who works on your code after you. So, you define a place where you want to put ALL of your class files and then set up the include path to point to this location. The easiest way for a beginner to set the include path, is to pass it through a PHP system function called set_include_path. This will set the include path for the current run of the script you are using. This is what it will look like:
-
//the root path of your server, to the execution directory for the server
-
$rootPath = $_SERVER['DOCUMENT_ROOT'];
-
//set the include path with the current include path and the path to your class files
-
set_include_path(get_include_path() . PATH_SEPARATOR . realpath($rootPath.'/path/to/class/files/'));
You will find the document root of your server and set the include path by appending your class file path to your current include path. Now, whenever you include a file, PHP will look in your current directory, and any directories in the include path. The “realpath” function will cleanup any issues in the path like double slashes.
So, I know that this has been quite a lot and seems a little bit like “Seriously Matt, I just want to get information from Twitter, why are you telling me all this?”. I understand that this is true, but it is important to know WHY things work the same way that it is important to know THAT things work. I believe that when you know the why, you can understand what you are playing with and use it to its fullest potential instead of chalking it up to magic (PHP Magic functions excluded — nerd ?humor?).
Where has this gotten us? The following code is what you will need to have a PHP environment that is conducive to dynamically loading classes and using the Zend classes, so you don’t have to re-write all that code.
-
//the root path of your server, to the execution directory for the server
-
$rootPath = $_SERVER['DOCUMENT_ROOT'];
-
//set the include path with the current include path and the path to your class files
-
set_include_path(get_include_path() . PATH_SEPARATOR . realpath($rootPath.'/path/to/class/files/'));
-
-
/**
-
* Sets up the PHP functionality for dynamically loading classes based on class name
-
*/
-
function __autoload($className)
-
{
-
$path = str_replace('_', '/', $string).'.php';
-
require_once $path;
-
}
Having this code means that the next step is to use the Twitter API and avoid wasting time on skinning that damn cat.
Sphere: Related Content