1. 程式人生 > >阿里身份實名認證例項

阿里身份實名認證例項

購買成功後會出現這個介面

程式碼:

//身份證驗證

$authen=Authentication(名字,身份證號);

if ($authen['error_code'] != 0){ //失敗返回

$this->error($authen['reason']);

}

function Authentication($name,$idCard){

$url='http://1.api.apistore.cn/idcard3';

$appCode = '****';//就是圖片中的appcode

//姓名

$params['realName'] = $name;

//身份證號碼

$params['cardNo'] = $idCard;

//傳送遠端請求;

$result = APISTORE($url, $params, $appCode, "POST");

//返回結果

return $result; }

/**
 * APISTORE 獲取資料
 * @param $url 請求地址
 * @param array $params 請求的資料
 * @param $appCode 您的APPCODE
 * @param $method
 * @return array|mixed
 */
function APISTORE($url, $params = array(), $appCode, $method = "GET")
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $method == "POST" ? $url : $url . '?' . http_build_query($params));
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Authorization:APPCODE ' . $appCode
    ));
    //如果是https協議
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        //CURL_SSLVERSION_TLSv1
        curl_setopt($curl, CURLOPT_SSLVERSION, 1);
    }
    //超時時間
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //通過POST方式提交
    if ($method == "POST") {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
    }
    //返回內容
    $callbcak = curl_exec($curl);
    //http status
    $CURLINFO_HTTP_CODE = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    //關閉,釋放資源
    curl_close($curl);
    //如果返回的不是200,請參閱錯誤碼 https://help.aliyun.com/document_detail/43906.html
    if ($CURLINFO_HTTP_CODE == 200)
        return json_decode($callbcak, true);
    else if ($CURLINFO_HTTP_CODE == 403)
        return array("error_code" => $CURLINFO_HTTP_CODE, "reason" => "剩餘次數不足");
    else if ($CURLINFO_HTTP_CODE == 400)
        return array("error_code" => $CURLINFO_HTTP_CODE, "reason" => "APPCODE錯誤");
    else
        return array("error_code" => $CURLINFO_HTTP_CODE, "reason" => "APPCODE錯誤");
}