

{"id":257,"date":"2023-09-19T17:03:04","date_gmt":"2023-09-19T09:03:04","guid":{"rendered":"https:\/\/www.01j.top\/?p=257"},"modified":"2023-09-19T17:03:05","modified_gmt":"2023-09-19T09:03:05","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%92%8cqq%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%9b%be%e7%89%87%e5%ae%89%e5%85%a8%e5%86%85%e5%ae%b9%e6%a3%80%e6%b5%8b%e6%8e%a5%e5%8f%a3%e4%b9%8bthinkphp%e5%ae%9e","status":"publish","type":"post","link":"https:\/\/www.01j.top\/?p=257","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u548cQQ\u5c0f\u7a0b\u5e8f\u56fe\u7247\u5b89\u5168\u5185\u5bb9\u68c0\u6d4b\u63a5\u53e3\u4e4bThinkPHP\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u7531\u4e8e\u76f8\u5173\u7ba1\u63a7\uff0cUGC\u5c0f\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\uff0c\u5fc5\u987b\u8981\u8fc7\u6ee4\u8fdd\u6cd5\u8fdd\u89c4\u5185\u5bb9\uff08\u5982\u9ec4\uff09\u3002<\/p>\n\n\n\n<p>UGC\u5c0f\u7a0b\u5e8f\u5b98\u65b9\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<p>\u5c0f\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u6216\u670d\u52a1\u4e2d\uff0c\u6d89\u53ca\u7528\u6237\u5c06\u81ea\u5df1\u81ea\u5b9a\u4e49\u7f16\u8f91\u7684\u6587\u5b57\u3001\u56fe\u7247\u3001\u97f3\u9891\u3001\u89c6\u9891\u7b49\u5185\u5bb9\u901a\u8fc7\u5c0f\u7a0b\u5e8f\u8fdb\u884c\u5c55\u793a\u6216\u63d0\u4f9b\u7ed9\u5176\u4ed6\u7528\u6237\u7684\uff0c\u5c5e\u4e8eUGC\u5c0f\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p>\u62d2\u5ba1\u6761\u4f8b\uff1a<br>\u5b58\u5728\u5e73\u53f0\u4e3a\u5141\u8bb8\u7684\u670d\u52a1\u5185\u5bb9\uff0c\u8fdd\u53cd\u300a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5e73\u53f0\u8fd0\u8425\u89c4\u8303\u5e38\u89c1\u62d2\u7edd\u60c5\u5f623.2\u300b<\/p>\n\n\n\n<h2 id=\"h2-0\">\u539f\u56e0\uff1a<\/h2>\n\n\n\n<p>\u5c0f\u7a0b\u5e8f\u63d0\u4f9b\u7528\u6237\u56fe\u7247\u5904\u7406\u529f\u80fd\uff0c\u4f46\u662f\u672a\u63a5\u5165\u56fe\u7247\u5b89\u5168\u5185\u5bb9\u68c0\u6d4b\u63a5\u53e3https:\/\/api.weixin.qq.com\/wxa\/img_sec_check<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u4e0d\u8981\u5c1d\u8bd5\u505a\u5047\u8c03\u7528\uff0c\u5ba1\u6838\u4eba\u5458\u771f\u7684\u4f1a\u7528\u6709\u989c\u8272\u7684\u56fe\u7247\u6765\u6d4b\u8bd5\uff01\uff01\uff01<\/p>\n\n\n\n<h2 id=\"h2-1\">\u89e3\u51b3\u65b9\u6848\uff1a<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<p><br>\u5c1d\u8bd5\u8fc7\u5c0f\u7a0b\u5e8f\u4e91\u5f00\u53d1\uff0c\u4f46\u662f\u4e91\u51fd\u6570\u5927\u5c0f\u6709\u9650\u5236\u592a\u6b7b\uff0c\u884c\u4e0d\u901a\u3002\u6539\u81ea\u5df1\u670d\u52a1\u5668\u7528php\u5b9e\u73b0\u5b89\u5168\u6821\u9a8c\u63a5\u53e3\u3002\u7528\u7684\u662fThinkPHP\uff0c\u4f60\u4eec\u81ea\u884c\u8c03\u6574\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u548cQQ\u5c0f\u7a0b\u5e8f\u7684\u5b89\u5168\u68c0\u6d4b\u63a5\u53e3\u601d\u8def\u548c\u4ee3\u7801\u5dee\u4e0d\u591a\u4e00\u6b21\uff0c\u4f46\u662f\u4e3a\u4e86\u5927\u5bb6\u65b9\u4fbf\u53c2\u8003\u6bd4\u5bf9\u548cCopy\uff0c\u5c31\u5168\u8d34\u51fa\u6765\u4e86\u3002<br><\/p>\n<\/div><\/div>\n\n\n\n<h2 id=\"h2-2\">\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef\u76f8\u5173\u4ee3\u7801<\/h2>\n\n\n\n<h2 id=\"h2-3\">\u56fe\u7247\u654f\u611f\u68c0\u6d4b\u63a5\u53e3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u56fe\u7247\u654f\u611f\u68c0\u6d4b\r\n    public function wxImgSecCheck() {\r\n        $access_token = $this->getMiniToken();\r\n        $img = request()->param('img');\r\n        if (empty($img)) {\r\n            return RJson::error(\"\");\r\n        }\r\n        \r\n        $filePath = $this->saveBase64Image($img, 0);\r\n        if (empty($filePath)) {\r\n            return RJson::error(\"\");\r\n        }\r\n        $obj = new \\CURLFile(realpath($filePath));\r\n        $obj->setMimeType(\"image\/jpeg\");\r\n        $file&#91;'media'] = $obj;\r\n        $url = \"https:\/\/api.weixin.qq.com\/wxa\/img_sec_check?access_token=\".$access_token;\r\n        $info = $this->httpRequest($url,$file);\r\n        return RJson::success($info);\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-4\">\u83b7\u53d6access_token<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u83b7\u53d6access_token\r\n\tprivate function getMiniToken() {\r\n        $cacheKey = 'miniRcubeAccessToken';\r\n        $access_token = Cache::get($cacheKey); \r\n        if (empty($token)) {\r\n            $appid = \"\u4f60\u7684APPID\";\r\n            $secret = \"\u4f60\u7684SECRET\";\r\n            $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\" . $appid . \"&amp;secret=\" . $secret;\r\n            $jsonResult = file_get_contents($url);\r\n            $resultArray = json_decode($jsonResult, true);\r\n            $access_token = $resultArray&#91;\"access_token\"];\r\n            Cache::set($cacheKey,$access_token,180);\r\n        }\r\n        return $access_token;\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-5\">QQ\u5c0f\u7a0b\u5e8f\u670d\u52a1\u7aef\u76f8\u5173\u4ee3\u7801<\/h2>\n\n\n\n<h2 id=\"h2-6\">\u56fe\u7247\u654f\u611f\u68c0\u6d4b\u63a5\u53e3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/QQ\u5c0f\u7a0b\u5e8f\u56fe\u7247\u654f\u611f\u68c0\u6d4b\r\n    public function qqImgSecCheck() {\r\n        $access_token = $this->getQQMiniToken();\r\n        $img = request()->param('img');\r\n        if (empty($img)) {\r\n            return RJson::error(\"\");\r\n        }\r\n       \t\r\n        $filePath = $this->saveBase64Image($img, 1);\r\n        if (empty($filePath)) {\r\n            return RJson::error(\"\");\r\n        }\r\n        $obj = new \\CURLFile(realpath($filePath));\r\n        $obj->setMimeType(\"image\/jpeg\");\r\n        $file&#91;'media'] = $obj;\r\n        $url = \"https:\/\/api.q.qq.com\/api\/json\/security\/ImgSecCheck?access_token=\".$access_token;\r\n        $info = $this->httpRequest($url,$file);\r\n        return RJson::success($info);\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-7\">\u83b7\u53d6access_token<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/QQ\u5c0f\u7a0b\u5e8f\u83b7\u53d6access_token\r\n\tprivate function getQQMiniToken() {\r\n        $cacheKey = 'miniQQRcubeAccessToken';\r\n        $access_token = Cache::get($cacheKey); \r\n        if (empty($token)) {\r\n            $appid = \"\u4f60\u7684APPID\";\r\n            $secret = \"\u4f60\u7684SECRET\";\r\n            $url = \"https:\/\/api.q.qq.com\/api\/getToken?grant_type=client_credential&amp;appid=\" . $appid . \"&amp;secret=\" . $secret;\r\n            $jsonResult = file_get_contents($url);\r\n            $resultArray = json_decode($jsonResult, true);\r\n            $access_token = $resultArray&#91;\"access_token\"];\r\n            Cache::set($cacheKey,$access_token,180);\r\n        }\r\n        return $access_token;\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-8\">\u4e3b\u8981\u4ee3\u7801\u5230\u8fd9\u91cc\u5c31\u5b8c\u4e86\uff0c\u4e0b\u9762\u8865\u5145\u7528\u5230\u7684\u79c1\u6709\u65b9\u6cd5\u548c\u7c7b\uff1a<\/h2>\n\n\n\n<h2 id=\"h2-9\">HTTP\u8bf7\u6c42\uff08\u652f\u6301HTTP\/HTTPS\uff0c\u652f\u6301GET\/POST\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/HTTP\u8bf7\u6c42\uff08\u652f\u6301HTTP\/HTTPS\uff0c\u652f\u6301GET\/POST\uff09\r\n    private function httpRequest($url, $data = null)\r\n    {\r\n        $curl = curl_init();\r\n        curl_setopt($curl, CURLOPT_URL, $url);\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\r\n        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);\r\n\r\n        if (!empty($data)) {\r\n            curl_setopt($curl, CURLOPT_POST, TRUE);\r\n            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);\r\n        }\r\n        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);\r\n        $output = curl_exec($curl);\r\n        curl_close($curl);\r\n        return $output;\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-10\">\u4fdd\u5b58Base64\u56fe\u7247<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate function saveBase64Image($base64_image, $ptype) {\r\n        \/\/\u4fdd\u5b58\u4f4d\u7f6e--\u56fe\u7247\u540d\r\n        $image_name =date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).\".jpg\";\r\n        $group_name = \"_\" . date('Ymd');\r\n        $group_path = ROOT_PATH . 'public' . DS . 'static' . DS . 'jjfly'. DS . $group_name;\r\n        $image_path = $group_path .'\/'.$image_name;\r\n        $http_path = \"https:\/\/www.iftangtang.com\/static\/jjfly\/\" . $group_name . '\/'.$image_name;\r\n        if (!file_exists($group_path)) {\r\n            mkdir ($group_path,0777,true);\r\n        }\r\n            \r\n        \/\/\u89e3\u7801\r\n        $base64_image = str_replace (\" \",\"+\", $base64_image);\r\n        $base64_image = str_replace('data:image\/jpg;base64,', '', $base64_image);\r\n        $base64_image = str_replace('\\n', '', $base64_image);\r\n        $base64_image = str_replace('\\t', '', $base64_image);\r\n        \r\n        $decode = base64_decode($base64_image);\r\n        if (file_put_contents($image_path, $decode)){\r\n            return $image_path;\r\n        }\r\n        return false;\r\n    }\r\n<\/code><\/pre>\n\n\n\n<h2 id=\"h2-11\">\u7b80\u6613\u5c01\u88c5\u63a5\u53e3\u7528\u5230\u7684Json\u8fd4\u56de\u7c7b\uff1aRJson.php<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nclass RJson\r\n{\r\n    \/*\r\n     * \u72b6\u6001\u7801code\r\n     * 200\u6210\u529f\uff0c201\u5931\u8d25\r\n     * 1001\u672a\u767b\u5f55\u6216\u767b\u5f55\u4fe1\u606f\u8fc7\u671f,1002\u4e3aaccess_toke\u8fc7\u671f\r\n     * *\/\r\n    \/\/\u8bf7\u6c42\u6210\u529f\u8fd4\u56dejson\u683c\u5f0f\u6570\u636e\r\n    public static function success($data='',$msg='\u64cd\u4f5c\u6210\u529f',$code=200){\r\n        $result = &#91;\r\n            'data' => $data,\r\n            'msg' => $msg,\r\n            'code' => $code\r\n        ];\r\n\r\n        return json_encode($result, JSON_UNESCAPED_UNICODE);\r\n    }\r\n    \/\/\u8bf7\u6c42\u5931\u8d25\u8fd4\u56dejson\u683c\u5f0f\u6570\u636e\r\n    public static function error($msg='\u64cd\u4f5c\u5931\u8d25',$code=201,$data=''){\r\n        $result = &#91;\r\n            'data' => $data,\r\n            'msg' => $msg,\r\n            'code' => $code\r\n        ];\r\n\r\n        return json_encode($result, JSON_UNESCAPED_UNICODE);\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8e\u76f8\u5173\u7ba1\u63a7\uff0cUGC\u5c0f\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\uff0c\u5fc5\u987b\u8981\u8fc7\u6ee4\u8fdd\u6cd5\u8fdd\u89c4\u5185\u5bb9\uff08\u5982\u9ec4\uff09\u3002 UGC\u5c0f\u7a0b\u5e8f\u5b98\u65b9\u5b9a\u4e49\uff1a \u5c0f\u7a0b\u5e8f\u4e2d\u7684\u529f\u80fd\u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[42],"tags":[],"_links":{"self":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/257"}],"collection":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":1,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}