支付宝服务窗API接口开发php版本

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了支付宝服务窗API接口开发php版本脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

支付宝服务窗API接口的开发对于许多网站要充值的朋友来讲是非常的重要的,今天我们就一起来看一篇关于PHP版本的支付宝服务窗API接口的开发例子。

这两天没事要接入支付宝服务窗,看支付宝的DEMO,我的神,我怎么评价好呢?阅读性不是很好,很阻碍简单的开发。所以我就根据提供的API简单的开发了点,接口还有很多不完善,有兴趣的可以自己完善一下,下边我就把代码贴出来,有时间再写如何使用。

<PRe class="brush:PHP;">

class AlipayService{
/**

/**

  • POST数据方法
  • @param array params 参数数组
  • @author widuu admin@widuu.com
    */
    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,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,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,$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;
    }
    /**
  • 拼接加密参数
  • @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 (
'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',);
/ 获取某些接口时没有biz_content参数 /
if( count($biz_content) > 0 ){
$params['biz_content'] = 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('ERROR','验证参数为空');
    }
    }
    /
    构建参数,使用字典排序再拼接字符串 /
    $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 = "true$public_rsa_string";
    /
    生成验证信息 /
    $sign = $this->rsa_sign ( $response_xml );
    /
    构建返回数据 /
    $response = "<?xml version=\"1.0\" encoding=\"GBK\"?>$response_xml$signRSA";
    if( $this->debug ){
    $this->write_log('CHECK_RESPONSE',$response);
    }
    /
    输出返回信息 */
    echo $response;
    exit();
    }else{
    if( $this->debug ){
    $this->write_log('ERROR','验证失败');
    }
    }
    }
    /**
  • 拼接为字符串函数
  • @params array params 拼接函数
  • @author widuu admin@widuu.com
    /
    private function build_query($params){
    /
    删除sign字符串 /
    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;
    }
    /**
  • 验证加密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(),'PEM_read_bio' ) ){
    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['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);
    }
    /**
  • DEBUG 为true时的拼接字符串
  • @param string $level 自定义标识符
  • @param string $info 自定义内容
  • @param string $log_path 自定义日志路径
  • @author widuu admin@widuu.com
    */
    public function write_log($level,$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 . "\r\n",FILE_APPEND );
    }
    }

脚本宝典总结

以上是脚本宝典为你收集整理的支付宝服务窗API接口开发php版本全部内容,希望文章能够帮你解决支付宝服务窗API接口开发php版本所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。