love_php/app/Services/YDService.php

120 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Services;
class YDService
{
private $secretKey;
private $secretId;
private $lpa_businessId;
public function __construct()
{
$this->secretKey = env('YD_SECRET_KEY');
$this->secretId = env('YD_SECRET_ID');
$this->lpa_businessId = env('YD_LIVE_PERSON_AUDIT_BUSINESS_ID');
}
public function getSignature($params)
{
ksort($params);
$buff="";
foreach($params as $key=>$value){
if($value !== null) {
$buff .=$key;
$buff .=$value;
}
}
$buff .= $this->secretKey;
return md5(mb_convert_encoding($buff, "utf8", "auto"));
}
public function postDataCurl($url,$data,$businessId){
$timeout = 5000;
$data['version'] = 'v1';
$data['secretId'] = $this->secretId;
$data['businessId'] = $businessId;
$data['timestamp'] = time()*1000;
$data['nonce'] = sprintf("%d", rand());
$params = $data;
//不需要做签名字段
unset($params['actionVideos']);
$data["signature"] = $this->getSignature($params);
$http_header = array(
'Content-Type:application/x-www-form-urlencoded;charset=utf-8'
);
$postdataArray = array();
foreach ($data as $key=>$value){
array_push($postdataArray, $key.'='.urlencode($value));
// $postdata.= ($key.'='.urlencode($value).'&');
}
$postdata = join('&', $postdataArray);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_HEADER, false );
curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (false === $result) {
$result = curl_errno($ch);
}
curl_close($ch);
return $this->json_to_array($result) ;
}
//视频人脸核身
public function livePersonAudit($request){
$url = 'https://verify.dun.163.com/v1/liveperson/h5/audit';
$actionVideos = [$request->action_videos];
$data = [
'cardNo' => $request->card_num,
'name' => $request->name,
'videoType' => $request->video_type,
'actions' => '["4"]',
'actionVideos' => json_encode($actionVideos),
'needAvatar' => 'true',
];
$result = $this->postDataCurl($url, $data, $this->lpa_businessId);
if(empty($result)){
return false;
}
return $result;
}
/**
* 将json字符串转化成php数组
* @param $json_str
* @return $json_arr
*/
public function json_to_array($json_str){
if(is_array($json_str) || is_object($json_str)){
$json_str = $json_str;
}else if(is_null(json_decode($json_str))){
$json_str = $json_str;
}else{
$json_str = strval($json_str);
$json_str = json_decode($json_str,true);
}
$json_arr=array();
foreach($json_str as $k=>$w){
if(is_object($w)){
$json_arr[$k]= $this->json_to_array($w); //判断类型是不是object
}else if(is_array($w)){
$json_arr[$k]= $this->json_to_array($w);
}else{
$json_arr[$k]= $w;
}
}
return $json_arr;
}
}