💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 发送模板API例子 在给用户发送模板之前,先得获得用户Openid(用户唯一标识) 要获得用户Openid,要用户授权。 所以编写了一个类GetOpenid.class.php来获取Openid,具体见上一章。 * * * * * ~~~ <?php /** * 微信模板消息设置模板. * User: wss * Date: 2016/8/10 * Time: 14:46 * @author 大魔仙 */ $appid = "";//微信公众平台的标识,可在微信平台账号中获取 $appsecret = "";可在微信平台账号中获取 //调用类 require_once "GetOpenid.class.php"; //类实例化。 $GetOpenid = new GetOpenid(); /*/** * 判断是否授权过 * * @Author: 大魔仙 * @Date: 2016/6/15 */ 获取用户权限链接: //https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=(*你的回调URL)*&response_type=code&scope=snsapi_userinfo&state=111#wechat_redirect //第一步:用户同意授权,获取code $code = $GetOpenid->getCode(); //第二步:通过code换取网页授权access_token $data = $GetOpenid->getAccessToken($code); //$accessToken = $data->access_token; $openid = $data->openid; //三、用appid和appsecert获得access token 接口 /*$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $output = https_request($url); //用于微信接口数据传输的万能函数 $jsoninfo = json_decode($output, true); //生成PHP关联数组 $access_token = $jsoninfo["access_token"]; var_dump($jsoninfo);*/ //因为获得access_token是有次数限制的,(具体限制可靠查看官方接口权限),所以最好是在获取后保存下,一般2小时后失效。 //$access_token="DPkXmi-iVpp4z6e0YeN-QaeSn8prwWXdTfnoDIJ2E218j4-d_h-HepBuSnW3E1SpqI4uKACbTr9tRNUwsgDVe4WQ5Wlw0NI57eQGcgmyeLkUWPeACAKMF"; //四、定义发送信息模板 $date = array( "touser" =>$openid,//接收者openid "template_id"=>"RQNOdQ7MMMzbyS-m3E5a9w8qNWZl857-d2i76EAwEGI",//模板ID "url"=>"http://admin.nextdog.cc/AAAA/wushanshan/HelloWord.php",//模板跳转链接 "data"=>array( "first"=> array( "value"=>urlencode("欢迎来到大魔仙模板测试!"), "color"=>"#173177" ), "remark"=>array( "value"=>urlencode("\\n欢迎再次购买!(hello world!)"), "color"=>"#173177" ) //因为我定义了行业类型就局限性,只有这些内容 ); $post_send=json_encode($date); $url2="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$access_token"; $output = https_request($url2,$post_send); //调用微信接口数据传输的万能函数 $jsoninfo = json_decode($output, true); //生成PHP关联数组 var_dump($jsoninfo); function https_request($url,$data = null) { //函数定义,数据传输万能方法 $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); curl_close($curl); return $output; } ~~~