<?php
$api_key = '你的接口密钥,登录控制台后在密钥管理页面申请';
$secret_key = '';
try {
$client = new ApiClient($api_key, $secret_key);
$client->setTimeout(10);
$client->setVerifySSL(false);
echo "=== 开始GET请求 ===\n";
$response = $client->get('https://ovoav.com/api/skygm/gjfxz', [
'key' => $api_key,
'key2' => '其他参数'
]);
print_r($response);
} catch (ApiClientException $e) {
echo "API请求错误: " . $e->getMessage();
if ($e->getCode() > 0) {
echo " (HTTP状态码: " . $e->getCode() . ")";
}
print_r($client->getLastRequestInfo() ?? []);
}
class ApiClient
{
private $apiKey;
private $secretKey;
private $timeout = 30;
private $verifySSL = true;
private $lastRequestInfo = [];
private $defaultHeaders = [];
public function __construct(string $apiKey = '', string $secretKey = '')
{
$this->apiKey = $apiKey;
$this->secretKey = $secretKey;
}
public function setTimeout(int $seconds): self
{
$this->timeout = $seconds;
return $this;
}
public function setVerifySSL(bool $verify): self
{
$this->verifySSL = $verify;
return $this;
}
public function addDefaultHeader(string $name, string $value): self
{
$this->defaultHeaders[$name] = $value;
return $this;
}
public function get(string $endpoint, array $query = [], array $headers = []): array
{
return $this->request('GET', $endpoint, [
'query' => $query,
'headers' => $headers
]);
}
public function post(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('POST', $endpoint, [
'form_data' => $data,
'headers' => $headers
]);
}
public function postJson(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('POST', $endpoint, [
'json' => $data,
'headers' => array_merge(['Content-Type' => 'application/json'], $headers)
]);
}
public function put(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('PUT', $endpoint, [
'json' => $data,
'headers' => $headers
]);
}
public function delete(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('DELETE', $endpoint, [
'json' => $data,
'headers' => $headers
]);
}
public function getLastRequestInfo(): array
{
return $this->lastRequestInfo;
}
private function request(string $method, string $endpoint, array $options = []): array
{
$ch = curl_init();
$url = ltrim($endpoint, '/');
$headers = $this->prepareHeaders($options['headers'] ?? []);
if (!empty($options['query'])) {
$url .= '?' . http_build_query($options['query']);
}
$postData = null;
if (isset($options['form_data'])) {
$postData = http_build_query($options['form_data']);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
} elseif (isset($options['json'])) {
$postData = json_encode($options['json']);
$headers[] = 'Content-Type: application/json';
}
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_SSL_VERIFYPEER => $this->verifySSL,
CURLOPT_SSL_VERIFYHOST => $this->verifySSL,
CURLOPT_HEADER => true,
]);
if ($method !== 'GET' && $postData !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$response = curl_exec($ch);
$error = curl_error($ch);
$info = $this->lastRequestInfo = curl_getinfo($ch);
curl_close($ch);
if ($error) {
throw new ApiClientException("cURL请求失败: " . $error);
}
$headerSize = $info['header_size'];
$responseHeaders = substr($response, 0, $headerSize);
$responseBody = substr($response, $headerSize);
$result = json_decode($responseBody, true) ?? $responseBody;
if ($info['http_code'] >= 400) {
$errorMsg = is_array($result) ? ($result['message'] ?? $responseBody) : $responseBody;
throw new ApiClientException("API请求失败: " . $errorMsg, $info['http_code']);
}
return [
'status' => $info['http_code'],
'headers' => $this->parseHeaders($responseHeaders),
'data' => $result
];
}
private function prepareHeaders(array $headers): array
{
$headers = array_merge($this->defaultHeaders, $headers);
if ($this->apiKey && $this->secretKey) {
$timestamp = time();
$signString = "key={$this->apiKey}×tamp={$timestamp}";
$signature = hash_hmac('sha256', $signString, $this->secretKey);
$headers['X-Api-Key'] = $this->apiKey;
$headers['X-Api-Timestamp'] = $timestamp;
$headers['X-Api-Sign'] = $signature;
}
$curlHeaders = [];
foreach ($headers as $name => $value) {
$curlHeaders[] = "$name: $value";
}
return $curlHeaders;
}
private function parseHeaders(string $headers): array
{
$parsed = [];
foreach (explode("\r\n", $headers) as $i => $line) {
if ($i === 0) {
$parsed['HTTP_CODE'] = $line;
} else {
$parts = explode(': ', $line, 2);
if (count($parts) === 2) {
$parsed[$parts[0]] = $parts[1];
}
}
}
return $parsed;
}
}
class ApiClientException extends \Exception
{
}