PHP自动抢微信红包软件送礼如何实现

88必发娱乐城_88必发娱乐(唯一)官网【www.88bifa.com】未经考察的人生是不值得过的,过度考察的人生是没法过的。
php实现微信拼手气红包
$result = sendHB(100, 10);
var_export($result);
echo array_sum($result);
* 拼手气红包实现
* 生成num个随机数,每个随机数占随机数总和的比例*money_total的值即为每个红包的钱额
* 考虑到精度问题,最后重置最大的那个红包的钱额为money_total-其他红包的总额
* 浮点数比较大小,使用number_format,精确到2位小数
* @param double $money_total
总钱额, 每人最少0.01,精确到2位小数
* @param int $num 发送给几个人
* @return array num个元素的一维数组,值是随机钱额
function sendHB($money_total, $num) {
if($money_total & $num*0.01) {
exit('钱太少');
$rand_arr = array();
for($i=0; $i&$ $i++) {
$rand = rand(1, 100);
$rand_arr[] = $
$rand_sum = array_sum($rand_arr);
$rand_money_arr = array();
$rand_money_arr = array_pad($rand_money_arr, $num, 0.01);
//保证每个红包至少0.01
foreach ($rand_arr as $key =& $r) {
$rand_money = number_format($money_total*$r/$rand_sum, 2);
if($rand_money &= 0.01 || number_format(array_sum($rand_money_arr), 2) &= number_format($money_total, 2)) {
$rand_money_arr[$key] = 0.01;
$rand_money_arr[$key] = $rand_
$max_index = $max_rand = 0;
foreach ($rand_money_arr as $key =& $rm) {
if($rm & $max_rand) {
$max_rand = $
$max_index = $
unset($rand_money_arr[$max_index]);
//这里的array_sum($rand_money_arr)一定是小于$money_total的
$rand_money_arr[$max_index] = number_format($money_total - array_sum($rand_money_arr), 2);
ksort($rand_money_arr);
return $rand_money_
没有更多推荐了,PHP版微信公众平台红包API
&更新时间:日 09:16:41 & 作者:youkuiyuan
这篇文章主要介绍了PHP版微信公众平台微信API类,目前主要实现了微信红包接口,陆续会继续进行更新,非常的实用,这里推荐给小伙伴们,有需要的朋友可以参考下。
重写了一下PHP下面的微信API接口,
微信红包支持,JSAPI的动态参数接口支持
微信API类 - 增加红包支持
/********************************************************
@author Kyler You &QQ:&
@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);
/****************************************************
* 微信带证书提交数据 - 微信红包使用
****************************************************/
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);
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
/****************************************************
* 微信获取AccessToken 返回指定微信公众号的at信息
****************************************************/
public function wxAccessToken($appId = NULL , $appSecret = NULL){
= is_null($appId) ? self::appId : $appId;
$appSecret
= is_null($appSecret) ? self::appSecret : $appS
//echo $appId,$appS
= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appS
= $this-&wxHttpsRequest($url);
//print_r($result);
= json_decode($result, true);
$access_token
= $jsoninfo["access_token"];
return $access_
/****************************************************
* 微信通过OPENID获取用户信息,返回数组
****************************************************/
public function wxGetUser($openId){
$wxAccessToken = $this-&wxAccessToken();
= "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";
= $this-&wxHttpsRequest($url);
= json_decode($result, true);
/****************************************************
* 微信通过指定模板信息发送给指定用户,发送完成后返回指定JSON数据
****************************************************/
public function wxSendTemplate($jsonData){
$wxAccessToken = $this-&wxAccessToken();
= "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessT
= $this-&wxHttpsRequest($url,$jsonData);
/****************************************************
发送自定义的模板消息
****************************************************/
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);
/****************************************************
* 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_base //验证时不返回确认页面,只能获取OPENID
****************************************************/
public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){
= 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";
/****************************************************
* 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_userinfo //获取用户完整信息
****************************************************/
public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){
= 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";
/****************************************************
* 微信OAUTH跳转指定URL
****************************************************/
public function wxHeader($url){
header("location:".$url);
/****************************************************
* 微信通过OAUTH返回页面中获取AT信息
****************************************************/
public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){
= is_null($appId) ? self::appId : $appId;
$appSecret
= is_null($appSecret) ? self::appSecret : $appS
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
= $this-&wxHttpsRequest($url);
//print_r($result);
= json_decode($result, true);
//$access_token
= $jsoninfo["access_token"];
/****************************************************
* 微信通过OAUTH的Access_Token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行
****************************************************/
public function wxOauthUser($OauthAT,$openId){
= "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";
= $this-&wxHttpsRequest($url);
= json_decode($result, true);
/*****************************************************
生成随机字符串 - 最长为32位字符串
*****************************************************/
public function wxNonceStr($length = 16, $type = FALSE) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str = "";
for ($i = 0; $i & $ $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
if($type == TRUE){
return strtoupper(md5(time() . $str));
/*******************************************************
微信商户订单号 - 最长28位字符串
*******************************************************/
public function wxMchBillno($mchid = NULL) {
if(is_null($mchid)){
if(self::mchid == "" || is_null(self::mchid)){
$mchid = time();
$mchid = self::
$mchid = substr(addslashes($mchid),0,10);
return date("Ymd",time()).time().$
/*******************************************************
微信格式化数组变成参数格式 - 支持url加密
*******************************************************/
public function wxSetParam($parameters){
if(is_array($parameters) && !empty($parameters)){
$this-&parameters = $
return $this-&
return array();
/*******************************************************
微信格式化数组变成参数格式 - 支持url加密
*******************************************************/
public function wxFormatArray($parameters = NULL, $urlencode = FALSE){
if(is_null($parameters)){
$parameters = $this-&
$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 $//返回字符串
/*******************************************************
微信MD5签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]
*******************************************************/
public function wxMd5Sign($content, $privatekey){
if (is_null($key)) {
throw new Exception("财付通签名key不能为空!");
if (is_null($content)) {
throw new Exception("财付通签名内容不能为空");
$signStr = $content . "&key=" . $
return strtoupper(md5($signStr));
catch (Exception $e)
die($e-&getMessage());
/*******************************************************
微信Sha1签名生成器 - 需要将参数数组转化成为字符串[wxFormatArray方法]
*******************************************************/
public function wxSha1Sign($content, $privatekey){
if (is_null($key)) {
throw new Exception("财付通签名key不能为空!");
if (is_null($content)) {
throw new Exception("财付通签名内容不能为空");
$signStr = $content . "&key=" . $
return strtoupper(sha1($signStr));
catch (Exception $e)
die($e-&getMessage());
/*******************************************************
将数组解析XML - 微信红包接口
*******************************************************/
public function wxArrayToXml($parameters = NULL){
if(is_null($parameters)){
$parameters = $this-&
if(!is_array($parameters) || empty($parameters)){
die("参数不为数组无法解析");
$xml = "&xml&";
foreach ($arr as $key=&$val)
if (is_numeric($val))
$xml.="&".$key."&".$val."&/".$key."&";
$xml.="&".$key."&&![CDATA[".$val."]]&&/".$key."&";
$xml.="&/xml&";
后期还是会增加在一起的把这个CLASS做起来,网上资源很多,但是都是有一定基础的人去看看改改可以,对于没有接触刚刚接触的新手还是需要给予支持的。帮助用户屡屡思路。
以上所述就是本文的全部内容了,希望大家能够喜欢。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具小程序与php 实现微信支付
小编分享视频教程窗口wucy03)为好友,获取视频教程,这里的视频教程随时都会变,主要针对初学者,新手们!更多视频教程请查看小编朋友圈
带你入门的vue2.0视频教程与案例开发
链接:http://pan.baidu.com/s/1bpo60r1
文章来自:博客
http://blog.csdn.net/admin1008611/article/details/
很多友友们都会找小程序与微信支付的教程,今天找了一个近期更新的文章给大家参考参考,视频教程呢其实在 “Thinkphp5与小程序商城” 这门课程有详细的讲解到。有这个视频的大家可以仔细认真的看看。以下是文章,截图不明确的可以查看原文链接。
小程序访问地址:
payfee.php:
WeixinPay.php:
小程序页面处理:
回调url:notify.php
以上几步你就可以完成小程序的微信支付与php完美的结合起来
责任编辑:
声明:该文观点仅代表作者本人,搜狐号系信息发布平台,搜狐仅提供信息存储空间服务。
今日搜狐热点最近一段时间微信小程序向个人开发者开放了申请,于是自己开始学习如何开发小程序,由于对后台开发不熟悉,所以自己碰到了许多坑。
自己碰到的第一个坑就是如何实现与小程序的后台通信,根据微信小程序官方文档的说法,网络通信需要通过wx.request发起网络请求,官方文档的写法是:
wx.request({
url: 'test.php', //仅为示例,并非真实的接口地址
'content-type': 'application/json'
success: function(res) {
console.log(res.data)
但是在实际的操作中是url微信要求必须是https协议的。
下面我以一个例子来完成微信小程序从后台请求数据:
打开微信官方小程序开发IDE,在app.json文件中新建一个页面
"pages/index/index",
"pages/todos/todos"//在这里新建一个页面
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
2.加上上面的一段代码保存后我们就可以看到在pages文件夹下多了一个页面
然后在todos.js中进行数据请求,请求代码如下:
onLoad: function () {
var that =
wx.request({
url: 'https://www.***.***/index.php',//此处填写你后台请求地址
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function (res) {
// success
console.log(res.data);//在控制台输出在远程后台请求到的数据
fail: function (res) {
complete: function (res) {
// complete
3.第三步就是编写后台响应程序来响应微信小程序的前台请求,我的后台使用php写的,后台响应的文件index.php如下:
$data = array(
'tid' =& 100,
'name' =& 'IT学习笔记',
'site' =& 'www.icvo.net');
$response = array(
'code' =& 200,
'message' =& 'success for request',
'data' =& $data,
echo json_encode($response);//生成json文件
4.这时候,我们就可以在控制台看到我们请求的json数据了
这样,一次成功的微信小程序从后台请求数据就成功了。
相关文章推荐}

我要回帖

更多关于 微信抢红包神器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信