PHP微信紅包生成代碼分享
本文實(shí)例為大家分享了PHP微信公眾號(hào)自動(dòng)發(fā)送紅包API代碼,分享給大家供大家參考。具體如下:
貼出核心接口代碼至于數(shù)據(jù)自己填寫(xiě),接口測(cè)試OK
wechat_packet.php
<!--?php
/**
* 發(fā)送紅包接口
* Created by PhpStorm.
* User: ADKi
* Date: 2016/4/25 0025
* Time: 15:25
*/
class wechat_packet{
private $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';//請(qǐng)求URL
private $mch_id;//商戶號(hào)
private $weixin_appid;//公眾賬號(hào)appid
private $send_name;//商戶名稱(chēng)
private $total_num = 1;//發(fā)送紅包總?cè)藬?shù)
private $wishing;//紅包祝福語(yǔ)
private $client_ip;//調(diào)用接口的機(jī)器Ip地址
private $act_name;//活動(dòng)名稱(chēng)
private $remark;//備注信息
private $nonce_str;//隨機(jī)字符串,不長(zhǎng)于32位
private $api_password;
private $arraytoxml;//數(shù)組轉(zhuǎn)xml
/**
* 公鑰
*/
private $public_key = "/api/wechat/cert/apiclient_cert.pem";
/**
* 私鑰
*/
private $private_key = '/api/wechat/cert/apiclient_key.pem';
/**
* ca證書(shū)
*/
private $rootca = 'api/wechat/cert/rootca.pem';
public function __construct()
{
//初始化紅包設(shè)置信息
$this--->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);
}
/*
請(qǐng)確保您的libcurl版本是否支持雙向認(rèn)證,版本高于7.20.1
*/
private function curl_post_ssl($url, $vars, $second=30,$aHeader=array()){
$ch = curl_init();
//超時(shí)時(shí)間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//這里設(shè)置代理,如果有的話
//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 分別屬于兩個(gè).pem文件
//默認(rèn)格式為PEM,可以注釋
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().$this->public_key);
//默認(rèn)格式為PEM,可以注釋
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,getcwd().$this->private_key);
//ca證書(shū)
curl_setopt($ch,CURLOPT_CAINFO,$this->rootca);
//第二種方式,兩個(gè)文件合成一個(gè).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 .= '&';
}
}
}
//轉(zhuǎn)成UTF-8
$stringA = $this->gbkToUtf8($stringA);
$stringSignTemp="$stringA&key=$this->api_password";
$sign = MD5($stringSignTemp);
$sign = strtoupper($sign);
return $sign;
}
//生成隨機(jī)字符串
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兩個(gè)數(shù)之間的一個(gè)隨機(jī)整數(shù)
}
return $str;
}
/**
*自動(dòng)判斷把gbk或gb2312編碼的字符串轉(zhuǎn)為utf8
*能自動(dòng)判斷輸入字符串的編碼類(lèi),如果本身是utf-8就不用轉(zhuǎn)換,否則就轉(zhuǎn)換為utf-8的字符串
*支持的字符編碼類(lèi)型是: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;
}
}
數(shù)組轉(zhuǎn)xml:arraytoxml.php
<!--?php
/**
* Created by PhpStorm.
* User: ADKi
* Date: 2016/4/26 0026
* Time: 12:19
*/
class ArrayToXML
{
public function toXml($data){
$xml = '<xml-->';
foreach ($data as $key => $value){
if (is_numeric($value)){
$xml .= "<".$key.">".$value."<!--".$key."-->";
}else{
$xml .= "<".$key."><!--[CDATA[".$value."]]--><!--".$key."-->";
}
}
$xml .= '';
return $xml;
}
}
重寫(xiě)了一下PHP下面的微信API接口,
微信紅包支持,JSAPI的動(dòng)態(tài)參數(shù)接口支持
http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/wxapi.class.php
微信API類(lèi) - 增加紅包支持
<?php
/********************************************************
* @author Kyler You <QQ:2444756311>
* @link http://mp.weixin.qq.com/wiki/home/index.html
* @version 2.0.1
* @uses $wxApi = new WxApi();
* @package 微信API接口 陸續(xù)會(huì)繼續(xù)進(jìn)行更新
********************************************************/
class WxApi {
const appId = "";
const appSecret = "";
const mchid = ""; //商戶號(hào)
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;
}
/****************************************************
* 微信帶證書(shū)提交數(shù)據(jù) - 微信紅包使用
****************************************************/
public function wxHttpsRequestPem($url, $vars, $second=30,$aHeader=array()){
$ch = curl_init();
//超時(shí)時(shí)間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//這里設(shè)置代理,如果有的話
//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 分別屬于兩個(gè).pem文件
//默認(rèn)格式為PEM,可以注釋
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
//默認(rèn)格式為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');
//第二種方式,兩個(gè)文件合成一個(gè).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 返回指定微信公眾號(hào)的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;
}
/****************************************************
* 微信通過(guò)OPENID獲取用戶信息,返回?cái)?shù)組
****************************************************/
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;
}
/****************************************************
* 微信通過(guò)指定模板信息發(fā)送給指定用戶,發(fā)送完成后返回指定JSON數(shù)據(jù)
****************************************************/
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;
}
/****************************************************
* 發(fā)送自定義的模板消息
****************************************************/
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;
}
/****************************************************
* 微信設(shè)置OAUTH跳轉(zhuǎn)URL,返回字符串信息 - SCOPE = snsapi_base //驗(yàn)證時(shí)不返回確認(rèn)頁(yè)面,只能獲取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;
}
/****************************************************
* 微信設(shè)置OAUTH跳轉(zhuǎn)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跳轉(zhuǎn)指定URL
****************************************************/
public function wxHeader($url){
header("location:".$url);
}
/****************************************************
* 微信通過(guò)OAUTH返回頁(yè)面中獲取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;
}
/****************************************************
* 微信通過(guò)OAUTH的Access_Token的信息獲取當(dāng)前用戶信息 // 只執(zhí)行在snsapi_userinfo模式運(yùn)行
****************************************************/
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;
}
/*****************************************************
* 生成隨機(jī)字符串 - 最長(zhǎng)為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;
}
}
/*******************************************************
* 微信商戶訂單號(hào) - 最長(zhǎng)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;
}
/*******************************************************
* 微信格式化數(shù)組變成參數(shù)格式 - 支持url加密
*******************************************************/
public function wxSetParam($parameters){
if(is_array($parameters) && !empty($parameters)){
$this->parameters = $parameters;
return $this->parameters;
}
else{
return array();
}
}
/*******************************************************
* 微信格式化數(shù)組變成參數(shù)格式 - 支持url加密
*******************************************************/
public function wxFormatArray($parameters = NULL, $urlencode = FALSE){
if(is_null($parameters)){
$parameters = $this->parameters;
}
$restr = "";//初始化空
ksort($parameters);//排序參數(shù)
foreach ($parameters as $k => $v){//循環(huán)定制參數(shù)
if (null != $v && "null" != $v && "sign" != $k) {
if($urlencode){//如果參數(shù)需要增加URL加密就增加,不需要?jiǎng)t不需要
$v = urlencode($v);
}
$restr .= $k . "=" . $v . "&";//返回完整字符串
}
}
if (strlen($restr) > 0) {//如果存在數(shù)據(jù)則將最后“&”刪除
$restr = substr($restr, 0, strlen($restr)-1);
}
return $restr;//返回字符串
}
/*******************************************************
* 微信MD5簽名生成器 - 需要將參數(shù)數(shù)組轉(zhuǎn)化成為字符串[wxFormatArray方法]
*******************************************************/
public function wxMd5Sign($content, $privatekey){
try {
if (is_null($key)) {
throw new Exception("財(cái)付通簽名key不能為空!");
}
if (is_null($content)) {
throw new Exception("財(cái)付通簽名內(nèi)容不能為空");
}
$signStr = $content . "&key=" . $key;
return strtoupper(md5($signStr));
}
catch (Exception $e)
{
die($e->getMessage());
}
}
/*******************************************************
* 微信Sha1簽名生成器 - 需要將參數(shù)數(shù)組轉(zhuǎn)化成為字符串[wxFormatArray方法]
*******************************************************/
public function wxSha1Sign($content, $privatekey){
try {
if (is_null($key)) {
throw new Exception("財(cái)付通簽名key不能為空!");
}
if (is_null($content)) {
throw new Exception("財(cái)付通簽名內(nèi)容不能為空");
}
$signStr = $content . "&key=" . $key;
return strtoupper(sha1($signStr));
}
catch (Exception $e)
{
die($e->getMessage());
}
}
/*******************************************************
* 將數(shù)組解析XML - 微信紅包接口
*******************************************************/
public function wxArrayToXml($parameters = NULL){
if(is_null($parameters)){
$parameters = $this->parameters;
}
if(!is_array($parameters) || empty($parameters)){
die("參數(shù)不為數(shù)組無(wú)法解析");
}
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
}
后期還是會(huì)增加在一起的把這個(gè)CLASS做起來(lái),網(wǎng)上資源很多,但是都是有一定基礎(chǔ)的人去看看改改可以,對(duì)于沒(méi)有接觸剛剛接觸的新手還是需要給予支持的。幫助用戶屢屢思路。
再來(lái)一份:
首先給大家看一看這個(gè)表格:

