1.登录公众号,新建消息推送模版
2.打开微信官方文档 ->找到模版消息接口 https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
3. 代码
/** * User: xxg * Date: 2023/10/27 11:58 * @Notes:数据处理 */ public function getSen($customerId,$phone,$data,$djbh,$tablename){ $custome = $this->customers($customerId); if ($custome['code'] == 500){ return ["code" => '500' , "msg" => "客户信息未查到"]; } # 通过客户ID查询客户下用户 $user_info = AdminWechat::where("phone",$phone)->first()->toArray(); if (empty($user_info)){ return ["code" => '500',"msg" => '此客户未关注注册公众号']; } $dataArr = array( "thing1" => ["value" => $data->KHMC], //公司门店 "thing2" => ["value" => '配货通知'], //单据类型 "character_string3" => ["value" => $data->DJBH], //订单编号 "thing4" => ["value" => $data->SL], //单据数量 "amount5" => ["value" => $data->JE], //订单金额 ); return $this->sendWechatMessage($user_info['openid'],$custome,$dataArr,$djbh,$tablename); } /** * User: xxg * Date: 2023/10/27 11:54 * @Notes:微信消息发送 */ public function sendWechatMessage($openid,$custome,$data) { $dataArr= array( "touser" => $openid, //openid "template_id" => $custome['template_id'], //模板id "url" => "", "miniprogram" => array( "appid" => env('WECHAT_MINI_PROGRAM_APPID', ''), //小程序APPID "pagepath" => 'pages_gzh/index/formgzh', ), "client_msg_id" => $openid.rand(1000000,999999), "data" => $data, ); //下单用户发送 $access_token = $this->access_token($custome); if ($access_token == 400){ return ['code'=>0,'message'=>'获取access_token失败']; } $urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $result = $this->http_post_json($urls,$dataArr); //发送请求 if($result['errcode'] == 0){ return ['code'=>200,'data'=>'推送成功']; } return ['code'=>0,'message'=>$result[1]['errmsg']]; } public function http_post_json($send_template_url,$template)//接受消息的用户openid,发送的消息,点击详情跳转的url { $data=json_encode($template); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $send_template_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); curl_close($ch); return json_decode($res,true);; } /** * User: xxg * Date: 2023/10/27 11:48 * @Notes:获取access_token * $custome 客户配置项 */ public function access_token($custome){ $appId = $custome['app_id']; $appSecret = $custome['secret']; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 $data = json_decode(curl_exec($ch),true); if(curl_errno($ch)){ var_dump(curl_error($ch)); //若错误打印错误信息 } curl_close($ch);//关闭curl return $data['access_token']; } /** * User: xxg * Date: 2023/10/27 11:48 * @Notes:查询客户 */ public function customers($id){ if (empty($id)){ return ["code" => '',"msg" => '']; } $row = Customer::where("id",$id)->first()->toArray(); if (empty($row)){ return ["code" => '500',"msg" => '']; } return [ "code" => '200', "app_id" => $row['AppID'], "secret" => $row['AppSecret'], "template_id" => $row['template_id'] ]; }
4.展示效果
标签:ch,return,模版,推送,curl,php,data,id,CURLOPT From: https://www.cnblogs.com/ixiangang06/p/17797048.html