I've created the following functions to access the twitter api and retrieve the latest tweet. It works just fine and I can display the latest tweet in the footer of my site but rather than happening on every page load I'd like to set up a cron job to fetch the latest tweet every hour.
I've not used the wp cron before so wondered if anyone could advise me on how best to do this?
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value)
$values[] = "$key=\"" . rawurlencode($value) . "\"";
$r .= implode(', ', $values);
return $r;
}
function hs_tweets_fetch($screen_name = 'XXXXXXXXXXX', $count = 1) {
$config = array(
'oauth_access_token' => 'XXXXXXXXXXXXXX',
'oauth_access_token_secret' => 'XXXXXXXXXXXXXXX',
'consumer_key' => 'XXXXXXXXXXXXXXXXXXXXXX',
'consumer_secret' => 'XXXXXXXXXXXXXXXX',
'base_url' => 'https://api.twitter.com/1.1/'
);
$url = 'statuses/user_timeline.json?screen_name=' . $screen_name . '&count=' . $count;
$url_parts = parse_url($url);
parse_str($url_parts['query'], $url_arguments);
$full_url = $config['base_url'].$url; // Url with the query on it.
$base_url = $config['base_url'].$url_parts['path']; // Url without the query.
$oauth = array(
'oauth_consumer_key' => $config['consumer_key'],
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $config['oauth_access_token'],
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($base_url, 'GET', array_merge($oauth, $url_arguments));
$composite_key = rawurlencode($config['consumer_secret']) . '&' . rawurlencode($config['oauth_access_token_secret']);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(
buildAuthorizationHeader($oauth),
'Expect:'
);
$options = array(
CURLOPT_HTTPHEADER => $header,
//CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HEADER => false,
CURLOPT_URL => $full_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$result = curl_exec($feed);
$info = curl_getinfo($feed);
curl_close($feed);
if ($info['http_code'] == 200) {
$result = json_decode($result);
} else {
$result = false;
}
return $result;
}
You have to schedule hourly cron and than have to save tweets in your db. Cron to get tweets could be something like this.
if (!wp_next_scheduled('hs_tweets_fetch_cron'))
{
wp_schedule_event(time(), 'hourly', 'hs_tweets_fetch_cron');
}
add_action('hs_tweets_fetch_cron', 'hs_tweets_fetch');
function hs_tweets_fetch_cron()
{
1- // fetch the tweets
2- // Save/update the tweets in Wordpress options meta or custom table
}
And than display the tweets from database table in footer.
Instead of a cron for this type of work, I'll use the WordPress transient api.
Transient will make the same than à cron but differently. You can set an expiration and datas are store in the options table, If the transient name doesn't exists or if the expiration is over, then the function will create the transient with the data you need.