微信小程序模板消息推送
function getAccessToken () {
$appid = '';
$appsecret = '';
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$html = file_get_contents($url);
$output = json_decode($html, true);
$access_token = $output['access_token'];
return $access_token;
}
function send_post($post_data){
$post_data = json_encode($post_data, true);
$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$this->getAccessToken();
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/json',
'content' => $post_data,
'timeout' => 60
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}
public function send()
{
$post_faqi = array(
"touser" => '',
"template_id" => '',
"page" => '',
"form_id" => '',
"data" => array(
'keyword1' => array("value" => '恭喜你找到一个客户,赶紧联系TA吧', "color" => "#4a4a4a"),
'keyword2' => array("value" => '', "color" => "#9b9b9b"),
'keyword3' => array("value" => '', "color" => "#9b9b9b"),
),
);
$this->send_post($post_faqi);
}