PHP微信V3接口万能代码

万能代码

/**
     * 代码案例
     * @return string
     */
    public function down()
    {
        $url = 'https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2021-10-16&sub_mchid=123123&bill_type=ALL';
        $result = json_decode($this->getCurl($url, '', $this->createWxAuthorization($url)), true);
        if (isset($result['download_url'])) {
            $downurl = $result['download_url'];
            $data = $this->getCurl($downurl, '', $this->createWxAuthorization($downurl));
            var_dump($data);
        }
        return 'hello1';
    }


/**
     * 请求微信接口
     * @param $url 请求微信V3接口地址
     * @param $data post数据
     * @param array $headers 微信V3接口签名
     * @param int $timeout 请求超时时间
     * @param string $method 请求方式
     * @return bool|string
     */
    private function getCurl($url, $data, $headers = [], $timeout = 10, $method = 'GET')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }


    /**
     *  生成微信V3接口签名
     * @param $url 请求微信V3接口地址
     * @param string $body POST请求时 需要 转JSON字符串
     * @param string $method 请求方式
     * @return string[] 微信V3接口签名 Authorization的值
     */
    public function createWxAuthorization($url, $body = '', $method = 'GET')
    {
        if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
            throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
        }
        $urlParts = parse_url($url);
        $canonical_url = ($urlParts['path'] . (!empty($urlParts['query']) ? "?${$urlParts['query']}" : ""));
        $privateKey = ‘’;        //私钥地址
        $merchantId = '';        //商户号或者微信渠道号
        $serial_no = '';//证书编号
        $timestamp = time();        //当前时间戳
        $nonce = $this->createNoncestr();        //随机字符串
        $message = "{$method}\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $body . "\n";
        //生成签名
        openssl_sign($message, $rawSign, openssl_get_privatekey(file_get_contents($privateKey)), 'sha256WithRSAEncryption');
        $sign = base64_encode($rawSign);
        //Authorization 类型
        $schema = 'WECHATPAY2-SHA256-RSA2048';
        //生成token
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $merchantId, $serial_no, $nonce, $timestamp, $sign);
        $header = [
            'Content-Type:application/json',
            'Accept:application/json',
            'User-Agent:*/*',
            'Authorization: ' . $schema . ' ' . $token
        ];
        return $header;
    }

    /**
     * 生成随机数
     * @param int $length
     * @return string
     */
    public function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }```
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值