1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| $secretKey = '填写你的密钥'; $secretId = '填写你的密钥';
add_action('publish_post', 'Clean_By_Publish', 0);
add_action('comment_post', 'Clean_By_Comments',0);
add_action('comment_unapproved_to_approved', 'Clean_By_Approved',0);
function Clean_By_Publish($post_ID){ global $secretKey,$secretId; $url = get_permalink($post_ID); $action='RefreshCdnUrl';
$PRIVATE_PARAMS = array( 'urls.0' => home_url(), 'urls.1' => $url , ); $HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array( 'Nonce' => rand(), 'Timestamp' =>time(NULL), 'Action' =>$action, 'SecretId' => $secretId, );
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps); }
function Clean_By_Comments($comment_id) { global $secretKey,$secretId; $comment = get_comment($comment_id); $url = get_permalink($comment->comment_post_ID); $action='RefreshCdnUrl';
$PRIVATE_PARAMS = array( 'urls.0' => $url, ); $HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array( 'Nonce' => rand(), 'Timestamp' =>time(NULL), 'Action' =>$action, 'SecretId' => $secretId, );
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps); }
function Clean_By_Approved($comment) { global $secretKey,$secretId; $url = get_permalink($comment->comment_post_ID); $action='RefreshCdnUrl';
$PRIVATE_PARAMS = array( 'urls.0' => $url, ); $HttpUrl="cdn.api.qcloud.com";
$HttpMethod="POST";
$isHttps =true;
$COMMON_PARAMS = array( 'Nonce' => rand(), 'Timestamp' =>time(NULL), 'Action' =>$action, 'SecretId' => $secretId, );
CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps); }
function CreateRequest($HttpUrl,$HttpMethod,$COMMON_PARAMS,$secretKey, $PRIVATE_PARAMS, $isHttps) { $FullHttpUrl = $HttpUrl."/v2/index.php";
$ReqParaArray = array_merge($COMMON_PARAMS, $PRIVATE_PARAMS); ksort($ReqParaArray);
$SigTxt = $HttpMethod.$FullHttpUrl."?"; $isFirst = true; foreach ($ReqParaArray as $key => $value) { if (!$isFirst) { $SigTxt = $SigTxt."&"; } $isFirst= false;
if(strpos($key, '_')) { $key = str_replace('_', '.', $key); } $SigTxt=$SigTxt.$key."=".$value; }
$Signature = base64_encode(hash_hmac('sha1', $SigTxt, $secretKey, true));
$Req = "Signature=".urlencode($Signature); foreach ($ReqParaArray as $key => $value) { $Req=$Req."&".$key."=".urlencode($value); }
if($HttpMethod === 'GET') { if($isHttps === true) { $Req="https://".$FullHttpUrl."?".$Req; } else { $Req="http://".$FullHttpUrl."?".$Req; } $Rsp = file_get_contents($Req); } else { if($isHttps === true) { $Rsp= SendPost("https://".$FullHttpUrl,$Req,$isHttps); } else { $Rsp= SendPost("http://".$FullHttpUrl,$Req,$isHttps); } }
return json_decode($Rsp,true); } function SendPost($FullHttpUrl, $Req, $isHttps) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $Req); curl_setopt($ch, CURLOPT_URL, $FullHttpUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1 ); if ($isHttps === true) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $result = curl_exec($ch); return $result; }
|