将wp函数挂钩到cron

时间:2016-10-25 11:04:22

标签: php wordpress

我创建了以下函数来访问twitter api并检索最新的推文。它工作得很好,我可以在我的网站的页脚中显示最新的推文,但不是发生在每个页面加载我想设置一个cron作业,每小时获取最新的推文。

我之前没有使用过wp cron,所以想知道是否有人可以告诉我如何最好地做到这一点?

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;

}

2 个答案:

答案 0 :(得分:0)

你必须安排每小时的cron而不是必须在数据库中保存推文。获得推文的Cron可能是这样的。

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 
}

然后在页脚中显示数据库表中的推文。

答案 1 :(得分:0)

我将使用WordPress瞬态API而不是这类工作的cron。

瞬态将与àcron相同但不同。您可以设置过期,数据存储在选项表中,如果瞬态名称不存在或过期已结束,则该函数将使用您需要的数据创建瞬态。