1 Star 2 Fork 1

码农兴哥/rx-php-box

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TestCreateHelloOrders.php 5.67 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
declare (strict_types=1);
namespace app\command\test;
use app\command\BaseCommand;
use app\common\utils\MyLog;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
/**
* 测试创建Hello订单
*/
class TestCreateHelloOrders extends BaseCommand
{
protected function configure()
{
// 指令配置
$this->setName('TestCreateHelloOrders')
->setDescription('测试创建Hello订单');
$this->outputArgumentNotice();
}
protected function execute(Input $input, Output $output)
{
try {
$this->testCreateOrder();
} catch (\Exception|\Error $e) {
MyLog::record()->writeExceptionLog($e, 'testCreateHelloOrders_execute_error');
}
}
/**
* API 客户端测试脚本(模拟对方接口调用)
*
* 功能:
* - 自动生成签名所需的所有头部
* - 发送请求并记录完整请求/响应信息
*/
private function testCreateOrder()
{
// ==================== 配置区 ====================
$access_key = config('hello_api.client_access_key');
if (empty($access_key)) {
$this->error(401, '当前系统的 ACCESS_KEY 未配置');
}
$secret_key = config('hello_api.client_secret_key');
if (empty($secret_key)) {
$this->error(401, '当前系统的 SECRET_KEY 未配置');
}
$apiUrl = 'http://rx-php-box.com/admin/demo/createHelloOrders'; // 你的接口地址
// 测试数据(JSON)
$testJson = '{"goods_id":123,"buy_num":2}';
$testData = json_decode($testJson, true);
$log = [];
// 1. 准备请求体(原始 JSON)
$rawBodyJson = json_encode($testData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$log['RAW_BODY'] = $rawBodyJson;
$bodyToSend = $rawBodyJson;
// 3. 计算 BodyMD5(注意:签名时需使用原始数据的 MD5,即加密前的明文 MD5)
$bodyMd5 = md5($rawBodyJson); // 无论是否加密,都用原始明文计算 MD5
$log['BodyMD5'] = $bodyMd5;
// 4. 生成时间戳和随机数
$timestamp = time();
$nonce = bin2hex(random_bytes(8)); // 16位随机字符串
$log['Timestamp'] = $timestamp;
$log['Nonce'] = $nonce;
// 5. 构造待签名字符串(字典序)
$signParams = [
'AccessKey' => $access_key,
'BodyMD5' => $bodyMd5,
'Nonce' => $nonce,
'Secret' => $secret_key,
'Timestamp' => $timestamp,
];
ksort($signParams);
$signStr = http_build_query($signParams, '', '&');
$log['Sign_String'] = $signStr;
// 6. 计算签名
$signature = hash_hmac('sha256', $signStr, $secret_key);
$log['Signature'] = $signature;
// 7. 准备请求头
$headers = [
'Content-Type: application/json; charset=utf-8',
'X-Access-Key: ' . $access_key,
'X-Timestamp: ' . $timestamp,
'X-Nonce: ' . $nonce,
'X-Signature: ' . $signature,
'Content-MD5: ' . $bodyMd5,
];
MyLog::record()->info($log, 'base_log');
$resp = $this->sendCurl($apiUrl, $bodyToSend, $headers);
//MyLog::record()->info($resp, 'response');
echo "成功获取到结果: ";
echo $resp;
exit;
}
private function sendCurl($apiUrl, $bodyToSend, $headers)
{
// 8. 发送 cURL 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyToSend);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // 需要获取响应头,用于调试
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseHeaders = substr($response, 0, $headerSize);
$responseBody = substr($response, $headerSize);
curl_close($ch);
//记录响应
$log = [];
$log['Request_URL'] = $apiUrl;
$log['Request_Headers'] = $headers;
$log['Request_Body'] = $bodyToSend;
$log['Response_Headers'] = $responseHeaders;
$log['Response_httpCode'] = $httpCode;
$log['Response_Body'] = $responseBody;
MyLog::record()->info($log, 'curl_log');
return $responseBody;
}
/**
* AES-256-GCM 加密函数
*
* @param string $plaintext 明文
* @param string $key 密钥(32字节)
* @return array|false 返回 [ciphertext, iv, tag] 或 false
*/
private function encryptAesGcm($plaintext, $key)
{
$iv = random_bytes(12); // GCM 推荐 12 字节 IV
$tag = null; // 标签将由 openssl_encrypt 填充
$ciphertext = openssl_encrypt(
$plaintext,
'aes-256-gcm',
$key,
OPENSSL_RAW_DATA,
$iv,
$tag, // 传入引用,函数会修改此变量
'', // aad 为空
16 // 标签长度 16 字节
);
if ($ciphertext === false) {
return false;
}
return [$ciphertext, $iv, $tag];
}
private function error($code, $message)
{
$resp = [
'code' => $code,
'msg' => $message,
'data' => null,
];
echo returnJson($resp);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rxbook/rx-php-box.git
git@gitee.com:rxbook/rx-php-box.git
rxbook
rx-php-box
rx-php-box
master

搜索帮助