zoukankan      html  css  js  c++  java
  • php接入友盟智能认证U-Verify一键登录

    php接入友盟+的智能认证实现三网通手机号一键登录

    1.开通阿里云接口服务。(详细文档

    2.注册登录友盟平台,开通智能认证服务。(详细文档

    3. 配置认证方案,进入智能认证-管理控制台,点击“认证管理”,创建认证方案。(详细文档

     

    4.客户端集成,下载对应的SDK

    5.封装服务端UmVerify类:

    <?php
    
    class UmVerify
    {
        // 友盟appkey配置 这里配置的友盟平台appkey,由于我的一个应用分安卓和ios,所以将安卓和ios放到一个数组中,也可以自定义配置,实例化对应的appkey,怎么方便怎么来
        static public $OAppKey = [
            'android' => '',
            'ios' => ''
        ];
    
        //阿里云市场购买应用的appKey
        protected $appKey = '';
    
        //阿里云市场购买应用的appSecret
        protected $appSecret = '';
    
        //友盟的appkey
        protected $um_appkey;
    
        protected $mobile_token;
    
        protected $host = 'https://verify5.market.alicloudapi.com';
    
        protected $path = '/api/v1/mobile/info';
    
        protected $method = 'POST';
    
        protected $accept = 'application/json';
    
        protected $content_type = 'application/json; charset=UTF-8';
    
        public function __construct($data)
        {
            $this->mobile_token = $data['token'];
            $this->um_appkey = $data['key'];
        }
    
        public function info()
        {
            $post_data['token'] = $this->mobile_token;
            //um_appkey为用户在友盟注册的应用分配的appKey
            $curl_url = $this->host . $this->path . "?appkey=" . $this->um_appkey;
            $header = $this->headers();
            $headerArray = $this->headerArray($header);
            return $this->curl($curl_url, $headerArray, $post_data);
        }
    
        protected function headers()
        {
            $header["Accept"] = $this->accept;
            $header["Content-Type"] = $this->content_type;
            $header["X-Ca-Version"] = "1";
            $header["X-Ca-Signature-Headers"] = "X-Ca-Key,X-Ca-Nonce,X-Ca-Stage,X-Ca-Timestamp,X-Ca-Version";
            $header["X-Ca-Stage"] = "RELEASE";
            //请求的阿里云AppKey
            $header["X-Ca-Key"] = $this->appKey;
            $header["X-Ca-Timestamp"] = strval(time() * 1000);
            mt_srand((double)microtime() * 10000);
            $uuid = strtoupper(md5(uniqid(rand(), true)));
            $header["X-Ca-Nonce"] = strval($uuid);
            //Headers
            $headers = "X-Ca-Key:" . $header["X-Ca-Key"] . "
    ";
            $headers .= "X-Ca-Nonce:" . $header["X-Ca-Nonce"] . "
    ";
            $headers .= "X-Ca-Stage:" . $header["X-Ca-Stage"] . "
    ";
            $headers .= "X-Ca-Timestamp:" . $header["X-Ca-Timestamp"] . "
    ";
            $headers .= "X-Ca-Version:" . $header["X-Ca-Version"] . "
    ";
            //um_appkey为用户在友盟注册的应用分配的appKey,token和phoneNumber是app传过来的值
            $url = $this->path . "?appkey=" . $this->um_appkey;
            //sign
            $str_sign = $this->method . "
    ";
            $str_sign .= $this->accept . "
    ";
            $str_sign .= "
    ";
            $str_sign .= $this->content_type . "
    ";
            $str_sign .= "
    ";
            $str_sign .= $headers;
            $str_sign .= $url;
            $sign = base64_encode(hash_hmac('sha256', $str_sign, $this->appSecret, true)); //secret为APP的密钥
            $header['X-Ca-Signature'] = $sign;
            return $header;
        }
    
        protected function headerArray($header)
        {
            $headerArray = [];
            foreach ($header as $k => $v) {
                array_push($headerArray, $k . ":" . $v);
            }
            return $headerArray;
        }
    
        protected function curl($curl_url, $headerArray, $post_data)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->method);
            curl_setopt($curl, CURLOPT_URL, $curl_url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, 10);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_data));
            $result = curl_exec($curl);
            curl_close($curl);
            return json_decode($result, true);
        }
    }

    6.调用UmVerify类

    $data = [
        'token' => $_POST['token'], //token需要客户端传递
        'key' => UmVerify::$OAppKey['ios'] //实例化传递的友盟平台ios的appkey
    ];
    $api = new UmVerify($data);
    $res = $api->info();
    var_dump($res);

     7.api具体返回码参考

  • 相关阅读:
    安装虚拟机及学习linux系统 20155222卢梓杰
    技能获取与编程学习 卢梓杰20155222
    人生第一篇博客
    20155228 2016-2017-2 《Java程序设计》第1周学习总结
    20155228 基于VirtualBox安装Ubuntu和学习linux命令的学习经历和心得
    20155228 获取技能的成功经验和关于C语言学习的调查
    20155228 你期望的师生关系是什么?
    预备作业03:安装虚拟机
    足球运动训练心得及经验分析-c语言学习调查
    我期望的师生关系
  • 原文地址:https://www.cnblogs.com/52lnamp/p/14976946.html
Copyright © 2011-2022 走看看