万能代码
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';
}
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;
}
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);
$schema = 'WECHATPAY2-SHA256-RSA2048';
$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;
}
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;
}```
|