支付宝服务窗API接口的开发对于许多网站要充值的朋友来讲是非常的重要的,今天我们就一起来看一篇关于php版本的支付宝服务窗API接口的开发例子。
这两天没事要接入支付宝服务窗,看支付宝的DEMO,我的神,我怎么评价好呢?阅读性不是很好,很阻碍简单的开发。所以我就根据提供的API简单的开发了点,接口还有很多不完善,有兴趣的可以自己完善一下,下边我就把代码贴出来,有时间再写如何使用。
'alipay.mobile.public.qrcode.create',
'follow' => 'alipay.mobile.public.follow.list',
'gis_get' => 'alipay.mobile.public.gis.get',
'menu_get' => 'alipay.mobile.public.menu.get',
'menu_add' => 'alipay.mobile.public.menu.add',
'down_media' => 'alipay.mobile.public.multimedia.download',
'menu_update' => 'alipay.mobile.public.menu.update',
'info_query' => 'alipay.mobile.public.info.query',
'info_modify' => 'alipay.mobile.public.info.modify',
'shortlink' => 'alipay.mobile.public.shortlink.create',
'label_add' => 'alipay.mobile.public.label.add',
'label_del' => 'alipay.mobile.public.label.delete',
'label_update' => 'alipay.mobile.public.label.update',
'label_query' => 'alipay.mobile.public.label.query',
'label_user_add' => 'alipay.mobile.public.label.user.add',
'label_user_del' => 'alipay.mobile.public.label.user.delete',
'label_user_query' => 'alipay.mobile.public.label.user.query',
'message_custom' => 'alipay.mobile.public.message.custom.send',
'message_total' => 'alipay.mobile.public.message.total.send',
'message_single' => 'alipay.mobile.public.message.single.send',
'message_label_send' => 'alipay.mobile.public.message.label.send',
);
private $private_rsa_key_path ='rsa_private_key.pem';
private $public_rsa_key_path ='rsa_public_key.pem';
private $app_id = '2015120200901652';
public function __construct( $debug = false ){
if( $debug ) $this->debug = true;
}
public function get_request(){
if( !emptyempty($_POST) ){
// 请求的服务接口
$this->service = $_POST['service'];
// 获取请求字符集
$this->charset = $_POST['charset'];
// 获取请求的biz_content
$request_biz_content = $_POST['biz_content'];
// 加密算法
$this->sign_type = $_POST['sign_type'];
// 加密字符串
$this->sign = $_POST['sign'];
// 如果请求格式不是Utf-8 转换格式为Utf-8
if( strtolower($this->charset) != 'utf-8' ){
$request_biz_content = iconv('GBK', 'utf-8', $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('REQUEST_INFO',var_export($request_array,true));
// 默认验证方法
if( $this->service == 'alipay.service.check'){
$this->verify($_POST);
exit();
}
return $request_array;
}
}
public function text($content,$mass=false){
$info['text'] = array( 'content' => $content );
$biz_content = $this->common_response('text',$info,$mass);
if($mass){
$method = 'message_total';
}else{
$method = 'message_custom';
}
$sys_params = $this->common_system($method,$biz_content);
$sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
$result = $this->response_post($sys_params);
return $result;
}
public function articles($articles,$mass=false){
$info['articles'] = array($articles);
$biz_content = $this->common_response('image-text',$info,$mass);
if($mass){
$method = 'message_total';
}else{
$method = 'message_custom';
}
$sys_params = $this->common_system($method,$biz_content);
$sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
$result = $this->response_post($sys_params);
return $result;
}
public function is_follow(){
$request = $this->request;
if( $request['MsgType'] == 'event' && $request['EventType'] == 'follow' ){
return true;
}else{
return false;
}
}
public function is_unfollow(){
$request = $this->request;
if( $request['MsgType'] == 'event' && $request['EventType'] == 'unfollow' ){
return true;
}else{
return false;
}
}
public function down_media($media_id,$filename){
$sys_params = $this->common_system('down_media',array('mediaId'=>$media_id));
$sys_params['sign'] = $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('SAVE_IMAGE','保存图片'.(string)$result);
}
return $result;
}
public function menu( $type,$menu = array() ){
if( !in_array( $type, array('get','update','add')) ){
if( $this->debug ){
$this->write_log('ERROR','菜单操作方法错误');
}
return false;
}
$method = 'menu_'.$type;
$sys_params = $this->common_system($method,$menu);
$sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
$result = $this->response_post($sys_params);
$menu_json = json_decode(iconv('GBK', 'utf-8', $result),true);
$interface = 'alipay_mobile_public_'.$method.'_response';
if( $menu_json[$interface]['code'] != 200 ){
if( $this->debug ){
$this->write_log('GET_MENU_ERROR',$menu_json[$interface]['msg']);
}
return false;
}
if( $type == 'get' ){
return $menu_json[$interface]['menu_content'];
}else{
return $menu_json[$interface]['msg'];
}
}
private function response_post($params,$type=false){
// 下载媒体和请求网关
if($down){
$url = 'https://openfile.alipay.com/chat/multimedia.do';
}else{
$url = 'https://openapi.alipay.com/gateway.do';
}
$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;
}
private function common_response($type,$info,$mass=false){
$request = $this->request;
$params = array();
// 如果不是群发
if( !$mass ) $params['toUserId'] = $request['FromUserId'];
$params['msgType'] = $type;
$params['createTime'] = time();
$content = array_merge($params,$info);
return $content;
}
private function common_system($interface_type,$biz_content){
$type = $this->interface_type;
$method = $type[$interface_type];
$params = array (
'method' => $method,
'charset' => 'UTF-8',
'sign_type' => 'RSA',
'app_id' => $this->app_id,
'timestamp' => date ( 'Y-m-d H:i:s', time () ),
'version'=>'1.0',
);
if( count($biz_content) > 0 ){
$params['biz_content'] = json_encode($biz_content);
}
return $params;
}
private function verify($params){
if( emptyempty($params) ){
if( $this->debug ){
$this->write_log('ERROR','验证参数为空');
}
}
$query_data = $this->build_query($params);
$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 = "true $public_rsa_string ";
$sign = $this->rsa_sign ( $response_xml );
$response = "$response_xml $sign RSA ";
if( $this->debug ){
$this->write_log('CHECK_RESPONSE',$response);
}
echo $response;
exit();
}else{
if( $this->debug ){
$this->write_log('ERROR','验证失败');
}
}
}
private function build_query($params){
unset($params['sign']);
ksort($params);
$query_array = array();
foreach ($params as $k => $v) {
$query_array[] = "$k"."="."$v";
}
$query_data = implode("&", $query_array);
return $query_data;
}
private function ras_verify($query_data){
$pubKey = file_get_contents($this->public_rsa_key_path);
$res = openssl_get_publickey($pubKey);
$result = (bool) openssl_verify($query_data, base64_decode($this->sign), $res);
openssl_free_key($res);
if( strpos( openssl_error_string(),'PEM_read_bio' ) ){
return true;
}
return $result;
}
private function rsa_sign($data) {
$priKey = file_get_contents ( $this->private_rsa_key_path );
$res = openssl_get_privatekey ( $priKey );
openssl_sign ( $data, $sign, $res );
openssl_free_key ( $res );
$sign = base64_encode ( $sign );
return $sign;
}
private function analysis($params){
switch($params['MsgType']){
case 'image':
$this->media_id = $params['Image']['MediaId'];
$this->format = $params['Image']['Format'];
break;
case 'text':
$this->text = $params['Text']['Content'];
break;
case 'event':
$this->event_type = $params['EventType'];
$this->action_param = $params['ActionParam'];
break;
default:
break;
}
$this->msg_type = $params['MsgType'];
$this->user_info = json_decode($params['UserInfo'],true);
}
public function write_log($level,$info,$log_path = '' ){
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 . "rn", FILE_APPEND );
}
}
好了以上就是小编为各位整理的一篇关于支付宝服务窗API接口的开发例子,这个有前提条件的就是我们必须要申请一个权限才可以,这个官方可以申请小编就不介绍。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



