php SDK 示例
<?php
class WLWClient {
private $baseUrl;
private $accessKey;
private $secretKey;
private $timeout;
public function __construct($baseUrl, $accessKey, $secretKey) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->timeout = 30;
}
/**
* 生成HMAC-SHA256签名
*/
private function generateSignature($method, $path, $params) {
// 排除签名参数并排序
unset($params['signature']);
ksort($params);
// 构造参数字符串
$paramStr = [];
foreach ($params as $key => $value) {
$paramStr[] = $key . '=' . $value;
}
$paramStr = implode('&', $paramStr);
// 构造签名字符串
$signStr = $method . $path . $params['timestamp'] . $params['nonce'];
if (!empty($paramStr)) {
$signStr .= $paramStr;
}
// HMAC-SHA256 签名
return hash_hmac('sha256', $signStr, $this->secretKey);
}
/**
* 发送HTTP请求
*/
private function makeRequest($method, $endpoint, $params = []) {
// 添加时间戳和nonce
$params['access_key'] = $this->accessKey;
$params['timestamp'] = time();
$params['nonce'] = $this->generateNonce();
// 生成签名
$params['signature'] = $this->generateSignature($method, $endpoint, $params);
$url = $this->baseUrl . $endpoint;
if ($method === 'GET') {
$url .= '?' . http_build_query($params);
$context = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => $this->timeout,
'header' => "User-Agent: WLW-OpenAPI-PHP-SDK/1.0\r\n"
]
]);
} else {
$postData = http_build_query($params);
$context = stream_context_create([
'http' => [
'method' => 'POST',
'timeout' => $this->timeout,
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" .
"User-Agent: WLW-OpenAPI-PHP-SDK/1.0\r\n" .
"Content-Length: " . strlen($postData) . "\r\n",
'content' => $postData
]
]);
}
$response = file_get_contents($url, false, $context);
if ($response === false) {
throw new Exception("HTTP请求失败");
}
$result = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("JSON解析失败: " . json_last_error_msg());
}
return $result;
}
/**
* 生成随机字符串
*/
private function generateNonce() {
return bin2hex(random_bytes(16));
}
/**
* 心跳检测
*/
public function heartbeat() {
return $this->makeRequest('GET', '/api/v1/heartbeat');
}
/**
* 查询卡板信息
*/
public function getCardInfo($cardNo) {
return $this->makeRequest('GET', '/api/v1/info', [
'card_no' => $cardNo
]);
}
/**
* 卡板复机
*/
public function restartCard($cardNo) {
return $this->makeRequest('POST', '/api/v1/start', [
'card_no' => $cardNo
]);
}
/**
* 卡板停机
*/
public function stopCard($cardNo) {
return $this->makeRequest('POST', '/api/v1/stop', [
'card_no' => $cardNo
]);
}
/**
* 订购套餐
*/
public function orderPackage($cardNo, $packageId, $strategy) {
return $this->makeRequest('POST', '/api/v1/orders', [
'card_no' => $cardNo,
'package_id' => $packageId,
'strategy' => $strategy
]);
}
}
// 使用示例
try {
$client = new WLWClient(
'http://localhost:8080',
'your_access_key',
'your_secret_key'
);
// 心跳检测
$heartbeat = $client->heartbeat();
echo "心跳检测成功: " . json_encode($heartbeat, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
// 查询卡板信息
$cardInfo = $client->getCardInfo('CARD123456789');
echo "卡板信息: " . json_encode($cardInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
// 卡板复机
$restartResult = $client->restartCard('CARD123456789');
echo "卡板复机: " . json_encode($restartResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
// 卡板停机
$stopResult = $client->stopCard('CARD123456789');
echo "卡板停机: " . json_encode($stopResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";
// 订购套餐
$order = $client->orderPackage('CARD123456789', 100, 1);
echo "订购套餐: " . json_encode($order, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";
} catch (Exception $e) {
echo "请求失败: " . $e->getMessage() . "\n";
}
?>