Fenster-API des PHP-Alipay-Entwicklungsdienstes

*文
Freigeben: 2023-03-18 20:18:02
Original
1425 Leute haben es durchsucht

In diesem Artikel wird hauptsächlich die Entwicklung der PHP-Version der API-Schnittstelle des Alipay-Dienstfensters vorgestellt. Interessierte Freunde können darauf verweisen. Ich hoffe, es hilft allen.

Die Entwicklung der API-Schnittstelle des Alipay-Dienstfensters ist für viele Freunde, die auf der Website aufladen möchten, sehr wichtig. Heute werfen wir einen Blick auf die Entwicklung der PHP-Version des Alipay-Dienstfensters API-Schnittstelle.

Ich habe in den letzten zwei Tagen nichts getan, um auf das Alipay-Servicefenster zuzugreifen. Schauen Sie sich die DEMO von Alipay an. Die Lesbarkeit ist nicht sehr gut und erschwert eine einfache Entwicklung. Deshalb habe ich es einfach auf der Grundlage der bereitgestellten API entwickelt. Wenn Sie interessiert sind, können Sie den Code unten veröffentlichen und aufschreiben, wie Sie ihn verwenden.

<?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 ); 
 } 
}
Nach dem Login kopieren

Okay, das Obige ist ein Beispiel für die Entwicklung der Alipay-Servicefenster-API-Schnittstelle, die vom Herausgeber für Sie zusammengestellt wurde. Voraussetzung dafür ist, dass wir eine Genehmigung beantragen müssen Der Herausgeber wird es nicht vorstellen.

Verwandte Empfehlungen:

Zusammenfassung der Alipay-Zahlungsschnittstelle von PHP

Kurze Erläuterung der PHP-Alipay-Schnittstellenbeispiele

PHP implementiert die RSA-Signaturgenerierungsauftragsfunktion am Beispiel von Alipay

Das obige ist der detaillierte Inhalt vonFenster-API des PHP-Alipay-Entwicklungsdienstes. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!