微信小程序通知(公众号)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | //调用 $this ->accessToken( $code , $title , $amount , date ( 'Y-m-d H:i:s' ,time())); /* *获取access_token *$code 前端传过来 *$title = 标题 *$amount = 金额 *$time = 时间 */ protected function accessToken( $code = '' , $title = '' , $amount = '' , $time = '' ){ $appid = '****' ; $secret = '****' ; // 设置请求的 URL $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret" ; // 初始化 cURL $ch = curl_init(); // 设置 cURL 选项 curl_setopt( $ch , CURLOPT_URL, $url ); curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true); // 执行请求并获取响应 $response = curl_exec( $ch ); // 检查是否有错误 if (curl_errno( $ch )) { return false; } // 关闭 cURL curl_close( $ch ); $response = json_decode( $response ,true); if (!isset( $response [ 'access_token' ])) return false; //获取access_token $accessToken = $response [ 'access_token' ]; //获取用户的openId $userOpenId = $this ->userOpenId( $appid , $secret , $code ); if (! $userOpenId ) return false; //结构体 (根据公众平台选择推送模版自行配置) $info = (object) array ( 'touser' => $userOpenId [ 'openid' ], 'template_id' => 'SXo-qjIdtIPbD-P1w6wZ-BMJkGv0TIveHrJkbw_3Z3Q' , 'page' => '/pages/order/index' , 'data' => array ( 'thing7' =>[ 'value' => $title ], 'amount4' =>[ 'value' => $amount ], 'time5' =>[ 'value' => $time ], ), ); //发送订阅消息 return $this ->subscribe( $info , $accessToken ); } //获取用户的openId protected function userOpenId( $appid , $secret , $code ) { $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code" ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, FALSE); if (! empty ( $data )) { curl_setopt( $curl , CURLOPT_POST, 1); curl_setopt( $curl , CURLOPT_POSTFIELDS, $data ); } curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); $output = curl_exec( $curl ); if (curl_errno( $curl )) { return false; } curl_close( $curl ); $data = json_decode( $output , true); if (isset( $data [ 'openid' ])) { return $data ; } return false; } //发送订阅消息 protected function subscribe( $data , $access_token ){ $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt( $curl , CURLOPT_SSL_VERIFYHOST, FALSE); if (! empty ( $data )) { curl_setopt( $curl , CURLOPT_POST, 1); curl_setopt( $curl , CURLOPT_POSTFIELDS, json_encode( $data )); } curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); $output = curl_exec( $curl ); if (curl_errno( $curl )) { return false; } curl_close( $curl ); $info = json_decode( $output , true); if ( $info [ 'errcode' ] == '0' ) { return true; } return false; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】