直接上代码:
$token=$msg['access_token']; //当前发送模板的token值 $openid=$msg['openid']; //接收消息的用户openid $template_id=$msg['template_id']; //所下发的摸版消息的id $page=$msg['page']; //摸版的跳转地址url链接 $form_id=$msg['form_id']; //表单的提交id $data_arr = array( 'keyword1' => array( "value" => $value1), //模板消息的值 'keyword2' => array( "value" => $value2), 'keyword3' => array( "value" => $value3), 'keyword4' => array( "value" => $value4), //这里根据你的模板对应的关键字建立数组 ); $post_data = array ( "touser" => $openid, //用户的 openID,可用过 wx.getUserInfo 获取 "template_id" => $template_id, //小程序后台申请到的模板编号 "page" => $page, //点击模板消息后跳转到的页面,可以传递参数 "form_id" => $form_id, //第一步里获取到的 formID "data" => $data_arr, "emphasis_keyword" => "keyword2.DATA" //需要强调的关键字,会加大居中显示 ); function send_post( $url, $post_data ) { $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/json', //header 需要设置为 JSON 'content' => $post_data, 'timeout' => 60 //超时时间 ) ); $context = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); return $result; } $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$token; //这里替换为你的 appID 和 appSecret $data = json_encode($post_data, true); //将数组编码为 JSON $return = send_post( $url, $data); return $return; //这里的返回值是一个 JSON,可通过 json_decode() 解码成数组