https://www.whmblog.cn/php/94.html
if($edit['user_identity_type'] == 1){
header("Content-type:text/html;charset=utf-8");
$key = 'd8aadc41573b0057507a8ff314dd241f';
$base64_img = $this->Base64EncodeImage('.'.mb_substr($edit['user_identity_face_logo'], 44));//根据服务器传入图片地址
$side = 'front';
$arr = explode(',', $base64_img);
$base64_img = $arr[1];
//var_dump($base64_img);exit;
$sendUrl = 'http://apis.juhe.cn/idimage/verify'; //短信接口的URL
$smsConf = array(
'key' => $key, //您申请的APPKEY
'image' => $base64_img, //图片base_64
'side' => $side //身份证正反面
);
$content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
$res = $result['result'];
if($error_code == 0 && $res['realname'] == $edit['user_realname'] && $res['idcard'] == $edit['user_identity_card']){
//实名认证成功
$edit['user_identity_statu'] = 2;
}else{
$edit['user_identity_statu'] = 0;
return $this->data->addBody(-140, $res, __('认证失败'), 250);
//状态非0,说明失败
}
}else{
//返回内容异常,以下可根据业务逻辑自行修改
return $this->data->addBody(-140, array(), __('请求认证失败'), 250);
}
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172
Safari/537.22' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
/**
* 图片转base64
* @param ImageFile String 图片路径
* @return 转为base64的图片
*/
function Base64EncodeImage($ImageFile) {
if(file_exists($ImageFile) || is_file($ImageFile)){
$base64_image = '';
$image_info = getimagesize($ImageFile);
$image_data = fread(fopen($ImageFile, 'r'), filesize($ImageFile));
$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return $base64_image;
}
else{
return false;
}
}