PHP 示例
<?php
/**
* 依赖:PHP 8+(openssl、curl 扩展)
* AppSecret 是平台发放的 RSA 私钥(Base64 DER PKCS#8)。
*/
$appId = getenv('GATEWAY_APPID');
if (!$appId) {
throw new RuntimeException('请设置 GATEWAY_APPID');
}
$appSecret = getenv('GATEWAY_APPSECRET');
if (!$appSecret) {
throw new RuntimeException('请设置 GATEWAY_APPSECRET');
}
$baseUrl = getenv('GATEWAY_BASEURL') ?: 'https://t-apiexpo.zhuopux.com';
$content = json_encode([
'RealName' => '张三',
'IDCardNO' => '110101199001011234',
'PhoneNum' => '13800000000',
'BarCode' => 'T20260001',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$request = [
'appid' => $appId,
'method' => 'AppointmentNotFaceAndBarCode',
'timestamp' => (string)round(microtime(true) * 1000),
'charset' => 'UTF-8',
'version' => '1.0',
'content' => $content,
];
$request['sign'] = sign(buildCanonical($request), $appSecret);
$ch = curl_init($baseUrl . '/api/gateway/receive');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json; charset=utf-8'],
CURLOPT_POSTFIELDS => json_encode($request, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException(curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo 'HTTP ' . $httpCode . PHP_EOL;
echo $response . PHP_EOL;
function buildCanonical(array $r): string
{
$keys = ['appid', 'charset', 'content', 'method', 'timestamp', 'version'];
sort($keys, SORT_STRING);
$parts = [];
foreach ($keys as $key) {
$parts[] = $key . '=' . $r[$key];
}
return implode('&', $parts);
}
function sign(string $canonical, string $privateKeyBase64): string
{
$md5Hex = md5($canonical); // 默认输出小写 hex
$pem = "-----BEGIN PRIVATE KEY-----\n"
. chunk_split($privateKeyBase64, 64, "\n")
. "-----END PRIVATE KEY-----\n";
$key = openssl_pkey_get_private($pem);
if ($key === false) {
throw new RuntimeException('私钥格式不正确:需要 Base64 DER PKCS#8');
}
$signature = '';
if (!openssl_sign($md5Hex, $signature, $key, OPENSSL_ALGO_SHA256)) {
throw new RuntimeException('签名失败');
}
return base64_encode($signature);
}