web/tweet.php
changeset 99 6cb4d10f0b8b
parent 0 4eba9c11703f
child 229 74c9ddc3640b
equal deleted inserted replaced
97:861cae17abda 99:6cb4d10f0b8b
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * include some common code (like we did in the 90s)
       
     5  * People still do this? ;)
       
     6  */
       
     7 include_once './common.php';
       
     8 
       
     9 /**
       
    10  * Check for a POSTed status message to send to Twitter
       
    11  */
       
    12 if (!empty($_POST) && isset($_POST['status'])
       
    13 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {
       
    14     /**
       
    15      * Easiest way to use OAuth now that we have an Access Token is to use
       
    16      * a preconfigured instance of Zend_Http_Client which automatically
       
    17      * signs and encodes all our requests without additional work
       
    18      */
       
    19     $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
       
    20     $client = $token->getHttpClient($configuration);
       
    21     $client->setUri('http://twitter.com/statuses/update.json');
       
    22     $client->setMethod(Zend_Http_Client::POST);
       
    23     $client->setParameterPost('status', $_POST['status']);
       
    24     $response = $client->request();
       
    25 
       
    26     /**
       
    27      * Check if the json response refers to our tweet details (assume it
       
    28      * means it was successfully posted). API gurus can correct me.
       
    29      */
       
    30     $data = json_decode($response->getBody());
       
    31     $result = $response->getBody();
       
    32     if (isset($data->text)) {
       
    33         $result = 'true';
       
    34     }
       
    35     /**
       
    36      * Tweet sent (hopefully), redirect back home...
       
    37      */
       
    38     header('Location: ' . URL_ROOT . '?result=' . $result);
       
    39 } else {
       
    40     /**
       
    41      * Mistaken request? Some malfeasant trying something?
       
    42      */
       
    43     exit('Invalid tweet request. Oops. Sorry.');
       
    44 }