

{"id":331,"date":"2024-06-28T08:18:51","date_gmt":"2024-06-28T00:18:51","guid":{"rendered":"https:\/\/www.01j.top\/?p=331"},"modified":"2024-06-28T08:18:52","modified_gmt":"2024-06-28T00:18:52","slug":"php%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%86%85%e5%ae%b9%e5%ae%89%e5%85%a8%e8%af%86%e5%88%ab%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/www.01j.top\/?p=331","title":{"rendered":"PHP\u5b9e\u73b0\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u63a5\u53e3"},"content":{"rendered":"\n<p>\u5728\u8fdb\u884c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u65f6\uff0c\u7528\u6237\u4e0a\u4f20\u7684\u6587\u5b57\u6216\u56fe\u7247\u53ef\u80fd\u5305\u542b\u975e\u6cd5\u5185\u5bb9\u3002\u4e3a\u4e86\u786e\u4fdd\u5e94\u7528\u7684\u5408\u89c4\u6027\uff0c\u672c\u6587\u5c06\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PHP\u8c03\u7528\u5fae\u4fe1API\u63a5\u53e3\u8fdb\u884c\u5185\u5bb9\u68c0\u6d4b\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u6587\u6863\u7684\u8bf4\u660e\uff0c\u6587\u672c\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u4e91\u8c03\u7528\u548c\u7b2c\u4e09\u65b9\u8c03\u7528\u3002\u7b2c\u4e00\u79cd\u65b9\u5f0f\u662f\u4e91\u5f00\u53d1\uff0c\u521a\u4e0a\u7ebf\u65f6\u51e0\u4e4e\u662f\u514d\u8d39\u7684\uff0c\u76ee\u524d\u4ec5\u63d0\u4f9b\u4e00\u4e2a\u6708\u7684\u514d\u8d39\u914d\u989d\u3002\u5c3d\u7ba1\u4f7f\u7528\u4e91\u51fd\u6570\u8fdb\u884c\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u5b83\u5e76\u975e\u5b8c\u5168\u514d\u8d39\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff0c\u5373\u901a\u8fc7\u7b2c\u4e09\u65b9\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u5b9e\u73b0\u4e5f\u5e76\u4e0d\u590d\u6742\uff0c\u4e3b\u8981\u9700\u8981\u5907\u6848\u57df\u540d\u548cHTTPS\u652f\u6301\u3002\u8ba9\u6211\u4eec\u63a5\u4e0b\u6765\u4e86\u89e3\u4e00\u4e0b\u5982\u4f55\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8fdb\u884c\u5185\u5bb9\u68c0\u6d4b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/cdn.zxki.cn\/tc\/2024\/06\/25\/667a15a64c2af.png\" alt=\"PHP\u5b9e\u73b0\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u63a5\u53e3 \u7b2c4\u5f20\u63d2\u56fe\" width=\"477\" height=\"416\"\/><\/figure>\n\n\n\n<h3 id=\"h3-0\">\u5b98\u65b9\u6587\u6863<\/h3>\n\n\n\n<p>\u5b98\u65b9\u5728\u5f00\u53d1\u6587\u6863\u4e2d\u7684\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u529f\u80fd\u63cf\u8ff0\uff1a\u8be5\u63a5\u53e3\u7528\u4e8e\u68c0\u67e5\u4e00\u6bb5\u6587\u672c\u662f\u5426\u542b\u6709\u8fdd\u6cd5\u8fdd\u89c4\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u5e94\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul><li>\u7528\u6237\u4e2a\u4eba\u8d44\u6599\u4e2d\u7684\u8fdd\u89c4\u6587\u5b57\u68c0\u6d4b\uff1b<\/li><li>\u5a92\u4f53\u65b0\u95fb\u7c7b\u7528\u6237\u53d1\u8868\u6587\u7ae0\u548c\u8bc4\u8bba\u5185\u5bb9\u7684\u68c0\u6d4b\uff1b<\/li><li>\u6e38\u620f\u7c7b\u7528\u6237\u7f16\u8f91\u4e0a\u4f20\u7684\u7d20\u6750\uff08\u4f8b\u5982\u7b54\u9898\u7c7b\u5c0f\u6e38\u620f\u7528\u6237\u4e0a\u4f20\u7684\u95ee\u9898\u53ca\u7b54\u6848\uff09\u7684\u68c0\u6d4b\u7b49\u3002<\/li><li>\u53e6\u5916\uff0c1.0\u7248\u672c\u7684\u63a5\u53e3\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684\u63a5\u53e3\u3002<\/li><\/ul>\n\n\n\n<p>\u7b2c\u4e09\u65b9\u8c03\u7528<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/cdn.zxki.cn\/tc\/2024\/06\/25\/667a1630aaad5.png\"><img src=\"https:\/\/cdn.zxki.cn\/tc\/2024\/06\/25\/667a1630aaad5.png\" alt=\"PHP\u5b9e\u73b0\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u63a5\u53e3 \u7b2c5\u5f20\u63d2\u56fe\" title=\"PHP\u5b9e\u73b0\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5185\u5bb9\u5b89\u5168\u8bc6\u522b\u63a5\u53e3 \u7b2c5\u5f20\u63d2\u56fe\"\/><\/a><\/figure>\n\n\n\n<h3 id=\"h3-1\">PHP\u4ee3\u7801\u5b9e\u73b0<\/h3>\n\n\n\n<p>\u7ed3\u6784\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>config.ini\uff0c\u914d\u7f6e\u6587\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u66f4\u6539\u91cc\u9762\u7684WX_APPID \u3001WX_APPSECRT\uff0c\u9700\u8981\u5728\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u540e\u53f0\u83b7\u53d6\u3002<\/p>\n\n\n\n<p>Verification.php\uff0c\u5c01\u88c5\u4e86\u5fae\u4fe1\u540e\u53f0\u63a5\u53e3\u7684\u8c03\u7528\u548c\u6d88\u606f\u5185\u5bb9\u5b89\u5168\u9a8c\u8bc1\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>api.php\uff0c\u4e3b\u8981\u6765\u63a5\u6536\u5c0f\u7a0b\u5e8f\u63d0\u4ea4\u7684\u68c0\u67e5\u5185\u5bb9\uff0c\u4e0e\u7528\u6237\u7684openid\u3002<\/p>\n\n\n\n<p>config.ini<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>WX_APPID = \"\"\nWX_APPSECRT = \"\"<\/code><\/pre>\n\n\n\n<p>Verification.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nclass Util_Verification\n{\n    private $appid;\n    private $appSecret;\n\n    public function __construct($ini_file) {\n        \/\/ \u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 appid \u548c appsecret\n        $conf = parse_ini_file($ini_file);\n        $this-&gt;appid = $conf&#91;'WX_APPID'];\n        $this-&gt;appSecret = $conf&#91;'WX_APPSECRT'];\n    }\n\n    \/**\n    * \u83b7\u53d6\u5fae\u4fe1 AccessToken\n    *\/\n    public function getAccessToken()\n    {\n        \/\/ \u4ece\u6587\u4ef6\u8bfb\u53d6 AccessToken\n        $accessTokenFile = 'access_token.txt';\n        if (file_exists($accessTokenFile)) {\n            $data = json_decode(file_get_contents($accessTokenFile), true);\n            if ($data&#91;'expire_time'] &gt; time()) {\n                return $data&#91;'access_token'];\n            }\n        }\n        \/\/ \u83b7\u53d6\u65b0\u7684 AccessToken\n        $url = \"https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=\".$this-&gt;appid.\"&amp;secret=\".$this-&gt;appSecret;\n        $info = curl_init();\n        curl_setopt($info, CURLOPT_RETURNTRANSFER, true);\n        curl_setopt($info, CURLOPT_HEADER, 0);\n        curl_setopt($info, CURLOPT_NOBODY, 0);\n        curl_setopt($info, CURLOPT_SSL_VERIFYPEER, false);\n        curl_setopt($info, CURLOPT_SSL_VERIFYHOST, false);\n        curl_setopt($info, CURLOPT_URL, $url);\n        $output = curl_exec($info);\n        curl_close($info);\n\n        $result = json_decode($output, true);\n\n        if (!empty($result&#91;'errcode'])) {\n            return $result&#91;'errmsg'];\n        } else {\n            \/\/ \u4fdd\u5b58\u65b0\u7684 AccessToken \u5230\u6587\u4ef6\n            $accessToken = $result&#91;'access_token'];\n            $data = &#91;\n                'access_token' =&gt; $accessToken,\n                'expire_time' =&gt; time() + 3600 \/\/ \u5047\u8bbe AccessToken \u6709\u6548\u671f\u4e3a1\u5c0f\u65f6\n            ];\n            file_put_contents($accessTokenFile, json_encode($data));\n            return $accessToken;\n        }\n    }\n\n    \/**\n     * \u8fdd\u89c4\u9a8c\u8bc1\n     * @param $content\n     * @param $openid\n     * @param $scene\n     * @param $version\n     * @param null $title\n     * @param null $nickname\n     * @param null $signature\n     * @return array|string\n     *\/\n    public function msgSecCheck($content, $openid, $scene, $version, $title = null, $nickname = null, $signature = null)\n    {\n        $accessToken = $this-&gt;getAccessToken();\n        $url = \"https:\/\/api.weixin.qq.com\/wxa\/msg_sec_check?access_token=\".$accessToken;\n        $postData = &#91;\n            'content' =&gt; $content,\n            'openid' =&gt; $openid,\n            'scene' =&gt; $scene,\n            'version' =&gt; $version,\n            'title' =&gt; $title,\n            'nickname' =&gt; $nickname,\n            'signature' =&gt; $signature\n        ];\n\n        \/\/ \u79fb\u9664\u4e3a\u7a7a\u7684\u53c2\u6570\n        $postData = array_filter($postData);\n\n        $postdata = json_encode($postData, JSON_UNESCAPED_UNICODE);\n        $options = array(\n            'http' =&gt; array(\n                'method' =&gt; 'POST',\n                'header' =&gt; 'Content-type:application\/json',\n                'content' =&gt; $postdata,\n                'timeout' =&gt; 3, \/\/ \u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d:s\uff09\n                'user-Agent' =&gt; \"Mozilla\/5.0 (Windows NT 6.1; rv:21.0) Gecko\/20100101 Firefox\/21.0\\r\\n\" .\n                    \"X-Requested-With: XMLHttpRequest\",\n            )\n        );\n        $context = stream_context_create($options);\n\n        try {\n            $result = file_get_contents($url, false, $context);\n            $result = json_decode($result, true);\n            if (!$result) {\n                throw new Exception(\"\u83b7\u53d6\u4fe1\u606f\u5931\u8d25\uff0c\u8bf7\u91cd\u65b0\u6253\u5f00\u9875\u9762\");\n            }\n            return $result;\n        } catch (Exception $exception) {\n            return $exception-&gt;getMessage();\n        }\n    }\n}\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>api.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nerror_reporting(0);\nheader('Content-Type: application\/json; charset=utf-8');\n\/\/ \u5305\u542b Util_Verification \u7c7b\u6587\u4ef6\nrequire 'Verification.php';\n\n\/\/ \u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\n$ini_file = 'config.ini';\n\n\/\/ \u5b9e\u4f8b\u5316 Util_Verification \u7c7b\n$verification = new Util_Verification($ini_file);\n\n\/\/ \u83b7\u53d6 AccessToken\n$accessToken = $verification-&gt;getAccessToken();\n\/\/echo \"AccessToken: \" . $accessToken . \"\\n\";\n\n\/\/ \u83b7\u53d6 POST \u63d0\u4ea4\u7684\u6570\u636e\n$postdata = file_get_contents(\"php:\/\/input\");\n$request = json_decode($postdata);\n\n\/\/ \u63d0\u53d6\u9700\u8981\u7684\u53c2\u6570\n$content = isset($request-&gt;content) ? $request-&gt;content : \"\";\n$openid = isset($request-&gt;openid) ? $request-&gt;openid : \"\";\n$scene = 1; \/\/ \u8d44\u6599\n$version = 2; \/\/ \u7248\u672c\u56fa\u5b9a\u503c\n\n\/\/ \u53ef\u9009\u7684\u53c2\u6570\n\/\/ $title = isset($request-&gt;title) ? $request-&gt;title : \"\";\n\/\/ $nickname = isset($request-&gt;nickname) ? $request-&gt;nickname : \"\";\n\/\/ $signature = isset($request-&gt;signature) ? $request-&gt;signature : \"\";\n\n\/\/ \u8c03\u7528\u6d88\u606f\u5b89\u5168\u68c0\u67e5\u65b9\u6cd5\n$result = $verification-&gt;msgSecCheck($content, $openid, $scene, $version);\necho json_encode($result);\n?&gt;\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u6211\u4eec\u53ea\u9700\u8981\u914d\u7f6e\u597dconfig.ini\u7684WX_APPID \u3001WX_APPSECRT\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u5b8c\u6210\u5c0f\u7a0b\u5e8f\u90e8\u5206\uff0c\u5373\u53ef\u5b9e\u73b0\u63d0\u4ea4\u6587\u672c\u5185\u5bb9\u8fdb\u884c\u76d1\u6d4b\u4e86\u3002<\/p>\n\n\n\n<p>\u5c0f\u7a0b\u5e8fjs\u53c2\u8003\u90e8\u5206<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wx.request({\n          url: 'https:\/\/www.qq.com\/api.php', \/\/ \u66ff\u6362\u6210\u4f60\u642d\u5efa\u597d\u7684\u63a5\u53e3\u5730\u5740\n          method: 'POST',\n          header: {\n            'Content-Type': 'application\/json'\n          },\n          data: {\n            content: data, \/\/\u9700\u8981\u68c0\u6d4b\u7684\u6587\u672c\u5185\u5bb9\n            openid: openid \/\/\u7528\u6237openid\u552f\u4e00\u8bc6\u522b\u7801\n          },\n          success: function(res) {\n            console.log(res.data);\n            if (res.data.errcode == 44004||res.data.errcode == 0 &amp;&amp;res.data.result.label== 100 ){\n              if (data) {\n                that.Inputsc(data) \/\/\u901a\u8fc7\u68c0\u6d4b\u7684\u903b\u8f91\n                    } else \n                        wx.showToast({\n                        title: \"\u8bf7\u8f93\u5165\u5185\u5bb9\uff0c\u4e0d\u80fd\u4e3a\u7a7a\",\n                        icon: \"none\",\n                        duration: 1000\n                    });\n            }\n            else{\n              that.clear() \/\/\u6e05\u9664\u6587\u672c\n              wx.showModal({\n                  title: '\u60a8\u8f93\u5165\u7684\u6587\u672c\u6d89\u53ca\u654f\u611f\u5185\u5bb9\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff01',\n                  icon: \"none\",\n                  duration: 800\n              }); \n          }\n            \/\/ \u5904\u7406\u6210\u529f\u54cd\u5e94\n          },\n          fail: function(err) {\n            console.error(err);\n            \/\/ \u5904\u7406\u5931\u8d25\u54cd\u5e94\n          }\n        });<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fdb\u884c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u65f6\uff0c\u7528\u6237\u4e0a\u4f20\u7684\u6587\u5b57\u6216\u56fe\u7247\u53ef\u80fd\u5305\u542b\u975e\u6cd5\u5185\u5bb9\u3002\u4e3a\u4e86\u786e\u4fdd\u5e94\u7528\u7684\u5408\u89c4\u6027\uff0c\u672c\u6587\u5c06\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PH [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[59,67],"_links":{"self":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/331"}],"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=331"}],"version-history":[{"count":1,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}