微信发红包
<?php //返回随机数函数 function getNonceNum($numLen=16){ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $numLen; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } $nonce_str = getNonceNum(); $appid = "###"; $mch_id = "####"; $body="test"; $ip="###"; $mch_billno = date("Ymd").getNonceNum(); $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; $param['nonce_str'] = $nonce_str; $param['mch_billno'] = $mch_billno; $param['mch_id'] = $mch_id; $param['wxappid'] = $appid; $param['send_name'] = "红包测试"; $param['re_openid'] = "####"; $param['total_amount'] = 100; $param['total_num'] = 1; $param['wishing'] = "红包祝福语"; $param['client_ip'] = $ip; $param['act_name'] = "发红包活动名称"; $param['remark'] = "转发文章抢红包"; ksort($param); $sign_raw = ""; foreach($param as $k => $v){ $sign_raw .= $k."=".$v."&"; } $sign_raw .= "key=###"; //file_put_contents("sign.raw",$sign_raw);//debug $sign = strtoupper(md5($sign_raw)); echo $sign; $xml = "<xml>"; foreach($param as $k => $v){ $xml .= "<$k>$v</$k>"; } $xml.="<sign>$sign</sign></xml>"; echo $xml; echo postXmlCurl($xml,$url); function postXmlCurl($xml, $url, $useCert = true, $second = 30){ $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); }else{ curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 } //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if($useCert == true){ //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLCERT, "../cert/####.pem"); curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); curl_setopt($ch,CURLOPT_SSLKEY, "../cert/####.pem"); curl_setopt($ch, CURLOPT_CAINFO, '../cert/rootca.pem'); } //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); echo "error"; } }