Moment For Technology

Laravel8.0 file upload image verification and text verification

Posted on Oct. 1, 2023, 4:40 a.m. by Ricky Kerr-Fletcher
Category: The back-end Tag: The back-end

First of all, you need to have a Baidu cloud account and get your API Key and Secret Key

Change your API Key and Secret Key

Finally under the audit method according to their own needs for logical processing

? php /** * Author: csk * Email: [email protected] * Date: 2021/11/5 * Time: 11:50 */ namespace app\api\controller; use app\Request; Public function contentAudit(Request $Request) {$content = $Request - Post ('content'); $token = $this-getAccessToken('ApiKey ', 'SecretKey '); $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token='. $token; $bodys = array( 'text' = $content ); $res = $this-curlPost($url, $bodys); $res = json_decode($res, true); Print_r ($res); die; } public function imageAudit(Request $Request) {$fileTmp = $fileTmp $request-file('image')-getPathname(); * $base_img = str_replace('data:image/ PNG; Base64,', ', 'base64 type file '); * conversion: https://www.cnblogs.com/Renyi-Fan/p/9588306.html * such as: $fileTmp = './static/img/123456789.jpg' */ $token = $this-getAccessToken('ApiKey ', 'SecretKey '); $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token='. $token; $img = file_get_contents($fileTmp); $img = encode($img); $bodys = array( 'image' = $img ); $res = $this-curlPost($url, $bodys); $res = json_decode($res, true); Print_r ($res); } / * * * the CURL of the Post request method * @ param string $* @ url param string $param * @ return a bool | string * / function curlPost ($url = ', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); curl_setopt($curl, CURLOPT_HEADER, 0); Curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); Curl_setopt ($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($curl); $data = curl_exec($curl); curl_close($curl); return $data; } /** * Obtain baidu open platform ticket * reference link: https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu */ public function getAccessToken($ApiKey = '', $SecretKey = '', = 'client_credentials' $grantType) {$url =' https://aip.baidubce.com/oauth/2.0/token '; $post_data['grant_type'] = $grantType; $post_data['client_id'] = $ApiKey; $post_data['client_secret'] = $SecretKey; $o = ""; foreach ($post_data as $k = $v) { $o .= "$k=" . urlencode($v) . ""; } $post_data = substr($o, 0, -1); $res = $this-curlPost($url, $post_data); $res = json_decode($res, true); If (isset($res['error'])) {exit(' error'); } $accessToken = $res['access_token']; return $accessToken; }}Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.