{"id":618,"date":"2023-02-26T11:00:05","date_gmt":"2023-02-26T03:00:05","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=618"},"modified":"2023-04-29T20:18:24","modified_gmt":"2023-04-29T12:18:24","slug":"opencart-add-third-party-and-custom-library","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/opencart-add-third-party-and-custom-library\/","title":{"rendered":"OpenCart\u6dfb\u52a0\u7b2c\u4e09\u65b9\u53ca\u81ea\u5b9a\u4e49Library\u5e93"},"content":{"rendered":"<h2>\u52a0\u5165Library\u5e93<\/h2>\n<p>\u5982\u6dfb\u52a0\u7f51\u7edc\u5e93AppBlogHttp\u548cRSA\u5e93AppBlogRsa<\/p>\n<p>system\/library\/apollo\/AppBlogHttp.php<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">class AppBlogHttp\n{\n    public static function request_post($url = &#039;&#039;, $params = &#039;&#039;, array $headers = array())\n    {\n        if (empty($url) || empty($params)) {\n            return &#039;{}&#039;;\n        }\n\n        $header = array(\n            &quot;Accept: application\/json&quot;,\n            &quot;Content-Type: application\/json&quot;,\n        );\n\n        if (!empty($headers)) {\n            foreach ($headers as $key =&gt; $val) {\n                $header[] = $key . &#039;: &#039; . $val;\n            }\n        }\n\n        $jsonData = json_encode($params);\n        $postUrl = $url;\n        $ch = curl_init();  \/\/\u521d\u59cb\u5316curl\n        curl_setopt($ch, CURLOPT_URL, $postUrl);\n        \/\/curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;POST&quot;);\n        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        curl_setopt($ch, CURLOPT_POST, true);\n        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);\n        $data = curl_exec($ch);  \/\/\u8fd0\u884ccurl\n        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        curl_close($ch);\n        $response = array();\n\n        $response[&#039;httpCode&#039;] = $httpCode;\n        \/\/$response[&#039;data&#039;] = json_decode($data);\n        $response[&#039;data&#039;] = $data;\n\n        \/\/return json_encode($response, JSON_UNESCAPED_SLASHES);\n        return $response;\n    }\n\n    public static function request_get($url = &#039;&#039;, $params = &#039;&#039;)\n    {\n        if (empty($url) || empty($params)) {\n            return &#039;{}&#039;;\n        }\n\n        $header = array(\n            &quot;Accept: application\/json&quot;,\n            \/\/&quot;Content-Type: application\/json&quot;,\n        );\n\n        $urlData = createLinkstring($params);\n        $getUrl = $url . &#039;?&#039; . $urlData;\n\n        $ch = curl_init();  \/\/\u521d\u59cb\u5316curl\n        curl_setopt($ch, CURLOPT_URL, $getUrl);\n        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        $data = curl_exec($ch);  \/\/\u8fd0\u884ccurl\n        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        curl_close($ch);\n        $response = array();\n\n        $response[&#039;httpCode&#039;] = $httpCode;\n        \/\/$response[&#039;data&#039;] = json_decode($data);\n        $response[&#039;data&#039;] = $data;\n\n        \/\/return json_encode($response, JSON_UNESCAPED_SLASHES);\n        return $response;\n    }\n}<\/code><\/pre>\n<p>system\/library\/appblog\/AppBlogRsa.php<\/p>\n<pre><code class=\"language-php\">class AppBlogRsa\n{\n    \/**\n     * RSA\u7b7e\u540d\n     * $data\u7b7e\u540d\u6570\u636e(\u9700\u8981\u5148\u6392\u5e8f\uff0c\u7136\u540e\u62fc\u63a5)\n     * \u7b7e\u540d\u79c1\u94a5\uff0c\u5fc5\u987b\u662f\u6ca1\u6709\u7ecf\u8fc7pkcs8\u8f6c\u6362\u7684\u79c1\u94a5\n     * \u6700\u540e\u7684\u7b7e\u540d\uff0c\u9700\u8981\u7528base64\u7f16\u7801\n     * return Sign\u7b7e\u540d\n     *\/\n    public static function Rsasign($data, $priKey, $sign_type=OPENSSL_ALGO_SHA1)\n    {\n        \/\/$priKey = file_get_contents(&#039;..\/key\/rsa_private_key.pem&#039;);\n        \/\/\u8f6c\u6362\u4e3aopenssl\u5bc6\u94a5\uff0c\u5fc5\u987b\u662f\u6ca1\u6709\u7ecf\u8fc7pkcs8\u8f6c\u6362\u7684\u79c1\u94a5\n        $res = openssl_get_privatekey($priKey);\n\n        \/\/\u8c03\u7528openssl\u5185\u7f6e\u7b7e\u540d\u65b9\u6cd5\uff0c\u751f\u6210\u7b7e\u540d$sign\n        \/\/openssl_sign($data, $sign, $res, OPENSSL_ALGO_MD5);\n        \/\/openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA1);\n        openssl_sign($data, $sign, $res, $sign_type);\n\n        \/\/\u91ca\u653e\u8d44\u6e90\n        openssl_free_key($res);\n\n        \/\/base64\u7f16\u7801\n        $sign = base64_encode($sign);\n\n        return $sign;\n    }\n\n    \/**RSA\u9a8c\u7b7e\n     * $data\u5f85\u7b7e\u540d\u6570\u636e(\u9700\u8981\u5148\u6392\u5e8f\uff0c\u7136\u540e\u62fc\u63a5)\n     * $sign\u9700\u8981\u9a8c\u7b7e\u7684\u7b7e\u540d,\u9700\u8981base64_decode\u89e3\u7801\n     * \u9a8c\u7b7e\u516c\u94a5\n     * return \u9a8c\u7b7e\u662f\u5426\u901a\u8fc7 bool\u503c\n     *\/\n    public static function Rsaverify($data, $sign, $pubKey, $sign_type=OPENSSL_ALGO_SHA1)\n    {\n        \/\/\u8bfb\u53d6\u8fde\u8fde\u652f\u4ed8\u516c\u94a5\u6587\u4ef6\n        \/\/$pubKey = file_get_contents(&#039;..\/key\/rsa_public_key.pem&#039;);\n\n        \/\/\u8f6c\u6362\u4e3aopenssl\u683c\u5f0f\u5bc6\u94a5\n        $res = openssl_get_publickey($pubKey);\n\n        \/\/\u8c03\u7528openssl\u5185\u7f6e\u65b9\u6cd5\u9a8c\u7b7e\uff0c\u8fd4\u56debool\u503c\n        $result = (bool) openssl_verify($data, base64_decode($sign), $res, $sign_type);\n\n        \/\/\u91ca\u653e\u8d44\u6e90\n        openssl_free_key($res);\n\n        \/\/\u8fd4\u56de\u8d44\u6e90\u662f\u5426\u6210\u529f\n        return $result;\n    }\n}<\/code><\/pre>\n<h2>\u5f15\u7528Library\u5e93<\/h2>\n<p>system\/startup.php<\/p>\n<pre><code class=\"language-php\">require_once(DIR_SYSTEM . &#039;library\/appblog\/AppBlogCore.php&#039;);\nrequire_once(DIR_SYSTEM . &#039;library\/appblog\/AppBlogRsa.php&#039;);\nrequire_once(DIR_SYSTEM . &#039;library\/appblog\/AppBlogHttp.php&#039;);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a0\u5165Library\u5e93 \u5982\u6dfb\u52a0\u7f51\u7edc\u5e93AppBlogHttp\u548cRSA\u5e93AppBlogRsa system\/libr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[173],"tags":[],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-opencart"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=618"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}