侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP版微信公众平台红包API

微信(小程序)  /  管理员 发布于 7年前   184

重写了一下PHP下面的微信API接口,

微信红包支持,JSAPI的动态参数接口支持

http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/wxapi.class.php

微信API类 - 增加红包支持

   *   @link http://mp.weixin.qq.com/wiki/home/index.html   *   @version 2.0.1   *   @uses $wxApi = new WxApi();   *   @package 微信API接口 陆续会继续进行更新   ********************************************************/   class WxApi {    const appId     = "";    const appSecret   = "";     const mchid     = ""; //商户号    const privatekey  = ""; //私钥    public $parameters = array();     public function __construct(){     }     /****************************************************     * 微信提交API方法,返回微信指定JSON     ****************************************************/     public function wxHttpsRequest($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;    }     /****************************************************     * 微信带证书提交数据 - 微信红包使用     ****************************************************/     public function wxHttpsRequestPem($url, $vars, $second=30,$aHeader=array()){        $ch = curl_init();        //超时时间        curl_setopt($ch,CURLOPT_TIMEOUT,$second);        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);        //这里设置代理,如果有的话        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);        curl_setopt($ch,CURLOPT_URL,$url);        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);         //以下两种方式需选择一种         //第一种方法,cert 与 key 分别属于两个.pem文件        //默认格式为PEM,可以注释        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');        curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');        //默认格式为PEM,可以注释        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');        curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');         curl_setopt($ch,CURLOPT_CAINFO,'PEM');        curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');         //第二种方式,两个文件合成一个.pem文件        //curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');         if( count($aHeader) >= 1 ){curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);        }         curl_setopt($ch,CURLOPT_POST, 1);        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);        $data = curl_exec($ch);        if($data){curl_close($ch);return $data;        }        else { $error = curl_errno($ch);echo "call faild, errorCode:$error\n"; curl_close($ch);return false;        }    }     /****************************************************     * 微信获取AccessToken 返回指定微信公众号的at信息     ****************************************************/     public function wxAccessToken($appId = NULL , $appSecret = NULL){        $appId     = is_null($appId) ? self::appId : $appId;        $appSecret   = is_null($appSecret) ? self::appSecret : $appSecret;        //echo $appId,$appSecret;        $url      = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;        $result     = $this->wxHttpsRequest($url);        //print_r($result);        $jsoninfo    = json_decode($result, true);        $access_token  = $jsoninfo["access_token"];        return $access_token;    }     /****************************************************     * 微信通过OPENID获取用户信息,返回数组     ****************************************************/     public function wxGetUser($openId){        $wxAccessToken = $this->wxAccessToken();        $url      = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";        $result     = $this->wxHttpsRequest($url);        $jsoninfo    = json_decode($result, true);        return $jsoninfo;    }     /****************************************************     * 微信通过指定模板信息发送给指定用户,发送完成后返回指定JSON数据     ****************************************************/     public function wxSendTemplate($jsonData){        $wxAccessToken = $this->wxAccessToken();        $url      = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken;        $result     = $this->wxHttpsRequest($url,$jsonData);        return $result;    }     /****************************************************     *   发送自定义的模板消息     ****************************************************/     public function wxSetSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE'){        $template = array('touser' => $touser,'template_id' => $template_id,'url' => $url,'topcolor' => $topcolor,'data' => $data        );        $jsonData = json_encode($template);        $result = $this->wxSendTemplate($jsonData);        return $result;    }     /****************************************************     * 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_base //验证时不返回确认页面,只能获取OPENID     ****************************************************/     public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){        $appId     = is_null($appId) ? self::appId : $appId;        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";        return $url;    }     /****************************************************     * 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_userinfo //获取用户完整信息     ****************************************************/     public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){        $appId     = is_null($appId) ? self::appId : $appId;        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";        return $url;    }     /****************************************************     * 微信OAUTH跳转指定URL     ****************************************************/     public function wxHeader($url){        header("location:".$url);    }     /****************************************************     * 微信通过OAUTH返回页面中获取AT信息     ****************************************************/     public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){        $appId     = is_null($appId) ? self::appId : $appId;        $appSecret   = is_null($appSecret) ? self::appSecret : $appSecret;        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";        $result     = $this->wxHttpsRequest($url);        //print_r($result);        $jsoninfo    = json_decode($result, true);        //$access_token   = $jsoninfo["access_token"];        return $jsoninfo;          }     /****************************************************     * 微信通过OAUTH的Access_Token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行     ****************************************************/     public function wxOauthUser($OauthAT,$openId){        $url      = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";        $result     = $this->wxHttpsRequest($url);        $jsoninfo    = json_decode($result, true);        return $jsoninfo;          }         /*****************************************************     *   生成随机字符串 - 最长为32位字符串     *****************************************************/    public function wxNonceStr($length = 16, $type = FALSE) {      $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";      $str = "";      for ($i = 0; $i < $length; $i++) {       $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);      }      if($type == TRUE){        return strtoupper(md5(time() . $str));      }      else {        return $str;      }    }         /*******************************************************     *   微信商户订单号 - 最长28位字符串     *******************************************************/         public function wxMchBillno($mchid = NULL) {      if(is_null($mchid)){        if(self::mchid == "" || is_null(self::mchid)){          $mchid = time();        }        else{          $mchid = self::mchid;        }      }      else{        $mchid = substr(addslashes($mchid),0,10);      }      return date("Ymd",time()).time().$mchid;    }         /*******************************************************     *   微信格式化数组变成参数格式 - 支持url加密     *******************************************************/public function wxSetParam($parameters){      if(is_array($parameters) && !empty($parameters)){        $this->parameters = $parameters;        return $this->parameters;      }      else{        return array();      }    }         /*******************************************************     *   微信格式化数组变成参数格式 - 支持url加密     *******************************************************/       public function wxFormatArray($parameters = NULL, $urlencode = FALSE){      if(is_null($parameters)){        $parameters = $this->parameters;      }      $restr = "";//初始化空      ksort($parameters);//排序参数      foreach ($parameters as $k => $v){//循环定制参数        if (null != $v && "null" != $v && "sign" != $k) {          if($urlencode){//如果参数需要增加URL加密就增加,不需要则不需要$v = urlencode($v);          }          $restr .= $k . "=" . $v . "&";//返回完整字符串        }      }      if (strlen($restr) > 0) {//如果存在数据则将最后“&”删除        $restr = substr($restr, 0, strlen($restr)-1);      }      return $restr;//返回字符串  }         /*******************************************************     *   微信MD5签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]     *******************************************************/    public function wxMd5Sign($content, $privatekey){    try {        if (is_null($key)) {          throw new Exception("财付通签名key不能为空!");        }        if (is_null($content)) {          throw new Exception("财付通签名内容不能为空");        }        $signStr = $content . "&key=" . $key;        return strtoupper(md5($signStr));      }      catch (Exception $e)      {        die($e->getMessage());      }    }         /*******************************************************     *   微信Sha1签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]     *******************************************************/    public function wxSha1Sign($content, $privatekey){    try {        if (is_null($key)) {          throw new Exception("财付通签名key不能为空!");        }        if (is_null($content)) {          throw new Exception("财付通签名内容不能为空");        }        $signStr = $content . "&key=" . $key;        return strtoupper(sha1($signStr));      }      catch (Exception $e)      {        die($e->getMessage());      }    }     /*******************************************************     *   将数组解析XML - 微信红包接口     *******************************************************/       public function wxArrayToXml($parameters = NULL){      if(is_null($parameters)){        $parameters = $this->parameters;      } if(!is_array($parameters) || empty($parameters)){        die("参数不为数组无法解析");      } $xml = "";      foreach ($arr as $key=>$val)      {        if (is_numeric($val))        {          $xml.="<".$key.">".$val."";         }        else          $xml.="<".$key.">";       }      $xml.="";      return $xml;     }       }

后期还是会增加在一起的把这个CLASS做起来,网上资源很多,但是都是有一定基础的人去看看改改可以,对于没有接触刚刚接触的新手还是需要给予支持的。帮助用户屡屡思路。

以上所述就是本文的全部内容了,希望大家能够喜欢。

您可能感兴趣的文章:

  • 使用PHP实现微信摇一摇周边红包
  • php实现微信发红包
  • php版微信公众号接口实现发红包的方法
  • php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
  • PHP实现微信发红包程序
  • PHP微信红包API接口
  • php实现的微信红包算法分析(非官方)
  • php版微信发红包接口用法示例
  • PHP微信红包生成代码分享
  • php实现微信发红包功能


  • 上一条:
    基于php的微信公众平台开发入门实例
    下一条:
    php微信公众平台开发类实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客