美文网首页
阿里语音识别和OSS后台token获取

阿里语音识别和OSS后台token获取

作者: 自洽十点 | 来源:发表于2019-06-19 16:10 被阅读0次

    去github上下载阿里phpsdk

    <?php
    header('Access-Control-Allow-Origin:*');
    require_once  './plugin/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
    require_once  './plugin/aliyun-openapi-php-sdk/aliyun-php-sdk-nls-cloud-meta/nls_cloud_meta/Request/V20180518/CreateTokenRequest.php';
    use nls_cloud_meta\Request\V20180518\CreateTokenRequest;
    
    DefaultProfile::addEndpoint(
        "cn-shanghai", 
        "cn-shanghai", 
        "nls-cloud-meta", 
        "nls-meta.cn-shanghai.aliyuncs.com");
    # 创建DefaultAcsClient实例并初始化
    $clientProfile = DefaultProfile::getProfile(
        "cn-",                   # Region ID 
        "",               # 您的 AccessKey ID
        ""            # 您的 AccessKey Secret
    );
    $zero1=date("Y/m/d");
    $myfile = fopen("TokenTime.txt", "r") or die("Unable to open file!");
    $zero2= fread($myfile,filesize("TokenTime.txt"));
    fclose($myfile);
    
    if(strtotime($zero1)>strtotime($zero2)){
    $myfile = fopen("TokenTime.txt", "w") or die("Unable to open file!");
    $txt =date("Y/m/d");
    fwrite($myfile, $txt);
    fclose($myfile);
    $client = new DefaultAcsClient($clientProfile);
    # 创建API请求并设置参数
    $request = new CreateTokenRequest();
    # 发起请求并处理返回
    try {
        $response = $client->getAcsResponse($request);
        $txt1 =$response->Token;
           print_r($txt1);
    
    $myfile1 = fopen("Token.txt", "w") or die("Unable to open file!");
    fwrite($myfile1,json_encode($txt1, JSON_FORCE_OBJECT));
    fclose($myfile1);
    } catch(ServerException $e) {
        print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
    } catch(ClientException $e) {
        print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
    }
    
    }else{
    $myfile1 = fopen("Token.txt", "r") or die("Unable to open file!");
    $token= fread($myfile1,filesize("Token.txt"));
    fclose($myfile1); 
    print_r(json_decode($token));
    }
    
    ?>
    

    阿里oss的使用需要先去后台创建角色和用户,使用的sdk和语音识别一样。

    <?php
    /*
     * 在您使用STS SDK前,请仔细阅读RAM使用指南中的角色管理部分,并阅读STS API文档
     *
     */
     
    include_once './aliyun-php-sdk-core/Config.php';
    include'Sts/Request/V20150401/AssumeRoleRequest.php';
    use Sts\Request\V20150401 as Sts;
    define("REGION_ID", "cn-hangzhou");
    define("ENDPOINT", "sts.cn-hangzhou.aliyuncs.com");
    // 只允许子用户使用角色
    DefaultProfile::addEndpoint(REGION_ID, REGION_ID, "Sts", ENDPOINT);
    $iClientProfile = DefaultProfile::getProfile(REGION_ID, "", "");
    $client = new DefaultAcsClient($iClientProfile);
    // 角色资源描述符,在RAM的控制台的资源详情页上可以获取
    $roleArn = "";
    // 在扮演角色(AssumeRole)时,可以附加一个授权策略,进一步限制角色的权限;
    // 详情请参考《RAM使用指南》
    // 此授权策略表示读取所有OSS的只读权限
    //$policy=<<<POLICY
    //{
    //  "Statement": [
    //    {
    //      "Action": [
    //        "oss:Get*",
    //        "oss:List*"
    //      ],
    //      "Effect": "Allow",
    //      "Resource": "*"
    //    }
    //  ],
    //  "Version": "1"
    //}
    //POLICY;
    $request = new Sts\AssumeRoleRequest();
    // RoleSessionName即临时身份的会话名称,用于区分不同的临时身份
    // 您可以使用您的客户的ID作为会话名称
    $request->setRoleSessionName("client_name");
    $request->setRoleArn($roleArn);
    //$request->setPolicy($policy);
    $request->setDurationSeconds(3600);
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
    } catch(ServerException $e) {
        print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
    } catch(ClientException $e) {
        print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
    }
    ?>
    

    相关文章

      网友评论

          本文标题:阿里语音识别和OSS后台token获取

          本文链接:https://www.haomeiwen.com/subject/hwbkzqtx.html