首页 » PHP » 微信红包接口PHP的调用方式

微信红包接口PHP的调用方式

8556 0

上一文《微信服务号红包发放》写得太过仓促,所以把接口实现的方式分开到此文中。下文直接给出代码,看不懂的,看完不知道怎么用的请绕道。

<?php
/* 微信发放红包        */
/* V 1.0               */
/* by tiandi           */
/* www.tiandiyoyo.com  */
/* 2015.3.5            */

define("APIKEY","");  //微信现金红包api key
define("PATHCERT",""); //cert存放位置,不放在网站下
define("PATHKEY",""); //key存放位置,不放在网站下
define("PATHCA",""); //ca存放位置,不放在网站下

class wxhb {
	var $para;
	function wxhb() 
	{
	}

	function __construct()
	{
	}

	function set_para($key,$value){
		$this->para[$key] = $value;
	}

	function create_noncestr( $length = 24 ) {  
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
		$str ="";  
		for ( $i = 0; $i < $length; $i++ )  {  
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);   
		}  
		return $str;  
	}

	function check_sign_para(){
		if($this->para["nonce_str"] == null || 
			$this->para["mch_billno"] == null || 
			$this->para["mch_id"] == null || 
			$this->para["wxappid"] == null || 
			$this->para["nick_name"] == null || 
			$this->para["send_name"] == null ||
			$this->para["re_openid"] == null || 
			$this->para["total_amount"] == null || 
			$this->para["max_value"] == null || 
			$this->para["total_num"] == null || 
			$this->para["wishing"] == null || 
			$this->para["client_ip"] == null || 
			$this->para["act_name"] == null || 
			$this->para["remark"] == null || 
			$this->para["min_value"] == null
			)
		{
			return false;
		}
		return true;

	}

	function create_sign(){
		if($this->check_sign_para() == false) {
			echo "签名参数错误!";
		}
		ksort($this->para);
		$tempsign = "";
		foreach ($this->para as $k => $v){
			if (null != $v && "null" != $v && "sign" != $k) {
				$tempsign .= $k . "=" . $v . "&";
			}
		}
		$tempsign = substr($tempsign, 0, strlen($tempsign)-1); //去掉最后的&
		$tempsign .="&key=". APIKEY;  //拼接APIKEY

		return strtoupper(md5($tempsign));
	}

	function create_xml(){
		$this->set_para('sign', $this->create_sign());
		return $this->ArrayToXml($this->para);
	}

	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; 
    }

	function curl_post_ssl($url, $vars, $second=30)
	{
		$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文件
		curl_setopt($ch,CURLOPT_SSLCERT,PATHCERT);
 		curl_setopt($ch,CURLOPT_SSLKEY,PATHKEY);
 		curl_setopt($ch,CURLOPT_CAINFO,PATHCA);
	 
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
	 
		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);
			curl_close($ch);
			return false;
		}
	}
}

使用时调用,并配置参数。

define("APPID","");   //公众号
define('MCHID', "");  //商户号

$money = 100;
$mch_billno = MCHID.date('YmdHis').rand(1000, 9999);  //订单号
$act_name = "红包";  //活动名称,没鸟用,目前微信版本里没显示的地方

$wxhb = new wxhb();
$wxhb->set_para("nonce_str", $wxhb->create_noncestr()); //随机字符串
$wxhb->set_para("mch_billno", $mch_billno); //订单号
$wxhb->set_para("mch_id", MCHID); //商户号
$wxhb->set_para("wxappid", APPID);  //公众号
$wxhb->set_para("nick_name", '昵称'); //昵称,没鸟用,目前微信版本里没显示的地方
$wxhb->set_para("send_name", '网站名'); //红包发送者名称
$wxhb->set_para("re_openid", $openid); //发放者openid
$wxhb->set_para("total_amount", $money); //付款金额,单位分
$wxhb->set_para("min_value", $money); // 最小红包金额,单位分
$wxhb->set_para("max_value", $money); // 最大红包金额,单位分
$wxhb->set_para("total_num", 1); //红包发放总人数
$wxhb->set_para("wishing", '恭喜恭喜!'); //红包祝福诧
$wxhb->set_para("client_ip", ''); //调用接口的机器 Ip 地址
$wxhb->set_para("act_name", $act_name); //活动名称
//下面的都没鸟用,目前微信版本里没显示的地方
$wxhb->set_para("remark", '');//备注信息
$wxhb->set_para("logo_imgurl", ''); //商户logo的url
$wxhb->set_para("share_content", ''); //分享文案
$wxhb->set_para("share_url", ''); //分享链接
$wxhb->set_para("share_imgurl", ''); //分享的图片url

$postxml = $wxhb->create_xml(); 
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$response = $wxhb->curl_post_ssl($url, $postxml);

$responseObj = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);

部分代码来自微信开发团队的DEMO。

文章评分14次,平均分3.6

本文原始地址:https://www.tiandiyoyo.com/2015/03/wechat_hongbao_api_for_php/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

    没有相关的文章

评论前先开启评论开关: