国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial Implement PHP WeChat red envelope API interface

Implement PHP WeChat red envelope API interface

Aug 14, 2020 pm 04:47 PM
api interface WeChat red envelope

Implement PHP WeChat red envelope API interface

First of all, let me take a look at this form:

Related learning recommendations: php programming(Video)

Based on the WeChat advanced red envelope interface, the PHP version of the API interface is developed, and now the main code analysis is carried out.

The red envelope interface calls the request code. All request parameters are required and correspond to the document:

class Wxapi {
 private $app_id = 'wxXXXXXXXXXXXX'; //公眾賬號appid,首先申請與之配套的公眾賬號
 private $app_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX';//公眾號secret,用戶獲取用戶授權(quán)token
 private $app_mchid = 'XXXXXXXX';//商戶號id
 function __construct(){
 //do sth here....
 }
 /**
  * 微信支付
  * @param string $openid 用戶openid
  */
 public function pay($re_openid)
 {
  include_once('WxHongBaoHelper.php');
  $commonUtil = new CommonUtil();
  $wxHongBaoHelper = new WxHongBaoHelper();
  $wxHongBaoHelper->setParameter("nonce_str", $this->great_rand());//隨機字符串,丌長于 32 位
  $wxHongBaoHelper->setParameter("mch_billno", $this->app_mchid.date('YmdHis').rand(1000, 9999));//訂單號
  $wxHongBaoHelper->setParameter("mch_id", $this->app_mchid);//商戶號
  $wxHongBaoHelper->setParameter("wxappid", $this->app_id);
  $wxHongBaoHelper->setParameter("nick_name", '紅包');//提供方名稱
  $wxHongBaoHelper->setParameter("send_name", '紅包');//紅包發(fā)送者名稱
  $wxHongBaoHelper->setParameter("re_openid", $re_openid);//相對于醫(yī)脈互通的openid
  $wxHongBaoHelper->setParameter("total_amount", 100);//付款金額,單位分
  $wxHongBaoHelper->setParameter("min_value", 100);//最小紅包金額,單位分
  $wxHongBaoHelper->setParameter("max_value", 100);//最大紅包金額,單位分
  $wxHongBaoHelper->setParameter("total_num", 1);//紅包収放總?cè)藬?shù)
  $wxHongBaoHelper->setParameter("wishing", '感謝您參與紅包派發(fā)活動,祝您新年快樂!');//紅包祝福詫
  $wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//調(diào)用接口的機器 Ip 地址
  $wxHongBaoHelper->setParameter("act_name", '紅包活動');//活勱名稱
  $wxHongBaoHelper->setParameter("remark", '快來搶!');//備注信息
  $postXml = $wxHongBaoHelper->create_hongbao_xml();
  $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
  $responseXml = $wxHongBaoHelper->curl_post_ssl($url, $postXml);
  //用作結(jié)果調(diào)試輸出
  //echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
 $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
 return $responseObj->return_code;
 }

Method to obtain a random string:

/**
 * 生成隨機數(shù)
 */  
public function great_rand(){
 $str = '1234567890abcdefghijklmnopqrstuvwxyz';
 for($i=0;$i<30;$i++){
  $j=rand(0,35);
  $t1 .= $str[$j];
 }
 return $t1; 
}

Signature algorithm:

/**
例如:
appid: wxd111665abv58f4f
mch_id: 10000100
device_info: 1000
Body: test
nonce_str: ibuaiVcKdpRxkhJA
第一步:對參數(shù)按照 key=value 的格式,并按照參數(shù)名 ASCII 字典序排序如下:
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_i
d=10000100&nonce_str=ibuaiVcKdpRxkhJA";
第二步:拼接支付密鑰:
stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A
9CF3B7"
*/
protected function get_sign(){
 define(&#39;PARTNERKEY&#39;,"QSRXXXXXXXXXXXXXXXXXXXXX");
 try {
  if (null == PARTNERKEY || "" == PARTNERKEY ) {
   throw new SDKRuntimeException("密鑰不能為空!" . "<br>");
  }
  if($this->check_sign_parameters() == false) { //檢查生成簽名參數(shù)
   throw new SDKRuntimeException("生成簽名參數(shù)缺失!" . "<br>");
  }
  $commonUtil = new CommonUtil();
  ksort($this->parameters);
  $unSignParaString = $commonUtil->formatQueryParaMap($this->parameters, false);
  $md5SignUtil = new MD5SignUtil();
  return $md5SignUtil->sign($unSignParaString,$commonUtil->trimString(PARTNERKEY));
 }catch (SDKRuntimeException $e)
 {
  die($e->errorMessage());
 }
 
}

CURL request and send certificate:

function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
 $ch = curl_init();
 //超時時間
 curl_setopt($ch,CURLOPT_TIMEOUT,$second);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
 //這里設(shè)置代理,如果有的話
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);  
 //cert 與 key 分別屬于兩個.pem文件
 //請確保您的libcurl版本是否支持雙向認證,版本高于7.20.1
 curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;apiclient_cert.pem&#39;);
 curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;apiclient_key.pem&#39;);
 curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).DIRECTORY_SEPARATOR.&#39;zhengshu&#39;.DIRECTORY_SEPARATOR.&#39;rootca.pem&#39;);
 if( count($aHeader) >= 1 ){
  curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
 }
 curl_setopt($ch,CURLOPT_POST, 1);
 curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
 $data = curl_exec($ch);
 if($data){
  curl_close($ch);
  return $data;
 }
 else { 
  $error = curl_errno($ch);
  //echo "call faild, errorCode:$error\n"; 
  curl_close($ch);
  return false;
 }
}

