php實現微信小程序消息通知

接入消息通知指引地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/callback_help.html

文檔地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#%E6%A8%A1%E7%89%88%E6%B6%88%E6%81%AF%E7%AE%A1%E7%90%86

看完這兩個地址基本上你就明白是怎麼實現消息通知的了,下面就是需要根據自己的業務需求寫php代碼了

php中業務分爲以下幾個步驟:

1、小程序後臺消息模板設置獲取模板ID

2、微信公衆平臺|小程序->設置->開發設置 獲取AppID(小程序ID)、AppSecret(小程序密鑰   注:重置後導致之前的失效)

3、通過AppID、AppSecret調用接口生成ACCESS_TOKEN

4、獲取form_id

5、發送模板消息

 

下面是實現上面步驟的詳細過程:

一、獲取模板ID

二: 獲取AppID(小程序ID)、AppSecret(小程序密鑰   注:重置後導致之前的失效)

三、生成ACCESS_TOKEN

接口地址:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET


代碼實現:

public function getAccessToken(Request $r)
{
    $appId = Input::get('appId',NULL);
    $appSecret = Input::get('appSecret',NULL);

    $r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret"); //返回的是字符串,需要用json_decode轉換成數組
    $data = json_decode($r,true);

    return $data['access_token'];

}

四、獲取form_id

需要在小程序上做個form表單提交,可以前端生成傳到後臺,就可以獲取到了

注:
頁面的 <form/> 組件,屬性report-submit爲true時,可以聲明爲需發模板消息,此時點擊按鈕提交表單可以獲取formId,用於發送模板消息(多個地方生成form_id傳給後端)。
form_id的長度:Android是13位時間戳、iOS是32位GUID
form_id其實就是前端負責獲取,傳給後端,後端將form_id存起來,在業務中用到消息通知的時候從表裏面取出來

form_id中需要注意的一點:一個form_id只能用一次,所以在建表的時候需要給個status區分已使用和未使用的狀態(這個坑已經進去過,發送完模板不修改status值,會使得消息通知偶爾成功,偶爾失敗)

五、發送模板消息

上面需要的參數都準備好了,OK,這裏自己封裝了一個方法。然後在用到的地方調用的
封裝的方法如下:
public function sendMessage()
{
    $token = $this->getToken();
    $post = [];
    $post['touser'] = '用戶openId';
    $post['page'] = 'index';
    $post['emphasis_keyword'] = 'keyword1.DATA';
    $post['color'] = '#173177';

    $post['template_id'] = '模板id';
    $post['form_id'] = 'formId';
    $post['data'] = [
        'keyword1'=>['value'=>'xxxxxx','color'=>'#173177'],
        'keyword2'=>['value'=>'2018-03-06 14:22:34','color'=>'#173177'],
        'keyword3'=>['value'=>'xxxxxx','color'=>'#173177']
    ];

    $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token;
    $re = $this->curl_url($url,$post);
    return $re;
}

private function curl_url($url, $json)
{
    $body = json_encode($json);
    $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;

}

調用:
public function send(Request $r)
{
    $wechat = new WeChatApi();
    $re = $wechat->sendMessage();
    return $re;

}



到這裏就可以實現消息通知了

原文地址:https://www.cnblogs.com/a-record/p/8521138.html

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章