Twitter::cachedRequest PHP Method

cachedRequest() public method

Cached HTTP request.
public cachedRequest ( $resource, array $data = NULL, $cacheExpire = NULL ) : stdClass | stdClass[]
$data array
return stdClass | stdClass[]
    public function cachedRequest($resource, array $data = NULL, $cacheExpire = NULL)
    {
        if (!self::$cacheDir) {
            return $this->request($resource, 'GET', $data);
        }
        if ($cacheExpire === NULL) {
            $cacheExpire = self::$cacheExpire;
        }
        $cacheFile = self::$cacheDir . '/twitter.' . md5($resource . json_encode($data) . serialize([$this->consumer, $this->token])) . '.json';
        $cache = @json_decode(@file_get_contents($cacheFile));
        // intentionally @
        $expiration = is_string($cacheExpire) ? strtotime($cacheExpire) - time() : $cacheExpire;
        if ($cache && @filemtime($cacheFile) + $expiration > time()) {
            // intentionally @
            return $cache;
        }
        try {
            $payload = $this->request($resource, 'GET', $data);
            file_put_contents($cacheFile, json_encode($payload));
            return $payload;
        } catch (TwitterException $e) {
            if ($cache) {
                return $cache;
            }
            throw $e;
        }
    }

Usage Example

コード例 #1
0
ファイル: twitter.php プロジェクト: rotten77/xcv
<?php

include dirname(__FILE__) . "/../app/app.php";
$dbTwitter = $db->modul_twitter[WEB_ID];
$twitter_username = trim((string) $dbTwitter['twitter_username']);
if ($twitter_username == "") {
    exit;
}
require_once dirname(__FILE__) . '/twitter-php/twitter.class.php';
$twitter = new Twitter('2bAPmsWQsQENPUPsZkmkg', '6JmH47npRapf7Gy448eF3LLvFPYgN1cZf0S5cYA2Yw', '12213012-49ciz2RcpRpH0JOFECmYSFwpeYQcnt63Y6sAAj3zm', '8lfmu0WVmQSlwmMoNfNvDGV2ZRez6cbIXOF23pgNjS66q');
$statuses = $twitter->cachedRequest("statuses/user_timeline", array("screen_name" => $twitter_username));
// echo '<pre>';
// var_dump($statuses);
$jsonData = array();
$i = 0;
foreach ($statuses as $status) {
    $jsonData[$i] = array("date" => date("d.m.Y, H:i", strtotime($status->created_at)), "user_name" => htmlspecialchars($status->user->screen_name), "status" => str_replace("'", "&#39;", Twitter::clickable($status)), "user_profile_image" => $status->user->profile_image_url, "tweet_url" => 'https://twitter.com/' . $twitter_username . '/status/' . $status->id_str);
    $i++;
}
// print_r($jsonData);
$dbTwitter->update(array("data_json" => json_encode($jsonData)));