根據(jù)微信高級(jí)紅包接口,開(kāi)發(fā)PHP版本的API接口,現(xiàn)在進(jìn)行主要代碼分析。
紅包接口調(diào)用請(qǐng)求代碼,所有請(qǐng)求參數(shù)為必填參數(shù)與文檔對(duì)應(yīng):
class Wxapi {
private $app_id = 'wxXXXXXXXXXXXX'; //公眾賬號(hào)appid,首先申請(qǐng)與之配套的公眾賬號(hào)
private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';//公眾號(hào)secret,用戶獲取用戶授權(quán)token
private $app_mchid = 'XXXXXXXX';//商戶號(hào)id
function __construct(){
//do sth here....
}
/**
* 微信支付
* @param string $openid 用戶openid
*/
public function pay($re_openid)
{
include_once('WxHongBaoHelper.php');
$commonUtil = new CommonUtil();
$wxHongBaoHelper = new WxHongBaoHelper();
$wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//隨機(jī)字符串,丌長(zhǎng)于 32 位
$wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//訂單號(hào)
$wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商戶號(hào)
$wxHongBaoHelper->setParameter("wxappid", $this->app_id);
$wxHongBaoHelper->setParameter("nick_name", '紅包');//提供方名稱(chēng)
$wxHongBaoHelper->setParameter("send_name", '紅包');//紅包發(fā)送者名稱(chēng)
$wxHongBaoHelper->setParameter("re_openid", $re_openid);//相對(duì)于醫(yī)脈互通的openid
$wxHongBaoHelper->setParameter("total_amount", 100);//付款金額,單位分
$wxHongBaoHelper->setParameter("min_value", 100);//最小紅包金額,單位分
$wxHongBaoHelper->setParameter("max_value", 100);//最大紅包金額,單位分
$wxHongBaoHelper->setParameter("total_num", 1);//紅包収放總?cè)藬?shù)
$wxHongBaoHelper->setParameter("wishing", '感謝您參與紅包派發(fā)活動(dòng),祝您新年快樂(lè)!');//紅包祝福詫
$wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//調(diào)用接口的機(jī)器 Ip 地址
$wxHongBaoHelper->setParameter("act_name", '紅包活動(dòng)');//活勱名稱(chēng)
$wxHongBaoHelper->setParameter("remark", '快來(lái)?yè)專(zhuān)?);//備注信息
$postXml = $wxHongBaoHelper->create_hongbao_xml();
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
//用作結(jié)果調(diào)試輸出
//echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
$responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
return $responseObj->return_code;
}
獲取隨機(jī)字符串方法:
/**
* 生成隨機(jī)數(shù)
*/
public function great_rand(){
$str = '1234567890abcdefghijklmnopqrstuvwxyz';
for($i=0;$i<30;$i++){
$j=rand(0,35);
$t1 .= $str[$j];
}
return $t1;
}
簽名算法:
/**
例如:
appid: wxd111665abv58f4f
mch_id: 10000100
device_info: 1000
Body: test
nonce_str: ibuaiVcKdpRxkhJA
第一步:對(duì)參數(shù)按照 key=value 的格式,并按照參數(shù)名 ASCII 字典序排序如下:
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
第二步:拼接支付密鑰:
stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
9CF3B7"
*/
protected function get_sign(){
define('PARTNERKEY',"QSRXXXXXXXXXXXXXXXXXXXXX");
try {
if (null == PARTNERKEY || "" == PARTNERKEY ) {
throw new SDKRuntimeException("密鑰不能為空!" . "<br>");
}
if($this->check_sign_parameters() == false) { //檢查生成簽名參數(shù)
throw new SDKRuntimeException("生成簽名參數(shù)缺失!" . "<br>");
}
$commonUtil = new CommonUtil();
ksort($this->parameters);
$unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
$md5SignUtil = new MD5SignUtil();
return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
}catch (SDKRuntimeException $e)
{
die($e->errorMessage());
}
}
CURL請(qǐng)求以及發(fā)送證書(shū):
function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
$ch = curl_init();
//超時(shí)時(shí)間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//這里設(shè)置代理,如果有的話
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//cert 與 key 分別屬于兩個(gè).pem文件
//請(qǐng)確保您的libcurl版本是否支持雙向認(rèn)證,版本高于7.20.1
curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.'zhengshu'.DIRECTORY_SEPARATOR.'rootca.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;
}
}
入口文件:
@require "pay.php";
//獲取用戶信息
$get = $_GET['param'];
$code = $_GET['code'];
//判斷code是否存在
if($get=='access_token' && !empty($code)){
$param['param'] = 'access_token';
$param['code'] = $code;
$packet = new Packet();
//獲取用戶openid信息
$userinfo = $packet->_route('userinfo',$param);
if(empty($userinfo['openid'])){
exit("NOAUTH");
}
//調(diào)取支付方法
$packet->_route('wxpacket',array('openid'=>$userinfo['openid']));
}else{
$packet->_route('userinfo');
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)PHP程序設(shè)計(jì)有所幫助,也希望大家多多支持腳本之家。
- php實(shí)現(xiàn)微信分享朋友鏈接功能
- 微信小程序支付功能 php后臺(tái)對(duì)接完整代碼分享
- php實(shí)現(xiàn)的微信分享到朋友圈并記錄分享次數(shù)功能
- thinkPHP微信分享接口JSSDK用法實(shí)例
- thinkphp項(xiàng)目如何自定義微信分享描述內(nèi)容
- PHP微信分享開(kāi)發(fā)詳解
- thinkphp整合微信支付代碼分享
- 微信自定義分享php代碼分析
- 基于thinkPHP實(shí)現(xiàn)的微信自定義分享功能示例
- php版微信公眾號(hào)自定義分享內(nèi)容實(shí)現(xiàn)方法
- php微信瀏覽器分享設(shè)置以及回調(diào)詳解
- PHP開(kāi)發(fā)微信支付的代碼分享
- php微信分享到朋友圈、QQ、朋友、微博
相關(guān)文章
PHP實(shí)現(xiàn)爬蟲(chóng)爬取圖片代碼實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)爬蟲(chóng)爬取圖片代碼實(shí)例,有實(shí)際的代碼例子,感興趣的同學(xué)可以嘗試下2021-03-03
php處理靜態(tài)頁(yè)面:頁(yè)面設(shè)置緩存時(shí)間實(shí)例
本篇文章主要介紹了php處理靜態(tài)頁(yè)面:頁(yè)面設(shè)置緩存時(shí)間實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
解決tp5在nginx下修改配置訪問(wèn)的問(wèn)題
今天小編就為大家分享一篇解決tp5在nginx下修改配置訪問(wèn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
基于PHP實(shí)現(xiàn)發(fā)微博動(dòng)態(tài)代碼實(shí)例
這篇文章主要介紹了基于PHP實(shí)現(xiàn)發(fā)微博動(dòng)態(tài)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
PHP中cookie和session的區(qū)別實(shí)例分析
這篇文章主要介紹了PHP中cookie和session的區(qū)別,比較詳盡的分析了二者從創(chuàng)建、運(yùn)用到清除的各個(gè)流程的注意事項(xiàng),需要的朋友可以參考下2014-08-08
微信小程序之支付后調(diào)用SDK的異步通知及驗(yàn)證處理訂單方法
下面小編就為大家分享一篇微信小程序之支付后調(diào)用SDK的異步通知及驗(yàn)證處理訂單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2018-01-01

