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"] . "\n";
        $headers .= "X-Ca-Nonce:" . $header["X-Ca-Nonce"] . "\n";
        $headers .= "X-Ca-Stage:" . $header["X-Ca-Stage"] . "\n";
        $headers .= "X-Ca-Timestamp:" . $header["X-Ca-Timestamp"] . "\n";
        $headers .= "X-Ca-Version:" . $header["X-Ca-Version"] . "\n";
        //um_appkey为用户在友盟注册的应用分配的appKey,token和phoneNumber是app传过来的值
        $url = $this->path . "?appkey=" . $this->um_appkey;
        //sign
        $str_sign = $this->method . "\n";
        $str_sign .= $this->accept . "\n";
        $str_sign .= "\n";
        $str_sign .= $this->content_type . "\n";
        $str_sign .= "\n";
        $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具体返回码参考

posted @ 2021-07-06 14:55  我是东山呀  阅读(281)  评论(0编辑  收藏  举报