php 微信發紅包介面測試執行個體

來源:互聯網
上載者:User


下面是一個類。使用方法:

$arr['openid']='ojgTTt8oF9VdYcGsJMACHpA-jy1U';
            $arr['hbname']="提現申請";
            $arr['body']="您的提現申請已經成功";
            $arr['fee']=1;
$comm = new Common_util_pub();         
$re = $comm->sendhongbaoto($arr);
var_dump($re);

注意認證位置和 商戶後台設定的key需要修改。

<?php
header("Content-type: text/html; charset=utf-8");
 
class Common_util_pub
{
    
    /**
* hbname 紅包名稱  fee 紅包金額 /元  body 內容  openid 微信使用者id
* @param undefined $arr
*
* @return
*/
    
public function sendhongbaoto($arr){
 
//$comm = new Common_util_pub();
$data['mch_id'] = '120005402';
$data['mch_billno'] = '120005402'.date("Ymd",time()).date("His",time()).rand(1111,9999);
$data['nonce_str'] = self::createNoncestr();
$data['re_openid'] = $arr['openid'];
$data['wxappid'] = 'wx8axxxxxbac4905';
$data['nick_name'] = $arr['hbname'];
$data['send_name'] = $arr['hbname'];
$data['total_amount'] = $arr['fee']*100;
$data['min_value'] = $arr['fee']*100;
$data['max_value'] = $arr['fee']*100;
$data['total_num'] = 1;
$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
$data['act_name'] = '測試活動';
$data['remark'] = '備忘一下';
$data['wishing'] = $arr['body'];
if(!$data['re_openid']) {  
     $rearr['return_msg']='缺少使用者openid';
     return $rearr;
}
$data['sign'] = self::getSign($data);
$xml = self::arrayToXml($data);
//var_dump($xml);
$url ="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
$re = self::wxHttpsRequestPem($xml,$url);
$rearr = self::xmlToArray($re);
 
 
return  $rearr;
}
    
function trimString($value)
    {
        $ret = null;
        if (null != $value)
        {
            $ret = $value;
            if (strlen($ret) == 0)
            {
                $ret = null;
            }
        }
        return $ret;
    }
    
    /**
     *  作用:產生隨機字串,不長於32位
     */
    public function createNoncestr( $length = 32 )
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; 
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  { 
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
        } 
        return $str;
    }
    
    /**
     *  作用:格式化參數,簽名過程需要使用
     */
    function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
               $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }
    
    /**
     *  作用:產生簽名
     */
    public function getSign($Obj)
    {
        foreach ($Obj as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        //簽名步驟一:按字典序排序參數
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //echo '【string1】'.$String.'</br>';
        //簽名步驟二:在string後加入KEY
        $String = $String."&key="."254554sefg4exxxxxxxxs5cds1"; // 商戶後台設定的key
        //echo "【string2】".$String."</br>";
        //簽名步驟三:MD5加密
        $String = md5($String);
        //echo "【string3】 ".$String."</br>";
        //簽名步驟四:所有字元轉為大寫
        $result_ = strtoupper($String);
        //echo "【result】 ".$result_."</br>";
        return $result_;
    }
    
    /**
     *  作用:array轉xml
     */
public  function arrayToXml($arr)
    {
        $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;
    }
    
    /**
     *  作用:將xml轉為array
     */
    public function xmlToArray($xml)
    {      
        //將XML轉為array       
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);     
        return $array_data;
    }
 
 
 
    
    
    
     public function wxHttpsRequestPem( $vars,$url, $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,dirname(__FILE__).'/hongbao/apiclient_cert.pem');
                //預設格式為PEM,可以注釋
                curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
                curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).'/hongbao/apiclient_key.pem');
 
                curl_setopt($ch,CURLOPT_CAINFO,'PEM');
                curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'/hongbao/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);
                        return $data;
                }
                else {
                        $error = curl_errno($ch);
                        echo "call faild, errorCode:$error\n";
                        curl_close($ch);
                        return false;
                }
        }
    
}
 
 
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.