Home Backend Development PHP Tutorial WeChat red envelope interface API implementation (php version)

WeChat red envelope interface API implementation (php version)

Jul 29, 2016 am 09:00 AM
curl gt quot

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>
Copy after login
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>
Copy after login
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>
Copy after login
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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

what is linux curl what is linux curl Apr 20, 2023 pm 05:05 PM

In Linux, curl is a very practical tool for transferring data to and from the server. It is a file transfer tool that uses URL rules to work under the command line; it supports file upload and download, and is a comprehensive transfer tool. . Curl provides a lot of very useful functions, including proxy access, user authentication, ftp upload and download, HTTP POST, SSL connection, cookie support, breakpoint resume and so on.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

See all articles