Entry file:

@require "pay.php";
//獲取用戶信息
$get = $_GET[&#39;param&#39;];
$code = $_GET[&#39;code&#39;];
//判斷code是否存在
if($get==&#39;access_token&#39; && !empty($code)){
 $param[&#39;param&#39;] = &#39;access_token&#39;;
 $param[&#39;code&#39;] = $code;
 $packet = new Packet();
 //獲取用戶openid信息
 $userinfo = $packet->_route(&#39;userinfo&#39;,$param);
 if(empty($userinfo[&#39;openid&#39;])){
  exit("NOAUTH");
 }
 //調(diào)取支付方法
 $packet->_route(&#39;wxpacket&#39;,array(&#39;openid&#39;=>$userinfo[&#39;openid&#39;]));
}else{
 $packet->_route(&#39;userinfo&#39;);
}

Related learning recommendations: Programming video

The above is the detailed content of Implement PHP WeChat red envelope API interface. For more information, please follow other related articles on the PHP Chinese website!

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1502
276
What is the maximum amount of WeChat red envelopes that can be sent? What is the maximum amount of WeChat red envelopes that can be sent? Feb 23, 2024 pm 04:40 PM

What is the maximum amount of WeChat red envelopes that can be sent? There is an upper limit on the amount of red envelopes sent in WeChat, and some users are not clear about the maximum amount of red envelopes that can be sent on WeChat. The maximum amount on non-special dates is 200. Next is an introduction to the maximum amount of red envelopes that the editor brings to users. Interested users can come and take a look! WeChat usage tutorial How to set WeChat status background image Answer: 200 yuan Details: 1. The upper limit of WeChat red envelope amount: 200. Each person can only send a maximum of 200 yuan in WeChat red envelopes each time. 2. Special red envelope amount: On May 20 of each year, users can send red envelopes with an upper limit of 520 yuan.

How to return a WeChat red envelope? How to reject a WeChat red envelope and return it immediately to the other party? How to return a WeChat red envelope? How to reject a WeChat red envelope and return it immediately to the other party? Feb 22, 2024 pm 04:43 PM

The current WeChat red envelope does not support manual return. Tutorial Applicable Model: iPhone13 System: iOS15.5 Version: WeChat 8.0.19 Analysis 1 The current WeChat red envelope does not support manual return. For WeChat red envelopes that have not been claimed, the WeChat system will automatically return it to the originating account after 24 hours, so when If we want to return the red envelope after receiving it, as long as we don't accept it, it will be automatically returned after 24 hours. Supplement: How to return a WeChat transfer 1 Click on the transfer that needs to be returned on the WeChat chat interface. 2In the transfer details interface, click the refund option. 3. In the new pop-up window that appears, click Return again. Summary/Notes: The current WeChat red envelope does not support manual return. When we receive the red envelope, as long as we do not accept it, it will be returned after 24 hours.

Email Sending API Interface Guide in PHP Email Sending API Interface Guide in PHP May 21, 2023 pm 12:12 PM

With the popularity of email in our daily lives, email sending has become an essential feature in many applications. As a popular web development language, PHP also provides a corresponding email sending API interface. This article will introduce the email sending API interface in PHP to beginners and developers, including how to configure the mail server, how to use PHP's built-in email functions, and how to use a third-party email sending library. 1. Configure the mail server. Before using PHP to send mail, you need to first configure an SMTP server.

What is the API interface for? What is the API interface for? Apr 23, 2024 pm 01:51 PM

An API interface is a specification for interaction between software components and is used to implement communication and data exchange between different applications or systems. The API interface acts as a "translator", converting the developer's instructions into computer language so that the applications can work together. Its advantages include convenient data sharing, simplified development, improved performance, enhanced security, improved productivity and interoperability.

What are the free API interface websites? What are the free API interface websites? Jan 05, 2024 am 11:33 AM

Free api interface website: 1. UomgAPI: a platform that provides stable and fast free API services, with over 100 API interfaces; 2. free-api: provides multiple free API interfaces; 3. JSON API: provides free data API interface; 4. AutoNavi Open Platform: Provides map-related API interfaces; 5. Face recognition Face++: Provides face recognition-related API interfaces; 6. Speed ??data: Provides over a hundred free API interfaces, suitable for various needs In the case of data sources; 7. Aggregate data, etc.

Developing API documentation: A step-by-step guide to PHP API interfaces Developing API documentation: A step-by-step guide to PHP API interfaces Jan 22, 2024 am 11:20 AM

With the increasing popularity of web applications, APIs (Application Programming Interfaces) are becoming more and more important and playing an increasingly important role in web development. WebAPI is a technology that allows users to access applications through the Internet. It is a basic tool for combining different applications. PHP is a widely used programming language, especially in the field of web development. Developers can allow other applications to use their application functionality by developing PHP API interfaces. In order to achieve this

What are the main types of api interfaces? What are the main types of api interfaces? Apr 23, 2024 pm 01:57 PM

API interface types are rich and diverse, including RESTful API, SOAP API, GraphQL API, etc. RESTful API communicates through the HTTP protocol, with a simple and efficient design, which is the current mainstream Web API design style. SOAP API is based on XML, focuses on cross-language and platform interoperability, and is mostly used in large enterprises and government agencies. GraphQL API is a new query language and runtime environment that supports flexible data query and response.

How to return WeChat red envelope? How to handle the return of WeChat red envelopes How to return WeChat red envelope? How to handle the return of WeChat red envelopes Mar 12, 2024 pm 05:00 PM

WeChat is a mobile application that integrates social networking, payment, shopping and other functions, and is deeply loved by users. Among them, WeChat red envelopes, as part of the payment function, have won the favor of the majority of users for their convenience and fun. However, sometimes we may encounter situations where the wrong red envelope is sent. Although it is sometimes troublesome to return WeChat red envelopes, when using WeChat red envelopes, please be sure to check the information carefully to ensure that each sending is accurate. How to return WeChat red envelope? How to handle the return of WeChat red envelopes The return method of WeChat red envelopes mainly depends on whether the red envelope has been received. If the red envelope has not been received, there are the following handling methods: In case 1, if the other party has not received the red envelope, you can directly communicate with the other party, inform them that the wrong red envelope was sent, and ask the other party not to receive it. more than 24 hours

See all articles