web/search_tweets.php
changeset 1478 adb28b75f2c7
parent 1476 b5098fdacc60
child 1557 7c67caaafdeb
equal deleted inserted replaced
1477:bdf75c8cff6b 1478:adb28b75f2c7
     7 }
     7 }
     8 
     8 
     9 
     9 
    10 include_once 'common.php';
    10 include_once 'common.php';
    11 use Abraham\TwitterOAuth\TwitterOAuth;
    11 use Abraham\TwitterOAuth\TwitterOAuth;
       
    12 use Stash\Pool;
       
    13 use Stash\Driver\Sqlite;
       
    14 
       
    15 $cacheDriver = new Stash\Driver\Sqlite(array('path' => sys_get_temp_dir()."/polemictweet_cache.db"));
       
    16 $cachePool = new Stash\Pool($cacheDriver);
    12 
    17 
    13 /**
    18 /**
    14  * Check for a POSTed status message to send to Twitter
    19  * Check for a POSTed status message to send to Twitter
    15  */
    20  */
    16 if (!empty($_GET)
    21 if (!empty($_GET)
    17 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {
    22 && isset($_SESSION['TWITTER_ACCESS_TOKEN'])) {
    18     /**
       
    19      * Easiest way to use OAuth now that we have an Access Token is to use
       
    20      * a preconfigured instance of Zend_Http_Client which automatically
       
    21      * signs and encodes all our requests without additional work
       
    22      */
       
    23      
       
    24     // if (isset($_GET['endpoint'])) {
       
    25     // 	$endpoint = $_GET['endpoint'];
       
    26 	// 	unset($_GET['endpoint']);
       
    27     // } else {
       
    28     // 	$endpoint = "search/tweets";
       
    29     // }
       
    30 
    23 
    31     $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
    24     $itemCachePath="search/tweets?" . http_build_query($_GET);
    32     $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']);
       
    33 
    25 
       
    26     $cachedStatusesResp = $cachePool->getItem($itemCachePath);
    34 
    27 
    35     // $statuses = $connection->get("search/tweets", $parameters);
    28     $statusesStr = $cachedStatusesResp->get();
    36     $statuses = $connection->get("search/tweets", $_GET);
       
    37     // $client = $token->getHttpClient($configuration);
       
    38     // $client->setUri("https://api.twitter.com/1.1/$endpoint.json");
       
    39     // $client->setMethod(Zend_Http_Client::GET);
       
    40     // $client->setParameterGet($_GET);
       
    41     // $response = $client->request();
       
    42 
    29 
    43     // echo $response->getBody();
    30     if ($cachedStatusesResp->isMiss()) {
    44     echo json_encode($statuses);
    31         $cachedStatusesResp->lock();
       
    32         
       
    33         $token = unserialize($_SESSION['TWITTER_ACCESS_TOKEN']);
       
    34         $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token['oauth_token'], $token['oauth_token_secret']);
       
    35     
       
    36     
       
    37         $statuses = $connection->get("search/tweets", $_GET);
       
    38         $statusesStr = json_encode($statuses);
       
    39 
       
    40         $cachedStatusesResp->set($statusesStr)->expiresAfter(3);
       
    41         $cachePool->save($cachedStatusesResp);
       
    42     }
       
    43 
       
    44     echo($statusesStr);
    45 
    45 
    46 } else {
    46 } else {
    47     /**
    47     /**
    48      * Mistaken request? Some malfeasant trying something?
    48      * Mistaken request? Some malfeasant trying something?
    49      */
    49      */