OpenCart添加第三方及自定义Library库

加入Library库

如添加网络库AppBlogHttp和RSA库AppBlogRsa

system/library/apollo/AppBlogHttp.php

class AppBlogHttp
{
    public static function request_post($url = '', $params = '', array $headers = array())
    {
        if (empty($url) || empty($params)) {
            return '{}';
        }

        $header = array(
            "Accept: application/json",
            "Content-Type: application/json",
        );

        if (!empty($headers)) {
            foreach ($headers as $key => $val) {
                $header[] = $key . ': ' . $val;
            }
        }

        $jsonData = json_encode($params);
        $postUrl = $url;
        $ch = curl_init();  //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);
        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        $data = curl_exec($ch);  //运行curl
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $response = array();

        $response['httpCode'] = $httpCode;
        //$response['data'] = json_decode($data);
        $response['data'] = $data;

        //return json_encode($response, JSON_UNESCAPED_SLASHES);
        return $response;
    }

    public static function request_get($url = '', $params = '')
    {
        if (empty($url) || empty($params)) {
            return '{}';
        }

        $header = array(
            "Accept: application/json",
            //"Content-Type: application/json",
        );

        $urlData = createLinkstring($params);
        $getUrl = $url . '?' . $urlData;

        $ch = curl_init();  //初始化curl
        curl_setopt($ch, CURLOPT_URL, $getUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);  //运行curl
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $response = array();

        $response['httpCode'] = $httpCode;
        //$response['data'] = json_decode($data);
        $response['data'] = $data;

        //return json_encode($response, JSON_UNESCAPED_SLASHES);
        return $response;
    }
}

system/library/appblog/AppBlogRsa.php

class AppBlogRsa
{
    /**
     * RSA签名
     * $data签名数据(需要先排序,然后拼接)
     * 签名私钥,必须是没有经过pkcs8转换的私钥
     * 最后的签名,需要用base64编码
     * return Sign签名
     */
    public static function Rsasign($data, $priKey, $sign_type=OPENSSL_ALGO_SHA1)
    {
        //$priKey = file_get_contents('../key/rsa_private_key.pem');
        //转换为openssl密钥,必须是没有经过pkcs8转换的私钥
        $res = openssl_get_privatekey($priKey);

        //调用openssl内置签名方法,生成签名$sign
        //openssl_sign($data, $sign, $res, OPENSSL_ALGO_MD5);
        //openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA1);
        openssl_sign($data, $sign, $res, $sign_type);

        //释放资源
        openssl_free_key($res);

        //base64编码
        $sign = base64_encode($sign);

        return $sign;
    }

    /**RSA验签
     * $data待签名数据(需要先排序,然后拼接)
     * $sign需要验签的签名,需要base64_decode解码
     * 验签公钥
     * return 验签是否通过 bool值
     */
    public static function Rsaverify($data, $sign, $pubKey, $sign_type=OPENSSL_ALGO_SHA1)
    {
        //读取连连支付公钥文件
        //$pubKey = file_get_contents('../key/rsa_public_key.pem');

        //转换为openssl格式密钥
        $res = openssl_get_publickey($pubKey);

        //调用openssl内置方法验签,返回bool值
        $result = (bool) openssl_verify($data, base64_decode($sign), $res, $sign_type);

        //释放资源
        openssl_free_key($res);

        //返回资源是否成功
        return $result;
    }
}

引用Library库

system/startup.php

require_once(DIR_SYSTEM . 'library/appblog/AppBlogCore.php');
require_once(DIR_SYSTEM . 'library/appblog/AppBlogRsa.php');
require_once(DIR_SYSTEM . 'library/appblog/AppBlogHttp.php');

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/opencart-add-third-party-and-custom-library/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
OpenCart添加第三方及自定义Library库
加入Library库 如添加网络库AppBlogHttp和RSA库AppBlogRsa system/library/apollo/AppBlogHttp.php class AppBlogHttp { public static function request……
<<上一篇
下一篇>>
文章目录
关闭
目 录