1. 程式人生 > >微信公眾號開發:模板訊息實現

微信公眾號開發:模板訊息實現

微信模板訊息,可以在適合的情景下,在公眾號聊天介面彈出一條訊息

// 這裡使用的時發貨資訊模板訊息
function template($order_id,$openid,$type,$postid){
    /**
        $order_id string 當前訂單ID,使用者點選詳情檢視
        $openid   string 將會收到資訊的使用者的openID
        $type     string 快遞公司名稱
        $postid   string 當前快遞單號
    */
    //這裡獲取access_token的值我之前已經有封裝,[https://blog.csdn.net/weixin_42085115/article/details/80375681](https://blog.csdn.net/weixin_42085115/article/details/80375681)
$access_token = getAccessToken(); $info = [ "touser"=>$openid,//要接受此訊息的使用者在當前公眾號的openID "template_id"=>"545VSJPuCLIAzbT2RoGN_Y8CINFAFCJGb7bLVc2ktTw",//模板型別的ID "url"=>"http://www.e.cn/Cart/selectExpress?id=".$order_id,//點選詳情將要調轉的連結地址,我這裡用來看物流詳情 "topcolor"
=>"#FF0000",//訊息框頂部顏色 "data"=>[ //data下的每個鍵名要根據微信文件的需求來,否則不顯示 "first"=> [ "value"=>"hello!".$openid,//這裡可以拼接使用者名稱 "color"=>"#173177" ], "delivername"=>[ "value"
=>$type, "color"=>"#173177" ], "ordername"=>[ "value"=>$postid, "color"=>"#173177" ], "remark"=>[ "value"=>"親,寶貝已經啟程了,好想快點來到你身邊,請注意檢視物流資訊", "color"=>"#000000" ], ], ]; $info = json_encode($info); $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $res = curlPost($url,$info);//這裡呼叫了之前封裝的curl傳送方法(https://mp.csdn.net/mdeditor/80117600) }