Home >php教程 >php手册 >PHP版微信红包API接口程序

PHP版微信红包API接口程序

WBOY
WBOYOriginal
2016-05-22 18:38:491153browse

对于 微信红包我相信今年过年时各位朋友都有知道吧,这个微信红包相当的火爆了,下面我们来给各位介绍一个PHP版微信红包API接口程序了,我们可以基于这个接口也实现开发第三方红包功能。

根据微信高级红包接口,开发PHP版本的API接口,现在进行主要代码分析。

红包接口调用请求代码,所有请求参数为必填参数与文档对应:

<?php
class Wxapi {
    private $app_id = &#39;wxXXXXXXXXXXXX&#39;; //公众账号appid,首先申请与之配套的公众账号
    private $app_secret = &#39;XXXXXXXXXXXXXXXXXXXXXXXX&#39;;//公众号secret,用户获取用户授权token
    private $app_mchid = &#39;XXXXXXXX&#39;;//商户号id
    function __construct(){
    //do sth here....
    }
    /**
     * 微信支付
     * @param string $openid 用户openid
     */
    public function pay($re_openid)
    {
        include_once(&#39;WxHongBaoHelper.php&#39;);
        $commonUtil = new CommonUtil();
        $wxHongBaoHelper = new WxHongBaoHelper();
        $wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//随机字符串,丌长于 32 位
        $wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date(&#39;YmdHis&#39;).rand(1000, 9999));//订单号
        $wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商户号
        $wxHongBaoHelper->setParameter("wxappid", $this->app_id);
        $wxHongBaoHelper->setParameter("nick_name", &#39;红包&#39;);//提供方名称
        $wxHongBaoHelper->setParameter("send_name", &#39;红包&#39;);//红包发送者名称
        $wxHongBaoHelper->setParameter("re_openid", $re_openid);//相对于医脉互通的openid
        $wxHongBaoHelper->setParameter("total_amount", 100);//付款金额,单位分
        $wxHongBaoHelper->setParameter("min_value", 100);//最小红包金额,单位分
        $wxHongBaoHelper->setParameter("max_value", 100);//最大红包金额,单位分
        $wxHongBaoHelper->setParameter("total_num", 1);//红包?Х抛苋耸?br />         $wxHongBaoHelper->setParameter("wishing", &#39;感谢您参与红包派发活动,祝您新年快乐!&#39;);//红包祝福诧
        $wxHongBaoHelper->setParameter("client_ip", &#39;127.0.0.1&#39;);//调用接口的机器 Ip 地址
        $wxHongBaoHelper->setParameter("act_name", &#39;红包活动&#39;);//活劢名称
        $wxHongBaoHelper->setParameter("remark", &#39;快来抢!&#39;);//备注信息
        $postXml = $wxHongBaoHelper->create_hongbao_xml();
        $url = &#39;https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack&#39;;
        $responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
        //用作结果调试输出
        //echo htmlentities($responseXml,ENT_COMPAT,&#39;UTF-8&#39;);
    $responseObj = simplexml_load_string($responseXml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
    return $responseObj->return_code;
    }
获取随机字符串方法:
/**
 * 生成随机数
 */     
public function great_rand(){
    $str = &#39;1234567890abcdefghijklmnopqrstuvwxyz&#39;;
    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

第一步:对参数按照 key=value 的格式,并按照参数名 ASCII 字典序排序如下:

stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";

第二步:拼接支付密钥:

stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7"

*/

protected function get_sign(){
    define(&#39;PARTNERKEY&#39;,"QSRXXXXXXXXXXXXXXXXXXXXX");
    try {
        if (null == PARTNERKEY || "" == PARTNERKEY ) {
           throw new SDKRuntimeException("密钥不能为空!" . "<br>");
        }
        if($this->check_sign_parameters() == false) {   //检查生成签名参数
           throw new SDKRuntimeException("生成签名参数缺失!" . "<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请求以及发送证书:
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_URL,$url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);     
    //cert 与 key 分别属于两个.pem文件
    //请确保您的libcurl版本是否支持双向认证,版本高于7.20.1
    curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;apiclient_cert.pem&#39;);
    curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;apiclient_key.pem&#39;);
    curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;rootca.pem&#39;);
    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:$errorn"; 
        curl_close($ch);
        return false;
    }
}

入口文件:

@require "pay.php";

//获取用户信息
$get = $_GET[&#39;param&#39;];
$code = $_GET[&#39;code&#39;];
//判断code是否存在
if($get==&#39;access_token&#39; && !empty($code)){
    $param[&#39;param&#39;] = &#39;access_token&#39;;
    $param[&#39;code&#39;] = $code;
    $packet = new Packet();
    //获取用户openid信息
    $userinfo = $packet->_route(&#39;userinfo&#39;,$param);
    if(empty($userinfo[&#39;openid&#39;])){
        exit("NOAUTH");
    }
    //调取支付方法
    $packet->_route(&#39;wxpacket&#39;,array(&#39;openid&#39;=>$userinfo[&#39;openid&#39;]));
}else{
    $packet->_route(&#39;userinfo&#39;);
}


文章链接:

随便收藏,请保留本文地址!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn