Skip to content

Instantly share code, notes, and snippets.

@ety001
Created May 16, 2016 13:24
Show Gist options
  • Select an option

  • Save ety001/d0a24573bb394901028299541583899f to your computer and use it in GitHub Desktop.

Select an option

Save ety001/d0a24573bb394901028299541583899f to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
$tmpl_id = 'PT0X_93niu22Ti3CYqEL0bXPkflJ06zUK5Yt3_KCF_g';
$appid = 'wx54602a12c477c961';
$secret = 'dde1ceb8fb98cb3ec0a70e9f0849221b';
$user_id = 'oUeGNtz41L35y49a_xqXGjWeBazU';
$access_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$tmpl_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
function get_access_token() {
global $access_url;
$cache = file_get_contents(dirname(__FILE__).'/cache.log');
$cache = explode('|', $cache);
$now = time();
if(!$cache || $now >= ($cache[1]+3600)){
$r = file_get_contents($access_url);
$r = json_decode($r, true);
$r = $r['access_token'];
file_put_contents('cache.log', $r.'|'.$now);
} else {
$r = $cache[0];
}
return $r;
}
function get_tmpl_url() {
global $tmpl_url;
return $tmpl_url . get_access_token();
}
function send($content) {
global $user_id, $tmpl_id;
$url = get_tmpl_url();
$json = '{
"touser":"'.$user_id.'",
"template_id":"'.$tmpl_id.'",
"url":"http://www.domyself.me",
"topcolor":"#FF0000",
"data":{
"first": {
"value":"有新消息",
"color":"#173177"
},
"remark":{
"value":"'.$content.'",
"color":"#173177"
}
}
}';
$options = array(
'http' =>
array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($json)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => 'POST',
'content' => $json
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
send($argv[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment