{"code":"INVALID_REQUEST","message":"头部信息不完整"}
<?php
$mch_id = '199999';
$private_key_path = 'apiclient_key.pem';
$serial_no = '5780E68666666BDECEBED64D7625392';
$url = 'https://api.mch.weixin.qq.com/v3/merchant-service/complaints-v2';
$params = [
'limit' => 50,
'offset' => 0,
'begin_date' => '2024-11-01',
'end_date' => '2024-11-30',
'complainted_mchid' => '19999',
];
function generate_signature($method, $url, $params, $private_key_path, $mch_id, $serial_no) {
$timestamp = time();
$nonce_str = bin2hex(random_bytes(16));
$query_str = http_build_query($params);
$absolute_url = parse_url($url, PHP_URL_PATH) . '?' . $query_str;
$sign_str = "{$method}\n{$absolute_url}\n{$timestamp}\n{$nonce_str}\n\n";
openssl_sign($sign_str, $signature, file_get_contents($private_key_path), OPENSSL_ALGO_SHA256);
$signature_base64 = base64_encode($signature);
$auth_header = sprintf(
'WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
$mch_id,
$nonce_str,
$signature_base64,
$timestamp,
$serial_no
);
return $auth_header;
}
function send_get_request($url, $params, $auth_header) {
$query_str = http_build_query($params);
$url_with_params = $url . '?' . $query_str;
$ch = curl_init($url_with_params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: $auth_header",
"Accept: application/json",
]);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
$auth_header = generate_signature('GET', $url, $params, $private_key_path, $mch_id, $serial_no);
$response = send_get_request($url, $params, $auth_header);
echo $response;
?>
请求头还需要添加 'content-type' => 'application/json' 及 'user-agent' 不过建议看这里
https://wechatpay.im/openapi/v3/merchant-service/complaints-v2