Using the Twitter API with Zend Part 2: Logging in a User and Updating Status

Be sure to review the last article in this series:  Using the Twitter API with Zend Part 1: Setting Up Your Environment
I am going to set up my environment with the following file path structure:
  • htdocs - my document root
    • index.php - the file I will be doing development in
    • library
      • Zend - where all my Zend Classes will be
So to set up my environment, I will use the following code from Part 1:
//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.'/library/'));

/**
* Sets up the PHP functionality for dynamically loading classes based on class name
*/
function __autoload($className)
{
$path = str_replace('_', '/', $string).'.php';
require_once $path;
}
Now I will need to instantiate the Zend_Service_Twitter object, and log the user in.  I am doing this with constants, but for an applicaiton you would set up a login form, and take this information off the GET or POST parameters. Some of this code is from the Zend Service Twitter page.
define('USER', 'user');
define('PASS', 'password');

//instantiate the Twitter object
$twitter = new Zend_Service_Twitter(USER, PASS);

// verify your credentials with twitter
$response = $twitter->account->verifyCredentials();
Now, if the user is not properly authenticated, the $response will have an error in it, you can check for the error like so:
//check for isset and empty to be redundant
if (isset($twitterUser->error) && strlen($twitterUser->error) > 0) {
   echo $twitterUser->error;
   echo '
There was a problem, please try again'; //stop execution, because we do not want to go any further exit(); }
If the user passes the authentication, which means there are no errors we now want to post their status message to Twittter. Again, you would be pulling the status message from somewhere before posting it and probably not from an inline-defined variable:
$message = 'This is a message I am posting from a script I wrote!';
//use the Zend_Service_Twitter method for updating a user's status (so we don't have to write it)
$response = $twitter->status->update($message);
The complete script for logging a user in, verifying their credentials and posting a status update would look something like this:
//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.'/library/'));

/**
* Sets up the PHP functionality for dynamically loading classes based on class name
*/
function __autoload($className)
{
$path = str_replace('_', '/', $string).'.php';
require_once $path;
}

define('USER', 'user');
define('PASS', 'password');

//instantiate the Twitter object
$twitter = new Zend_Service_Twitter(USER, PASS);

// verify your credentials with twitter
$response = $twitter->account->verifyCredentials();

//check for isset and empty to be redundant
if (isset($twitterUser->error) && strlen($twitterUser->error) > 0) {
   echo $twitterUser->error;
   echo '
There was a problem, please try again'; //stop execution, because we do not want to go any further exit(); } else { $message = 'This is a message I am posting from a script I wrote!'; //use the Zend_Service_Twitter method for updating a user's status (so we don't have to write it) $response = $twitter->status->update($message); echo 'Your message was posted'; }
Developer's note: One thing you can do if you are not sure why something is not working, or if you run into trouble is to output the return data from the object's method calls by using HTML's <pre> tag with PHP's var_dump function to "print" all the data in a variable to the screen like so:
echo '
';
var_dump($variable);
echo '
';
Annoying I have to write this note: The last <pre> should have been </pre> but my syntax highlighter couldn't tell that it was inside the box, so it was closing the box on this tag...yes, that is annoying.