Home  >  Article  >  Backend Development  >  WeChat red envelope interface API implementation (php version)

WeChat red envelope interface API implementation (php version)

WBOY
WBOYOriginal
2016-07-29 09:00:251529browse

Please support for more: http://www.webyang.net/Html/web/article_245.html

1. WeChat red envelope document description

Currently, WeChat red envelopes are divided into two types: cash red envelopes and fission red envelopes.
1. Cash red envelope: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5
2. Fission red envelope: https://pay.weixin.qq.com/wiki /doc/api/cash_coupon.php?chapter=16_5
For more information, please check: https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php
2. PHP interface implementation
This article explains cash red envelopes The call is basically the same as the others, so I won’t try it.
Parameter description:

WeChat red envelope interface API implementation (php version)

Code implementation:
fragment one,
<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>
fragment two,
<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>
fragment three,
<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>
code structure:
|~action/
| `-PacketClass.php
|~lib/
| | ~cert/
| | |-apiclient_cert.pem
| | |-apiclient_key.pem
| | `-rootca.pem
| |-SdkExtraClass.php
| |-WxApi.php
| `-WxPacketClass.php
` -index.php
Each file has detailed instructions.
3. Effect display

WeChat red envelope interface API implementation (php version) WeChat red envelope interface API implementation (php version)

If you need the source code, please contact me~

The above introduces the WeChat red envelope interface API implementation (php version), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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