阿里云全站CDN – PHP代码

/ 2评 / 0

因为还是用WP的多,还是发WP代码吧,EMLOG用户只需要修改钩子就可以了.
Update:

据说WP有no-cache头,导致CDN失效,具体应该加个mod_cache做apache覆盖就可以了吧.纯瞎猜.

这个文件放到function.php的结尾,自动加载.

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: TaterLi <admin@lijingquan.net>                              |
// +----------------------------------------------------------------------+
//
// $Id:$
include (get_stylesheet_directory() . '/../../../aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php');
useCdnRequestV20141111 as Cdn;
//更新或发布文章清理本文和首页CDN缓存
add_action('publish_post', 'AliYun_TaterLi_Cache_Publish', 0);
//提交评论更新本文CDN缓存
add_action('comment_post', 'AliYun_TaterLi_Cache_Comments', 0);
//评论被审核更新本文CDN缓存
add_action('comment_unapproved_to_approved', 'AliYun_TaterLi_Cache_Approved', 0);
//发布文章更新CDN缓存函数
function AliYun_TaterLi_Cache_Publish($post_ID) {
    global $secretKey, $secretId;
    $url = get_permalink($post_ID);
    $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "xxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new CdnRefreshObjectCachesRequest();
    $request->setMethod("GET");
    $request->setObjectPath($url);
    $response = $client->getAcsResponse($request);
}
//提交评论清理文章CDN函数
function AliYun_TaterLi_Cache_Comments($comment_id) {
    global $secretKey, $secretId;
    $comment = get_comment($comment_id);
    $url = get_permalink($comment->comment_post_ID);
    $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "xxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new CdnRefreshObjectCachesRequest();
    $request->setMethod("GET");
    $request->setObjectPath($url);
    $response = $client->getAcsResponse($request);
}
//评论被审核清理CDN缓存函数
function AliYun_TaterLi_Cache_Approved($comment) {
    global $secretKey, $secretId;
    $url = get_permalink($comment->comment_post_ID);
    $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "xxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    $client = new DefaultAcsClient($iClientProfile);
    $request = new CdnRefreshObjectCachesRequest();
    $request->setMethod("GET");
    $request->setObjectPath($url);
    $response = $client->getAcsResponse($request);
}
?>

 

  1. dsf说道:

    阿里云的CDN速度怎样啊?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注