> 백엔드 개발 > PHP 튜토리얼 > WeChat 빨간 봉투 인터페이스 API 구현(php 버전)

WeChat 빨간 봉투 인터페이스 API 구현(php 버전)

WBOY
풀어 주다: 2016-07-29 09:00:25
원래의
1578명이 탐색했습니다.

더 많은 지원 부탁드립니다: http://www.webyang.net/Html/web/article_245.html

1. 위챗 빨간 봉투 문서 설명

현재 위챗 빨간 봉투는 현금빨간봉투와 핵분열빨간봉투로 나누어집니다.
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를 참조하세요.
2. PHP 인터페이스 구현
이 글에서는 현금 빨간 봉투를 호출하는 방법에 대해 설명하므로 다른 방법은 기본적으로 동일하므로 시도하지 않겠습니다.
매개변수 설명:

WeChat 빨간 봉투 인터페이스 API 구현(php 버전)

코드 구현:
조각 1,
<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>
로그인 후 복사
조각 2,
<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>
로그인 후 복사
조각 3,
<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/
| |-apiclient_cert.pem
| |-apiclient_key.pem
| `-rootca.pem
|-SdkExtraClass.php
| `-WxPacketClass.php
`-index.php
각 파일에는 자세한 지침이 있습니다.
3. 디스플레이


WeChat 빨간 봉투 인터페이스 API 구현(php 버전)WeChat 빨간 봉투 인터페이스 API 구현(php 버전)소스코드가 필요하시면 연락주세요~

위는 관련 내용을 포함하여 WeChat 빨간 봉투 인터페이스 API 구현(php 버전)을 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