微信红包接口API实现(php版)

WBOY
发布: 2016-07-29 09:00:25
原创
1550 人浏览过

更多请支持:http://www.webyang.net/Html/web/article_245.html

一、微信红包文档说明

目前微信红包总共分现金红包和裂变红包两种。
1、现金红包:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
2、裂变红包:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=16_5
更多请查看:https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php
二、php接口实现
本文讲解现金红包的调用,其他的大体一样,就不去尝试了。
参数说明:

微信红包接口API实现(php版)

代码实现:
片段一、
<ol>
<li value="1"><span>/**</span></li>
<li><span> * 微信支付</span></li>
<li><span> * @param string $openid 用户openid</span></li>
<li><span> */</span></li>
<li>
<span>public</span><span>function</span><span> pay</span><span>(</span><span>$re_openid</span><span>)</span>
</li>
<li><span>{</span></li>
<li>
<span>    include_once</span><span>(</span><span>'WxPacketClass.php'</span><span>);</span>
</li>
<li>
<span>    $wxHongBaoHelper </span><span>=</span><span>new</span><span>WxPacketClass</span><span>(</span><span>$this</span><span>-></span><span>app_sign</span><span>);</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"nonce_str"</span><span>,</span><span> $this</span><span>-></span><span>great_rand</span><span>());</span><span>//随机字符串,丌长于 32 位</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"mch_billno"</span><span>,</span><span> $this</span><span>-></span><span>app_mchid</span><span>.</span><span>date</span><span>(</span><span>'YmdHis'</span><span>).</span><span>rand</span><span>(</span><span>1000</span><span>,</span><span>9999</span><span>));</span><span>//订单号(28位)</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"mch_id"</span><span>,</span><span> $this</span><span>-></span><span>app_mchid</span><span>);</span><span>//商户号</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"wxappid"</span><span>,</span><span> $this</span><span>-></span><span>app_id</span><span>);</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"send_name"</span><span>,</span><span>'扬和宏科技'</span><span>);</span><span>//红包发送者名称</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"re_openid"</span><span>,</span><span> $re_openid</span><span>);</span><span>//openid</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"total_amount"</span><span>,</span><span>100</span><span>);</span><span>//付款金额,单位分</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"total_num"</span><span>,</span><span>1</span><span>);</span><span>//红包収放总人数</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"wishing"</span><span>,</span><span>'给您拜个晚年,祝您晚年幸福!'</span><span>);</span><span>//红包祝福诧</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"client_ip"</span><span>,</span><span>'127.0.0.1'</span><span>);</span><span>//调用接口的机器 Ip 地址</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"act_name"</span><span>,</span><span>'拜年红包活动'</span><span>);</span><span>//活劢名称</span>
</li>
<li>
<span>    $wxHongBaoHelper</span><span>-></span><span>setParameter</span><span>(</span><span>"remark"</span><span>,</span><span>'大家快来抢!'</span><span>);</span><span>//备注信息</span>
</li>
<li>
<span>    $postXml </span><span>=</span><span> $wxHongBaoHelper</span><span>-></span><span>create_hongbao_xml</span><span>();</span>
</li>
<li>
<span>    $url </span><span>=</span><span>'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'</span><span>;</span>
</li>
<li>
<span>    $responseXml </span><span>=</span><span> $wxHongBaoHelper</span><span>-></span><span>curl_post_ssl</span><span>(</span><span>$url</span><span>,</span><span> $postXml</span><span>);</span>
</li>
<li>
<span>    $responseObj </span><span>=</span><span> simplexml_load_string</span><span>(</span><span>$responseXml</span><span>,</span><span>'SimpleXMLElement'</span><span>,</span><span> LIBXML_NOCDATA</span><span>);</span>
</li>
<li>
<span>return</span><span> $responseObj</span><span>-></span><span>return_code</span><span>;</span>
</li>
<li><span>}</span></li>
</ol>
登录后复制
片段二、
<ol>
<li value="1"><span>//生成红包接口XML信息</span></li>
<li><span>/*</span></li>
<li><span><xml></xml></span></li>
<li><span>	<sign>![CDATA[E1EE61A9]]</sign></span></li>
<li><span>	<mch_billno>![CDATA[00100]]</mch_billno></span></li>
<li><span>	<mch_id>![CDATA[888]]</mch_id></span></li>
<li><span>	<wxappid>![CDATA[wxcbda96de0b165486]]</wxappid></span></li>
<li><span>	<send_name>![CDATA[send_name]]</send_name></span></li>
<li><span>	<re_openid>![CDATA[onqOjjXXXXXXXXX]]</re_openid></span></li>
<li><span>	<total_amount>![CDATA[100]]</total_amount></span></li>
<li><span>	<total_num>![CDATA[1]]</total_num></span></li>
<li><span>	<wishing>![CDATA[恭喜发财]]</wishing></span></li>
<li><span>	<client_ip>![CDATA[127.0.0.1]]</client_ip></span></li>
<li><span>	<act_name>![CDATA[新年红包]]</act_name></span></li>
<li><span>	<act_id>![CDATA[act_id]]</act_id></span></li>
<li><span>	<remark>![CDATA[新年红包]]</remark></span></li>
<li><span></span></li>
<li><span>*/</span></li>
<li>
<span>function</span><span> create_hongbao_xml</span><span>(</span><span>$retcode </span><span>=</span><span>0</span><span>,</span><span> $reterrmsg </span><span>=</span><span>"ok"</span><span>){</span>
</li>
<li>
<span>try</span><span>{</span>
</li>
<li>
<span>	    $this</span><span>-></span><span>setParameter</span><span>(</span><span>'sign'</span><span>,</span><span> $this</span><span>-></span><span>get_sign</span><span>());</span>
</li>
<li>
<span>	    $commonUtil </span><span>=</span><span>new</span><span>CommonUtil</span><span>();</span>
</li>
<li>
<span>return</span><span>  $commonUtil</span><span>-></span><span>arrayToXml</span><span>(</span><span>$this</span><span>-></span><span>parameters</span><span>);</span>
</li>
<li>
<span>}</span><span>catch</span><span>(</span><span>SDKException</span><span> $e</span><span>)</span><span>{</span>
</li>
<li>
<span>die</span><span>(</span><span>$e</span><span>-></span><span>errorMessage</span><span>());</span>
</li>
<li><span>}</span></li>
<li><span>}</span></li>
</ol>
登录后复制
片段三、
<ol>
<li value="1">
<span>function</span><span> curl_post_ssl</span><span>(</span><span>$url</span><span>,</span><span> $vars</span><span>,</span><span> $second</span><span>=</span><span>30</span><span>,</span><span>$aHeader</span><span>=</span><span>array</span><span>())</span><span>{</span>
</li>
<li>
<span>	$ch </span><span>=</span><span> curl_init</span><span>();</span>
</li>
<li><span>//超时时间</span></li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_TIMEOUT</span><span>,</span><span>$second</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_RETURNTRANSFER</span><span>,</span><span>1</span><span>);</span>
</li>
<li><span>//这里设置代理,如果有的话</span></li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_URL</span><span>,</span><span>$url</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_SSL_VERIFYPEER</span><span>,</span><span>false</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_SSL_VERIFYHOST</span><span>,</span><span>false</span><span>);</span>
</li>
<li>
<li><span>//cert 与 key 分别属于两个.pem文件</span></li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_SSLCERT</span><span>,</span><span>dirname</span><span>(</span><span>__FILE__</span><span>).</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'cert'</span><span>.</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'apiclient_cert.pem'</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_SSLKEY</span><span>,</span><span>dirname</span><span>(</span><span>__FILE__</span><span>).</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'cert'</span><span>.</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'apiclient_key.pem'</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_CAINFO</span><span>,</span><span>dirname</span><span>(</span><span>__FILE__</span><span>).</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'cert'</span><span>.</span><span>DIRECTORY_SEPARATOR</span><span>.</span><span>'rootca.pem'</span><span>);</span>
</li>
<li>
<li>
<span>if</span><span>(</span><span> count</span><span>(</span><span>$aHeader</span><span>)</span><span>>=</span><span>1</span><span>)</span><span> curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span> CURLOPT_HTTPHEADER</span><span>,</span><span> $aHeader</span><span>);</span>
</li>
<li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_POST</span><span>,</span><span>1</span><span>);</span>
</li>
<li>
<span>	curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_POSTFIELDS</span><span>,</span><span>$vars</span><span>);</span>
</li>
<li>
<span>	$data </span><span>=</span><span> curl_exec</span><span>(</span><span>$ch</span><span>);</span>
</li>
<li>
<span>if</span><span>(</span><span>$data</span><span>){</span>
</li>
<li>
<span>		curl_close</span><span>(</span><span>$ch</span><span>);</span>
</li>
<li>
<span>return</span><span> $data</span><span>;</span>
</li>
<li>
<span>}</span><span>else</span><span>{</span>
</li>
<li>
<span>		$error </span><span>=</span><span> curl_errno</span><span>(</span><span>$ch</span><span>);</span>
</li>
<li>
<span>		curl_close</span><span>(</span><span>$ch</span><span>);</span>
</li>
<li>
<span>return</span><span>false</span><span>;</span>
</li>
<li><span>}</span></li>
<li><span>}</span></li>
</ol>
登录后复制
代码结构:
|~action/
| `-PacketClass.php
|~lib/
| |~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-SdkExtraClass.php
| |-WxApi.php
| `-WxPacketClass.php
`-index.php
每个文件都有详细的说明。
三、效果展示

微信红包接口API实现(php版)  微信红包接口API实现(php版)

有需要源码的可以联系我~

以上就介绍了微信红包接口API实现(php版),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板