<?php
class wxservice{ /** * @todo w微信服务号接口地址 */ private static $appid = "---------"; private static $appsecret = "----------------"; private static $template_id = [ "1"=>"==========",//服务完成提醒 "2"=>"============",//派单成功通知 ]; private static $token;public function __construct (){
self::gettoken(); }/**
* @todo 获取access_token */ public static function gettoken(){ if(!file_exists("./upload/token.txt")){ fopen("./upload/token.txt", "w+"); } $file = file_get_contents("./upload/token.txt"); $file = json_decode($file,true); if( $file['date']<time() ){$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::$appid."&secret=".self::$appsecret;
$file = file_get_contents($url); $file = json_decode($file,true); $file['date'] = time() + $file['expires_in'] - 1200; $fp = file_put_contents("./upload/token.txt", json_encode($file)); } self::$token = $file['access_token']; }public static function send_service($openid,$info = [],$type = 1){
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".self::$token; $arr = [ "touser"=>$openid, "template_id"=>self::$template_id[$type], "url"=>"", "miniprogram"=>"", "data"=>[ "first"=>[ "value"=>"{$info['first']}", "color"=>"#173177" ], "keyword1"=>[ "value"=>"{$info['keyword1']}", "color"=>"#173177" ], "keyword2"=>[ "value"=>"{$info['keyword2']}", "color"=>"#173177" ], "keyword3"=>[ "value"=>"{$info['keyword3']}", "color"=>"#173177" ], "remark"=>[ "value"=>"{$info['remark']}", "color"=>"#173177" ], ] ]; $msg = self::postcurl($url,json_encode($arr)); return $msg;}
private static function postcurl($submitUrl,$postData = null)
{ //提交菜单 $curl = curl_init($submitUrl); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证 curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头 curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果 curl_setopt($curl,CURLOPT_POST,true); // post传输数据 curl_setopt($curl,CURLOPT_POSTFIELDS,$postData);// post传输数据 $responseText = curl_exec($curl); if($responseText == false) { throw new IException(curl_error($curl)); } curl_close($curl); return json_decode($responseText,true); }}