|
@@ -561,4 +561,68 @@ function remove0x($value)
|
|
|
return substr($value, 2);
|
|
|
}
|
|
|
return $value;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 生成 Mefree API 的签名
|
|
|
+ * @param string $timestamp UTC 时间戳 (ISO 8601 格式)
|
|
|
+ * @param string $method HTTP 方法 (GET/POST)
|
|
|
+ * @param string $request_path API 请求路径(含参数)
|
|
|
+ * @param string $secret_key API 的 Secret Key
|
|
|
+ * @return string 返回 Base64 编码的签名
|
|
|
+ */
|
|
|
+function generate_signature($timestamp, $method, $request_path, $secret_key) {
|
|
|
+ // 拼接待签名字符串
|
|
|
+ $string_to_sign = $timestamp . $method . $request_path;
|
|
|
+
|
|
|
+ // 使用 HMAC-SHA256 算法对字符串进行加密
|
|
|
+ $hash = hash_hmac('sha256', $string_to_sign, $secret_key, true);
|
|
|
+
|
|
|
+ // 返回 Base64 编码的签名
|
|
|
+ return base64_encode($hash);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 发起 Mefree API 请求
|
|
|
+ * @param string $method HTTP 方法 (GET/POST)
|
|
|
+ * @param string $request_path API 请求路径(含查询参数)
|
|
|
+ * @param string $base_url API 基础 URL
|
|
|
+ * @param string $api_key API Key
|
|
|
+ * @param string $secret_key API Secret Key
|
|
|
+ * @return mixed 返回 API 响应内容
|
|
|
+ */
|
|
|
+function send_request($method, $request_path, $base_url, $api_key, $secret_key) {
|
|
|
+ // 当前 UTC 时间戳
|
|
|
+ $timestamp = gmdate("Y-m-d\TH:i:s.v\Z");
|
|
|
+
|
|
|
+ // 生成签名
|
|
|
+ $signature = generate_signature($timestamp, $method, $request_path, $secret_key);
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ $headers = [
|
|
|
+ "Content-Type: application/json",
|
|
|
+ "MF-ACCESS-KEY: {$api_key}",
|
|
|
+ "MF-ACCESS-SIGN: {$signature}",
|
|
|
+ "MF-ACCESS-TIMESTAMP: {$timestamp}",
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 初始化 CURL
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $base_url . $request_path); // 设置 URL
|
|
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // 设置请求方法
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 设置请求头
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应内容
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ $response = curl_exec($ch);
|
|
|
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取 HTTP 状态码
|
|
|
+ curl_close($ch);
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ return [
|
|
|
+ "status_code" => $http_code,
|
|
|
+ "response" => $response
|
|
|
+ ];
|
|
|
}
|