阿里云 官网文档 (必看几遍)
https://help.aliyun.com/document\_detail/58646.html?spm=a2c4g.11186623.6.547.7c2b7556jv2l3F
如果你的是网页版或者是微信小程序 实用的认证计划:RPH5BioOnly 和 RPMin
这次我用的是 RPMin
筹备工作
1.须要要先去设置场景 依照以下步骤执行
https://help.aliyun.com/document\_detail/59975.html?spm=a2c4g.11186623.6.549.596d7556enMgP2
认证计划 请抉择 RPMin 实用 网页和 小程序 公众号
场景标识就是当前须要用到的 $biz 请先记得
2.获取AccessKey 。这个是为了平安起见创立子用户 。能够参考以下的
https://help.aliyun.com/document\_detail/63821.html?spm=a2c4g.11186623.6.554.6d5178a2ZYrAYY
走完这个你会失去 Access Key ID 和 Access Key Secret 请肯定妥善保存 丢了就没有了
3.俗话说 : 兵草未动,粮草先行。先去买个测试流量包。收费的100条 。
https://common-buy.aliyun.com/?commodityCode=cloudauthflowbag
4.提交认证材料(须要先理解 前面我用到了我再说)
https://help.aliyun.com/document\_detail/58176.html?spm=a2c4g.11186623.6.560.5e2f7b98SbkCcs
5.查问认证状态: 重点 须要与以后认证工作在GetVerifyToken时的认证ID保持一致。
就是这个id 这个是带 ‘{ }’ 这个括号肯定要带上哈 切记
https://help.aliyun.com/document\_detail/57049.html?spm=a2c4g.11186623.6.558.6b8a2daeqf2otb
好 以上只是废话 code才是要害
GitHub,引入 aliyun-php-sdk-core和 aliyun-php-sdk-cloudauth。
阐明 两个SDK 都必须引入。其中 aliyun-php-sdk-core为阿里云的外围SDK, aliyun-php-sdk-cloudauth为实人认证的SDK。
https://github.com/aliyun/aliyun-openapi-php-sdk/tree/c970186e99d4b055701acaade81e229a984510c4?spm=a2c4g.11186623.2.19.389d2794ymWUG2
RPMin认证计划示例
<?phpinclude_once './aliyun-php-sdk-core/Config.php';include_once 'Guid.php'; //参见https://help.aliyun.com/document_detail/64081.html#guiduse Cloudauth\Request\V20180504 as cloudauth; //请以理论目录为准//创立DefaultAcsClient实例并初始化$iClientProfile = DefaultProfile::getProfile( "cn-hangzhou", //默认 "YourAccessKeyID", //您的Access Key ID "YourAccessKeySecret"); //您的Access Key Secret$iClientProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Cloudauth", "cloudauth.aliyuncs.com");$client = new DefaultAcsClient($iClientProfile);$biz = "YourRPMinBiz"; //您在管制台上创立的、采纳RPMin认证计划的认证场景标识, 创立办法:https://help.aliyun.com/document_detail/59975.html$ticketId = guid(); //认证ID, 由应用方指定, 发动不同的认证工作须要更换不同的认证ID$token = null; //认证token, 表白一次认证会话//1. 服务端发动认证申请, 获取到token//GetVerifyToken接口文档:https://help.aliyun.com/document_detail/57050.html$getVerifyTokenRequest = new cloudauth\GetVerifyTokenRequest();$getVerifyTokenRequest->setBiz($biz);$getVerifyTokenRequest->setTicketId($ticketId);try { $response = $client->getAcsResponse($getVerifyTokenRequest); $token = $response->Data->VerifyToken->Token; //token默认30分钟时效,每次发动认证时都必须实时获取} catch (Exception $e) { print $e->getTrace();}//2. 用token提交认证资料//SubmitMaterials接口文档:https://help.aliyun.com/document_detail/58176.html$submitRequest = new cloudauth\SubmitMaterialsRequest();$submitRequest->setVerifyToken($token);//若应用base64上传图片, 须要设置申请办法为POST$submitRequest->setMethod("POST");$identificationNumber = array("MaterialType" => "IdentificationNumber", "Value" => "330110201711110101");$name = array("MaterialType" => "Name", "Value" => "张三");//传入图片材料,请管制单张图片大小在 2M 内,防止拉取超时$facePic = array("MaterialType" => "FacePic", "Value" => "base64://iVBORw0KGgoA..."); //base64形式上传图片, 格局为"base64://图片base64字符串", 以"base64://"结尾且图片base64字符串去掉头部形容(如"data:image/png;base64,"), 并留神管制接口申请的Body在8M以内$idCardFrontPic = array("MaterialType" => "IdCardFrontPic", "Value" => "http://image-demo.img-cn-hangzhou.aliyuncs.com/example.jpg"); //http形式上传图片, 此http地址须可公网拜访$idCardBackPic = array("MaterialType" => "IdCardBackPic", "Value" => "oss://verify-img:715559d76a40774OSS.JPG"); //oss形式上传图片, 此oss文件地址须可公开拜访$verifyMaterials = array($identificationNumber, $name, $facePic, $idCardFrontPic, $idCardBackPic);$submitRequest->setMaterials($verifyMaterials);try { $SubmitMaterialsResponse = $client->getAcsResponse($submitRequest); //因为审核须要工夫,SubmitMaterials接口并不保障同步返回认证后果,可能会返回认证中状态, 此时须要应用GetStatus接口轮询认证后果。 //GetStatus接口文档:https://help.aliyun.com/document_detail/57049.html //$getStatusRequest = new cloudauth\GetStatusRequest(); //$getStatusRequest->setBiz($biz); //$getStatusRequest->setTicketId($ticketId); //$response = $client->getAcsResponse($getStatusRequest); //$statusCode = $response->Data->StatusCode; //后续业务解决} catch (ServerException $e) { print $e->getMessage();} catch (ClientException $e) { print $e->getMessage();}//常见问题:https://help.aliyun.com/document_detail/57640.html
Guid.php
<?phpfunction guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; }}
性能阐明
获取认证状态 我是独自拿进去操作的
//获取认证状态 public function getstate(){// {D9EF9156-B058-F56D-1515-211BF38768B8} 须要取到大括号 $ticketId = $_GET['ticketId']; require APPPATH.'third_party/aliyun-php-sdk-core/Config.php'; require APPPATH.'third_party/Cloudauth/Request/V20180807/GetStatusRequest.php'; include_once 'Guid.php'; //参见https://help.aliyun.com/document_detail/64081.html#guid //创立DefaultAcsClient实例并初始化 $iClientProfile = DefaultProfile::getProfile( "cn-hangzhou", //默认 "xxxxxxxxxxxx", //您的Access Key ID "xxxxxxxxxxxx"); //您的Access Key Secret $iClientProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Cloudauth", "cloudauth.aliyuncs.com"); $biz = "YourRPMinBiz"; //您在管制台上创立的、采纳RPMin认证计划的认证场景标识, 创立办法:https://help.aliyun.com/document_detail/59975.html $getStatusRequest = new GetStatusRequest(); $getStatusRequest->setBiz($biz); $getStatusRequest->setTicketId($ticketId); $client = new DefaultAcsClient($iClientProfile); $response = $client->getAcsResponse($getStatusRequest); $statusCode = $response->Data->StatusCode; echo $statusCode; }