php阿里云滑块验证

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wen_3370/article/details/84965075

阿里云文档https://help.aliyun.com/document_detail/66353.html?spm=a2c4g.11186623.6.585.580d6556TufgWb

自己用的laravel框架 所以用composer安装

https://packagist.org/去搜索框架所需要的包

<?php
namespace App\Service;
use DefaultAcsClient;
use DefaultProfile;
use afs\Request\V20180112 as Afs;

/**
 * Class Aliyun
 * @package App\Service
 * @author w
 */
class Aliyun{

    /**
     * accesskeyid
     */
    private $accesskeyId;
    /**
     * secret
     */
    private $secret;
    /**
     * appkey
     */
    private $appkey;

    /**
     * 初始化配置
     */
    public function __construct()
    {
        $this->accesskeyId = config('aliyun.accesskey_id');
        $this->secret = config('aliyun.secret');
        $this->appkey = config('aliyun.appkey');
    }

    /**
     * @param $sessionId
     * @param $token
     * @param $sig
     * @param $scene
     * @param $ip
     * @return bool|string
     * 滑块验证
     */
    public function validALiYun($sessionId,$token,$sig,$scene,$ip){
        $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $this->accesskeyId, $this->secret);
        $client = new DefaultAcsClient($iClientProfile);
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
        $request = new Afs\AuthenticateSigRequest();
        $request->setSessionId($sessionId);// 必填参数,从前端获取,不可更改
        $request->setToken($token);// 必填参数,从前端获取,不可更改
        $request->setSig($sig);// 必填参数,从前端获取,不可更改
        $request->setScene($scene);// 必填参数,从前端获取,不可更改
        $request->setAppKey($this->appkey);//必填参数,后端填写
        $request->setRemoteIp($ip);//必填参数,后端填写
        try {
            $response = $client->getAcsResponse($request);
            $result = get_object_vars($response);
            if ($result['Code'] == 100) {
                return true;
            } else {
                return false;
            }
        } catch(ServerException $e) {
            return "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
        } catch(ClientException $e) {
            return "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
        }
    }

}

猜你喜欢

转载自blog.csdn.net/wen_3370/article/details/84965075