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

PHP微信公众号自动发送红包API

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

本文实例为大家分享了PHP微信公众号自动发送红包API代码,分享给大家供大家参考。具体如下:

贴出核心接口代码至于数据自己填写,接口测试OK
wechat_packet.php

weixin_appid = C('wap_weixin_appid');  $this->mch_id = C('wechat_mch_id');  $this->send_name = C('wechat_send_name');  $this->wishing = C('wechat_wishing');  $this->act_name = C('wechat_act_name');  $this->client_ip = $_SERVER['SERVER_ADDR'];  $this->remark = C('wechat_remark');  $this->nonce_str = $this->create_nonce_str(32);  $this->api_password = C('wechat_api_password');  $inc_file = BASE_PATH.DS.'api'.DS.'wechat'.DS.'arraytoxml.php';  if(is_file($inc_file)) {   require($inc_file);  }  $this->arraytoxml = new ArrayToXML(); }  public function send_post($mch_billno,$re_openid,$total_amount){  $sign = $this->create_sign($mch_billno,$re_openid,$total_amount);  $send_array = array(   'nonce_str' => $this->nonce_str,   'mch_billno' => $mch_billno,   'mch_id' => $this->mch_id,   'wxappid' => $this->weixin_appid,   'send_name' => $this->send_name,   're_openid' => $re_openid,   'total_amount' => $total_amount,   'total_num' => $this->total_num,   'wishing' => $this->wishing,   'client_ip' => $this->client_ip,   'act_name' => $this->act_name,   'remark' => $this->remark,   'sign' => $sign,  );  $send_xml = $this->arraytoxml->toXml($send_array,'');  $data = $this->curl_post_ssl($this->url, $send_xml);  $data = $this->xmlToArray($data);  file_put_contents('adki',var_export($data,true),FILE_APPEND); } /* 请确保您的libcurl版本是否支持双向认证,版本高于7.20.1 */ private function curl_post_ssl($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().$this->public_key);  //默认格式为PEM,可以注释  curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');  curl_setopt($ch,CURLOPT_SSLKEY,getcwd().$this->private_key);  //ca证书  curl_setopt($ch,CURLOPT_CAINFO,$this->rootca);  //第二种方式,两个文件合成一个.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\n\n\n";   curl_close($ch);   return false;  } } //生成签名 private function create_sign($mch_billno,$re_openid,$total_amount){  $string_array = array(   'act_name' => $this->act_name,   'client_ip' => $this->client_ip,   'mch_billno' => $mch_billno,   'mch_id' => $this->mch_id,   'nonce_str' => $this->nonce_str,   're_openid' => $re_openid,   'remark' => $this->remark,   'send_name' => $this->send_name,   'total_amount' => $total_amount,   'total_num' => $this->total_num,   'wishing' => $this->wishing,   'wxappid' => $this->weixin_appid,  );  foreach ($string_array as $key => $value){   if(!empty($value)){    $stringA .= "$key=$value";    if($key != 'wxappid'){     $stringA .= '&';    }   }  }  //转成UTF-8  $stringA = $this->gbkToUtf8($stringA);  $stringSignTemp="$stringA&key=$this->api_password";  $sign = MD5($stringSignTemp);  $sign = strtoupper($sign);  return $sign; } //生成随机字符串 private function create_nonce_str($length){  $str = null;  $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";  $max = strlen($strPol)-1;  for($i=0;$i<$length;$i++){   $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数  }  return $str; } /**  *自动判断把gbk或gb2312编码的字符串转为utf8  *能自动判断输入字符串的编码类,如果本身是utf-8就不用转换,否则就转换为utf-8的字符串  *支持的字符编码类型是:utf-8,gbk,gb2312  *@$str:string 字符串  */ private function gbkToUtf8($str){  $charset = mb_detect_encoding($str,array('ASCII','UTF-8','GBK','GB2312'));  $charset = strtolower($charset);  if("utf-8" != $charset){   $str = iconv('UTF-8',$charset,$str);  }  return $str; } private function xmlToArray($postStr){  $msg = array();  $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  return $msg; }}

数组转xml:arraytoxml.php

';  foreach ($data as $key => $value){   if (is_numeric($value)){    $xml .= "<".$key.">".$value."";   }else{    $xml .= "<".$key.">";   }  }  $xml .= '';  return $xml; }}

以上就是本文的全部内容,希望对大家学习PHP程序设计有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • php微信公众号开发模式详解
  • php 微信公众平台开发模式实现多客服的实例代码
  • php实现发送微信模板消息的方法
  • 关于php微信订阅号开发之token验证后自动发送消息给订阅号但是没有消息返回的问题
  • PHP实现微信模拟登陆并给用户发送消息的方法【文字,图片,图文】
  • php版微信公众平台实现预约提交后发送email的方法
  • 验证token、回复图文\文本、推送消息的实用微信类php代码
  • PHP memcache在微信公众平台的应用方法示例
  • 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交流群

    侯体宗的博客