Home > Backend Development > PHP Tutorial > PHP Alipay development service window API

PHP Alipay development service window API

*文
Release: 2023-03-18 20:18:02
Original
1549 people have browsed it

This article mainly introduces the development of the php version of the Alipay service window API interface. Interested friends can refer to it. I hope to be helpful.

The development of the Alipay service window API interface is very important for many friends who want to recharge on the website. Today we will take a look at an example of the development of the PHP version of the Alipay service window API interface.

I have nothing to do in the past two days to access the Alipay service window. Look at Alipay's DEMO. Oh my God, how can I evaluate it? The readability is not very good and it hinders simple development. So I simply developed it based on the API provided. There are still many imperfections in the interface. If you are interested, you can improve it yourself. I will post the code below, and I will write how to use it when I have time.

<?php 
class AlipayService{ 
 /** 
 - 服务接口信息 
 */ 
 public $service = null; 
 /** 
 - 签名信息 
 */ 
 public $sign = null; 
 /** 
 - 签名类型 
 */ 
 public $sign_type = null; 
 /** 
 - 字符集 
 */ 
 public $charset = null; 
 /** 
 - 解析的biz_content数据 
 */ 
 public $request = null; 
 /** 
 - 用户openid 
 */ 
 public $from_user_id = null; 
 /** 
 - 消息类型 
 */ 
 public $msg_type = null; 
 /** 
 - 事件类型 
 */ 
 public $event_type = null; 
 /** 
 - 行为参数 
 */ 
 public $action_param = null; 
 /** 
 - 支付宝用户信息 
 */ 
 public $user_info = null; 
 /** 
 - 文本消息内容 
 */ 
 public $text = null; 
 /** 
 - 图片媒体id 
 */ 
 public $media_id = null; 
 /** 
 - 图片格式 
 */ 
 public $format = null; 
 /** 
 - 是否开启调试 
 */ 
 private $debug = false; 
 /** 
 - 接口类型 
 */ 
 private $interface_type = array( 
  &#39;qrcode&#39; => &#39;alipay.mobile.public.qrcode.create&#39;, 
  &#39;follow&#39; => &#39;alipay.mobile.public.follow.list&#39;, 
  &#39;gis_get&#39; => &#39;alipay.mobile.public.gis.get&#39;, 
  &#39;menu_get&#39; => &#39;alipay.mobile.public.menu.get&#39;,  
  &#39;menu_add&#39; => &#39;alipay.mobile.public.menu.add&#39;, 
  &#39;down_media&#39; => &#39;alipay.mobile.public.multimedia.download&#39;, 
  &#39;menu_update&#39; => &#39;alipay.mobile.public.menu.update&#39;, 
  &#39;info_query&#39; => &#39;alipay.mobile.public.info.query&#39;, 
  &#39;info_modify&#39; => &#39;alipay.mobile.public.info.modify&#39;, 
  &#39;shortlink&#39; => &#39;alipay.mobile.public.shortlink.create&#39;, 
  &#39;label_add&#39; => &#39;alipay.mobile.public.label.add&#39;, 
  &#39;label_del&#39; => &#39;alipay.mobile.public.label.delete&#39;, 
  &#39;label_update&#39; => &#39;alipay.mobile.public.label.update&#39;, 
  &#39;label_query&#39;  => &#39;alipay.mobile.public.label.query&#39;, 
  &#39;label_user_add&#39; => &#39;alipay.mobile.public.label.user.add&#39;, 
  &#39;label_user_del&#39; => &#39;alipay.mobile.public.label.user.delete&#39;, 
  &#39;label_user_query&#39; => &#39;alipay.mobile.public.label.user.query&#39;, 
  &#39;message_custom&#39; => &#39;alipay.mobile.public.message.custom.send&#39;, 
  &#39;message_total&#39; => &#39;alipay.mobile.public.message.total.send&#39;, 
  &#39;message_single&#39; => &#39;alipay.mobile.public.message.single.send&#39;, 
  &#39;message_label_send&#39; => &#39;alipay.mobile.public.message.label.send&#39;, 
 ); 
 /** 
 - 私有密钥地址,替换为你自己的 
 */ 
 private $private_rsa_key_path =&#39;rsa_private_key.pem&#39;; 
 /** 
 - 私有密钥地址,替换为你自己的 
 */ 
 private $public_rsa_key_path =&#39;rsa_public_key.pem&#39;; 
 /** 
 - 支付宝窗的app id 替换成你自己的 
 */ 
 private $app_id = &#39;2015120200901652&#39;; 
 /** 
 - 开启DEBUG参数 
 - @params bool debug true 开启调试 false 关闭调试 
 - @author widuu <admin@widuu.com> 
 */ 
 public function __construct( $debug = false ){ 
 /* 是否开启DEBUG */ 
 if( $debug ) $this->debug = true; 
 } 
 /** 
 - 获取参数,解析请求参数 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function get_request(){ 
 if( !emptyempty($_POST) ){ 
  // 请求的服务接口 
  $this->service = $_POST[&#39;service&#39;]; 
  // 获取请求字符集 
  $this->charset = $_POST[&#39;charset&#39;]; 
  // 获取请求的biz_content 
  $request_biz_content = $_POST[&#39;biz_content&#39;]; 
  // 加密算法 
  $this->sign_type = $_POST[&#39;sign_type&#39;]; 
  // 加密字符串 
  $this->sign = $_POST[&#39;sign&#39;]; 
  // 如果请求格式不是Utf-8 转换格式为Utf-8 
  if( strtolower($this->charset) != &#39;utf-8&#39; ){ 
  $request_biz_content = iconv(&#39;GBK&#39;, &#39;utf-8&#39;, $request_biz_content); 
  } 
  // 解析字符串为xml 
  $request_xml = @simplexml_load_string($request_biz_content, "SimpleXMLElement" , LIBXML_NOCDATA ); 
  // 解析为数组 
  $request_array = json_decode(json_encode($request_xml),true); 
  $this->request = $request_array; 
  /* 解析 */ 
  $this->analysis($request_array); 
  if($this->debug) $this->write_log(&#39;REQUEST_INFO&#39;,var_export($request_array,true)); 
  // 默认验证方法 
  if( $this->service == &#39;alipay.service.check&#39;){ 
  $this->verify($_POST); 
  exit(); 
  } 
  /* 返回结果 */ 
  return $request_array; 
 } 
 } 
 /** 
 - 回复文本内容 
 - @params string content 文本数据 
 - @params bool mass ture为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 public function text($content,$mass=false){ 
 $info[&#39;text&#39;] = array( &#39;content&#39; => $content ); 
 /* 组织内容 */ 
 $biz_content = $this->common_response(&#39;text&#39;,$info,$mass); 
 /* 判断是否为群发 */ 
 if($mass){ 
  $method = &#39;message_total&#39;; 
 }else{ 
  $method = &#39;message_custom&#39;; 
 } 
 $sys_params = $this->common_system($method,$biz_content); 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回结果 结果是JSON数据 */ 
 $result = $this->response_post($sys_params); 
 return $result; 
 } 
 /** 
 - 回复图文内容 
 - @params array articles 拼接的图文消息数组 
 - @params bool mass ture为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 public function articles($articles,$mass=false){ 
 $info[&#39;articles&#39;] = array($articles); 
 /* 组织内容 */ 
 $biz_content = $this->common_response(&#39;image-text&#39;,$info,$mass); 
 /* 判断是否群发 */ 
 if($mass){ 
  $method = &#39;message_total&#39;; 
 }else{ 
  $method = &#39;message_custom&#39;; 
 } 
 /* 加密参数 */ 
 $sys_params = $this->common_system($method,$biz_content); 
 /* 加密字符 */ 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回结果 结果是JSON数据 */ 
 $result = $this->response_post($sys_params); 
 return $result; 
 } 
 /** 
 - 关注事件 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function is_follow(){ 
 $request = $this->request; 
 if( $request[&#39;MsgType&#39;] == &#39;event&#39; && $request[&#39;EventType&#39;] == &#39;follow&#39; ){ 
  return true; 
 }else{ 
  return false; 
 } 
 } 
 /** 
 - 取消关注事件 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function is_unfollow(){ 
 $request = $this->request; 
 if( $request[&#39;MsgType&#39;] == &#39;event&#39; && $request[&#39;EventType&#39;] == &#39;unfollow&#39; ){ 
  return true; 
 }else{ 
  return false; 
 } 
 } 
 /** 
 - 下载用户发来的图片 
 - @param media_id string 图片id 
 - @param filename string 保存图片地址和名称 
 - @author widuu <admin@widuu.com> 
 */ 
 public function down_media($media_id,$filename){ 
 $sys_params = $this->common_system(&#39;down_media&#39;,array(&#39;mediaId&#39;=>$media_id)); 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回数据 */ 
 $result = $this->response_post($sys_params,true); 
 $result = file_put_contents($filename, $result); 
 if( $this->debug ){ 
  $this->write_log(&#39;SAVE_IMAGE&#39;,&#39;保存图片&#39;.(string)$result); 
 } 
 return $result; 
 } 
 /** 
 - (添加|更新|获取)自定义菜单 
 - @param string $type (add|update|get) 
 - @param array $menu 菜单数组,如果是获取菜单可以留空 
 - @author widuu <admin@widuu.com> 
 */ 
 public function menu( $type,$menu = array() ){ 
 if( !in_array( $type, array(&#39;get&#39;,&#39;update&#39;,&#39;add&#39;)) ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;菜单操作方法错误&#39;); 
  } 
  return false; 
 } 
 /* 拼接接口方法 */ 
 $method = &#39;menu_&#39;.$type; 
 $sys_params = $this->common_system($method,$menu); 
 /* 加密字符串 */ 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 请求获取结果 */ 
 $result = $this->response_post($sys_params); 
 /* 转义并解析JSON 数据 */ 
 $menu_json = json_decode(iconv(&#39;GBK&#39;, &#39;utf-8&#39;, $result),true); 
 /* 组织接口信息 */ 
 $interface = &#39;alipay_mobile_public_&#39;.$method.&#39;_response&#39;; 
 /* 遇到错误返回 */ 
 if( $menu_json[$interface][&#39;code&#39;] != 200 ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;GET_MENU_ERROR&#39;,$menu_json[$interface][&#39;msg&#39;]); 
  } 
  return false; 
 } 
 /* 根据类型不同返回不同的结果 */ 
 if( $type == &#39;get&#39; ){ 
  return $menu_json[$interface][&#39;menu_content&#39;]; 
 }else{ 
  return $menu_json[$interface][&#39;msg&#39;]; 
 } 
 } 
 
 /** 
 - POST数据方法 
 - @param array params 参数数组 
 - @author widuu <admin@widuu.com> 
 */ 
 private function response_post($params,$type=false){ 
 // 下载媒体和请求网关 
 if($down){ 
  $url = &#39;https://openfile.alipay.com/chat/multimedia.do&#39;; 
 }else{ 
  $url = &#39;https://openapi.alipay.com/gateway.do&#39;; 
 } 
 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($ch, CURLOPT_POST, 1); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); 
 $curl = curl_exec($ch); 
 curl_close($ch); 
 return $curl; 
 } 
 /** 
 - 拼接回复数据 
 - @param string $type 回复类型 
 - @param array $info 回复内容 
 - @param bool $mass 是否为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 private function common_response($type,$info,$mass=false){ 
 $request = $this->request; 
 $params = array(); 
 // 如果不是群发 
 if( !$mass ) $params[&#39;toUserId&#39;] = $request[&#39;FromUserId&#39;]; 
 $params[&#39;msgType&#39;] = $type; 
 $params[&#39;createTime&#39;] = time(); 
 $content = array_merge($params,$info); 
 return $content; 
 } 
 /** 
 - 拼接加密参数 
 - @param string $interface_type 接口类型 
 - @param array $biz_content 返回biz_content的数组 
 - @author widuu <admin@widuu.com> 
 */ 
 
 private function common_system($interface_type,$biz_content){ 
 /* 接口集合 */ 
 $type = $this->interface_type; 
 $method = $type[$interface_type]; 
 /* 公共参数 */ 
 $params = array ( 
  &#39;method&#39; => $method, 
  &#39;charset&#39; => &#39;UTF-8&#39;, 
  &#39;sign_type&#39; => &#39;RSA&#39;, 
  &#39;app_id&#39; => $this->app_id, 
  &#39;timestamp&#39; => date ( &#39;Y-m-d H:i:s&#39;, time () ), 
  &#39;version&#39;=>&#39;1.0&#39;, 
 ); 
 /* 获取某些接口时没有biz_content参数 */ 
 if( count($biz_content) > 0 ){ 
  $params[&#39;biz_content&#39;] = json_encode($biz_content); 
 } 
 /* 返回系统参数 */ 
 return $params; 
 } 
 /** 
 - 服务验证 
 - @params array params 是自动获的验证信息 
 - @author widuu <admin@widuu.com> 
 */ 
 private function verify($params){ 
 /* 参数为空 */ 
 if( emptyempty($params) ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;验证参数为空&#39;); 
  } 
 } 
 /* 构建参数,使用字典排序再拼接字符串 */ 
 $query_data = $this->build_query($params); 
 /* 验证信息,有可能php版本BUG不支持验证 */ 
 $verify_result = $this->ras_verify($query_data); 
 /* 返回验证结果 */ 
 if( $verify_result ){ 
  /* 取公有密钥的字符串合并为一行 */ 
  $public_rsa_string = file_get_contents($this->public_rsa_key_path); 
  $public_rsa_string = str_replace ( "-----BEGIN PUBLIC KEY-----", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "-----END PUBLIC KEY-----", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "\r", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "\n", "", $public_rsa_string ); 
  /* 构建加密字符串 */ 
  $response_xml = "<success>true</success><biz_content>$public_rsa_string</biz_content>"; 
  /* 生成验证信息 */ 
  $sign = $this->rsa_sign ( $response_xml ); 
  /* 构建返回数据 */ 
  $response = "<?xml version=\"1.0\" encoding=\"GBK\"?><alipay><response>$response_xml</response><sign>$sign</sign><sign_type>RSA</sign_type></alipay>"; 
  if( $this->debug ){ 
  $this->write_log(&#39;CHECK_RESPONSE&#39;,$response); 
  } 
  /* 输出返回信息 */ 
  echo $response; 
  exit(); 
 }else{ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;验证失败&#39;); 
  } 
 } 
 } 
 /** 
 - 拼接为字符串函数 
 - @params array params 拼接函数 
 - @author widuu <admin@widuu.com> 
 */ 
 private function build_query($params){ 
 /* 删除sign字符串 */ 
 unset($params[&#39;sign&#39;]); 
 /* 字典排序 */ 
 ksort($params); 
 /* 拼接 */ 
 $query_array = array(); 
 foreach ($params as $k => $v) { 
  $query_array[] = "$k"."="."$v"; 
 } 
 $query_data = implode("&", $query_array); 
 /* 返回拼接好的字符串 */ 
 return $query_data; 
 } 
 /** 
 - 验证加密sign,有些PHP版本不支持,不支持情况直接返回true 
 - @params string query_data 加密字符串 
 - @author widuu <admin@widuu.com> 
 */ 
 private function ras_verify($query_data){ 
 /* 读取公钥文件,PEM格式 */ 
 $pubKey = file_get_contents($this->public_rsa_key_path); 
 /* 转换为openssl格式密钥 */ 
 $res = openssl_get_publickey($pubKey); 
 /* 调用openssl内置方法验签 */ 
 $result = (bool) openssl_verify($query_data, base64_decode($this->sign), $res); 
 /* 释放资源 */ 
 openssl_free_key($res); 
 /* 有些PHP版本错误,直接返回true */ 
 if( strpos( openssl_error_string(),&#39;PEM_read_bio&#39; ) ){ 
  return true; 
 } 
 /* 返回验签结果 */ 
 return $result; 
 } 
 /** 
 - 通过私有密钥加密数据 
 - @params string data 加密数据 
 - @author widuu <admin@widuu.com> 
 */ 
 private function rsa_sign($data) { 
 /* 读取私钥 */ 
 $priKey = file_get_contents ( $this->private_rsa_key_path ); 
 /* 转换为openssl格式密钥 */ 
 $res = openssl_get_privatekey ( $priKey ); 
 /* 调用openssl 加密 */ 
 openssl_sign ( $data, $sign, $res ); 
 /* 释放资源 */ 
 openssl_free_key ( $res ); 
 /* Base64加密 */ 
 $sign = base64_encode ( $sign ); 
 /* 返回加密参数 */ 
 return $sign; 
 } 
 private function analysis($params){ 
 switch($params[&#39;MsgType&#39;]){ 
  case &#39;image&#39;: 
  $this->media_id = $params[&#39;Image&#39;][&#39;MediaId&#39;]; 
  $this->format = $params[&#39;Image&#39;][&#39;Format&#39;]; 
  break; 
  case &#39;text&#39;: 
  $this->text = $params[&#39;Text&#39;][&#39;Content&#39;]; 
  break; 
  case &#39;event&#39;: 
  $this->event_type = $params[&#39;EventType&#39;]; 
  $this->action_param = $params[&#39;ActionParam&#39;]; 
  break; 
  default: 
  break; 
 } 
 $this->msg_type = $params[&#39;MsgType&#39;]; 
 $this->user_info = json_decode($params[&#39;UserInfo&#39;],true); 
 } 
 /** 
 - DEBUG 为true时的拼接字符串 
 - @param string $level 自定义标识符 
 - @param string $info 自定义内容 
 - @param string $log_path 自定义日志路径 
 - @author widuu <admin@widuu.com> 
 */ 
 public function write_log($level,$info,$log_path = &#39;&#39; ){ 
 if( emptyempty($log_path) ){ //phpfensi.com 
  $log_path = dirname ( __FILE__ ) . "/log.txt"; 
 } 
 file_put_contents($log_path, "[$level]".date ( "Y-m-d H:i:s" ) . " " . $info . "\r\n", FILE_APPEND ); 
 } 
}
Copy after login

Okay, the above is an example of the development of the Alipay service window API interface compiled by the editor for you. The prerequisite for this is that we must apply for a permission. This official can apply to the editor I won’t introduce it.

Related recommendations:

Summary of PHP’s Alipay payment interface

# #PHP Alipay Interface Examples

PHP implements the RSA signature generation order function using Alipay as an example

The above is the detailed content of PHP Alipay development service window API. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template