首頁 > 後端開發 > php教程 > 微信紅包介面API實作(php版)

微信紅包介面API實作(php版)

WBOY
發布: 2016-07-29 09:00:25
原創
1571 人瀏覽過

更多請支援: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/
|``-PackClass. ~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-SdkExtraClass.php
| |-WxApi.php
| |-SdkExtraClass.php
| |-WxApi.php
| `php-Wxacket `php. -index.php
每個文件都有詳細的說明。
三、效果展示

  微信紅包介面API實作(php版)微信紅包介面API實作(php版)

有需要源碼的可以聯絡我~

以上就介紹了微信紅包介面API實作(php版),包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板