1. 程式人生 > >php 小程式傳送模板訊息

php 小程式傳送模板訊息

public function senMsg(){
	$ACCESS_TOKEN = $this->getWxAccessToken();
	        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ACCESS_TOKEN['access_token'];
	        $post_data = array(
	            'touser' =>'',//接收者openid
	            'template_id' => '',//模板id
	            'page' => '',
	            'form_id' => '',//form_id
	            'data' => array(
	            	'keyword1' => array(
		            	'value' => '', 
	            	),
	            	'keyword2' => array(
	                    'value' => '=',
	                ),
	                'keyword3' => array(
	                    'value' => '',
	                ),
	                'keyword4' => array(
	                    'value' => date('Y-m-d H:i:s',$luck_time),
	                ),
	                'keyword5' => array(
	                    'value' => date('Y-m-d H:i:s',$luck_time+12*3600),
	                ),
	                'keyword6' => array(
	                    'value' => '=',
	                )
	            )//模板資料
	            // "emphasis_keyword": "keyword1.DATA" //放大顯示
	        );   
	        $post_data = json_encode($post_data);//將資料轉json
	        $result = $this->curl_post($url,$post_data);//傳送post請求
}
public function getWxAccessToken() {//獲取AccessToken
    $appid = '';
    $appsecret = '';
    if($_SESSION['access_token_'.$appid] && $_SESSION['expire_time_'.$appid]>time()){
        return $_SESSION['access_token_'.$appid];
    }else{
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        $access_token = $this->makeRequest($url);
        $access_token = json_decode($access_token['result'],true);
        $_SESSION['access_token_'.$appid][$access_token];
        $_SESSION['expire_time_'.$appid][time()+7000];
        return $access_token;
    }
}

public function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '') {
    if (empty($url)) {
        return array('code' => '100');
    }

    $_curl = curl_init();
    $_header = array(
        'Accept-Language: zh-CN',
        'Connection: Keep-Alive',
        'Cache-Control: no-cache'
    );
    // 方便直接訪問要設定host的地址
    if (!empty($hostIp)) {
        $urlInfo = parse_url($url);
        if (empty($urlInfo['host'])) {
            $urlInfo['host'] = substr(DOMAIN, 7, -1);
            $url = "http://{$hostIp}{$url}";
        } else {
            $url = str_replace($urlInfo['host'], $hostIp, $url);
        }
        $_header[] = "Host: {$urlInfo['host']}";
    }
    // 只要第二個引數傳了值之後,就是POST的
    if (!empty($params)) {
        curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($_curl, CURLOPT_POST, true);
    }
    if (substr($url, 0, 8) == 'https://') {
        curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($_curl, CURLOPT_URL, $url);
    curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
    curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);
    if ($expire > 0) {
        curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 處理超時時間
        curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立連線超時時間
    }
    // 額外的配置
    if (!empty($extend)) {
        curl_setopt_array($_curl, $extend);
    }
    $result['result'] = curl_exec($_curl);
    $result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
    $result['info'] = curl_getinfo($_curl);
    if ($result['result'] === false) {
        $result['result'] = curl_error($_curl);
        $result['code'] = -curl_errno($_curl);
    }
    curl_close($_curl);
    return $result;
}
 public function curl_post($url, $fields, $data_type='text') {//發起post請求
    $cl = curl_init();
    if(stripos($url, 'https://') !== FALSE) {
        curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($cl, CURLOPT_SSLVERSION, 1);
    }
    curl_setopt($cl, CURLOPT_URL, $url);
    curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($cl, CURLOPT_POST, true);        
    curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
    $content = curl_exec($cl);
    $status = curl_getinfo($cl);
    curl_close($cl);
    if (isset($status['http_code']) && $status['http_code'] == 200) {
        if ($data_type == 'json') {
            $content = json_decode($content);
        }
        return $content;
    } else {
        return FALSE;
    }
}