这篇文章主要介绍了php微信公众号开发之微信红包实现方法,结合实例形式分析了php实现微信公众号红包发送功能的实现思路、步骤与具体操作技巧,需要的朋友可以参考下
本文实例讲述了php微信公众号开发之微信红包实现方法。分享给大家供大家参考,具体如下:
这几天遇到了一个客户 要给他们的微信公众平台上添加微信现金红包功能,是个二次开发的功能,顺手百度一下,原来不复杂。就着手开发功能了。现将开发的过程和需求贴出来分享一下:
一.需求:
粉丝通过在客户的公众平台点击他们公司的订单,然后给这个订单返现五元,发到订单的这个微信号上。
二.开发想法:
1:先拿到关注这个粉丝的openid,openid是关注某个公众号的微信标识,这样就可以定位到这个人是订单的操作者了。
2:发送xml数据请求微信服务器。
代码有两个php文件
1.oauth2.php
<?php
$code=$_get['code'];
$state=$_get['state'];
$appid='xxxx';
$appsecret='xxxxxxxx';//
if (empty($code)) $this->error('授权失败');
$token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token=json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
echo '<h1>错误1</h1>'.$token->errcode;
echo '<br/><h2>错误信息1:</h2>'.$token->errmsg;
exit;
}
session_start();
$_session['openid']= $token->openid;
header('location:http://www.xxxxxxx.com/xxxxx/xxxxxx/xxxxxx/hongbao.php');//要跳转的文件路径
?>
2.hongbao.php
<?php
//xxxxx。。是需要开发者自己填写的内容,注意不要泄密
// 从session中获取到openid;
$openid=$_session["openid"];
if(empty($openid))
{
header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxx&redirect_uri=http://www.xxxxxxx.com/oauth2.php&respose_type=code&scope=snsapi_base&state=xxxx&connect_redirect=1#wechat_redirect');
}
}
// 关键的函数
public function weixin_red_packet(){
// 请求参数
// 随机字符串
$data['nonce_str']=$this->get_unique_value();
//商户号,输入你的商户号
$data['mch_id']="xxxxxxx";
//商户订单号,可以按要求自己组合28位的商户订单号
$data['mch_billno']=$data['mch_id'].date("ymd")."xxxxxx".rand(1000,9999);
//公众帐号appid,输入自己的公众号appid
$data['wxappid']="xxxxxxx";
//商户名称
$data['send_name']="xxxxx";
//用户openid,输入待发红包的用户openid
session_start();
$data['re_openid']=$_session["openid"];
//付款金额
$data['total_amount']="xxxx";
//红包发放总人数
$data['total_num']="xxxx";
//红包祝福语
$data['wishing']="xxxx";
//ip地址
$data['client_ip']=$_server['local_addr'];
//活动名称
$data['act_name']="xxxxx";
//备注
$data['remark']="xxxxx";
// 生成签名
//对数据数组进行处理
//api密钥,输入自己的k 微信商户号里面的k
$appsecret="xxxxxxxxxxxxxx"; //
$data=array_filter($data);
ksort($data);
$str="";
foreach($data as $k=>$v){
$str.=$k."=".$v."&";
}
$str.="key=".$appsecret;
$data['sign']=strtoupper(md5($str));
/*
发红包操作过程:
1.将请求数据转换成xml
2.发送请求
3.将请求结果转换为数组
4.将请求信息和请求结果录入到数据库中
4.判断是否通信成功
5.判断是否转账成功
*/
//发红包接口地址
$url="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
//将请求数据由数组转换成xml
$xml=$this->arraytoxml($data);
//进行请求操作
$res=$this->curl($xml,$url);
//将请求结果由xml转换成数组
$arr=$this->xmltoarray($res);
}
// 生成32位唯一随机字符串
private function get_unique_value(){
$str=uniqid(mt_rand(),1);
$str=sha1($str);
return md5($str);
}
// 将数组转换成xml
private function arraytoxml($arr){
$xml="<xml>";
foreach($arr as $k=>$v){
$xml.="<".$k.">".$v."</".$k.">";
}
$xml.="</xml>";
return $xml;
}
// 将xml转换成数组
private function xmltoarray($xml){
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring=simplexml_load_string($xml,"simplexmlelement",libxml_nocdata);
$arr=json_decode(json_encode($xmlstring),true);
return $arr;
}
//进行curl操作
private function curl($param="",$url) {
$posturl = $url;
$curlpost = $param;
//初始化curl
$ch = curl_init();
//抓取指定网页
curl_setopt($ch, curlopt_url,$posturl);
//设置header
curl_setopt($ch, curlopt_header, 0);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, curlopt_returntransfer, 1);
//post提交方式
curl_setopt($ch, curlopt_post, 1);
// 增加 http header(头)里的字段
curl_setopt($ch, curlopt_postfields, $curlpost);
// 终止从服务端进行验证
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);
//证书放到网站根目录的cert文件夹底下
curl_setopt($ch,curlopt_sslcert,dirname(__file__).directory_separator.
'cert'.directory_separator.'apiclient_cert.pem');
curl_setopt($ch,curlopt_sslkey,dirname(__file__).directory_separator.
'cert'.directory_separator.'apiient_key.pem');
curl_setopt($ch,curlopt_cainfo,dirname(__file__).directory_separator.
'cert'.directory_separator.'rootca.pem');
//运行curl
$data = curl_exec($ch);
//关闭curl
curl_close($ch);
return $data;
}
?>
相关推荐:js实现微信红包随机算法(附代码)
php用抛物线的模型实现微信红包生成算法的程序源码
php实现微信红包代码
以上就是php实现微信公众号开发之微信红包的方法的详细内容。