web/tweet_ajax.php
author Nicolas Sauret <nicolas.sauret@iri.centrepompidou.fr>
Fri, 17 Jan 2014 17:01:16 +0100
changeset 1051 f44f048d2e60
parent 1050 10feb1ce6d17
child 1427 8b3d57a519eb
permissions -rwxr-xr-x
bpi as current event + museo#3 in archive

<?php

/**
 * include some common code (like we did in the 90s)
 * People still do this? ;)
 */
include_once 'common.php';

/**
 * Check for a POSTed status message to send to Twitter
 */
if (!empty($_POST) && isset($_POST['status'])
&& isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {
    /**
     * Easiest way to use OAuth now that we have an Access Token is to use
     * a preconfigured instance of Zend_Http_Client which automatically
     * signs and encodes all our requests without additional work
     */
    $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
    $client = $token->getHttpClient($configuration);
    $client->setUri('https://api.twitter.com/1.1/statuses/update.json');
    $client->setMethod(Zend_Http_Client::POST);
    $client->setParameterPost('status', $_POST['status']);
    $response = $client->request();

    /**
     * Check if the json response refers to our tweet details (assume it
     * means it was successfully posted). API gurus can correct me.
     */
    $data = json_decode($response->getBody());
    $result = $response->getBody();
    if (isset($data->text)) {
        $result = 'true';
    }
    /**
     * Tweet sent (hopefully), redirect back home...
     */
    //header('Location: ' . URL_ROOT . '?result=' . $result);
	echo($result);
} else {
    /**
     * Mistaken request? Some malfeasant trying something?
     */
    exit('Invalid tweet request. Oops. Sorry.');
}