$appId, 'generate_url' => $longUrl, 'group_name' => $groupName, 'nonce' => bin2hex(random_bytes(16)), 'remark' => $remark, 'timestamp' => (string) time(), ]; ksort($payload, SORT_STRING); $parts = []; foreach ($payload as $key => $value) { $parts[] = $key . '=' . rawurlencode($value); } $signature = strtoupper(hash_hmac('sha256', implode('&', $parts), $appSecret)); $curl = curl_init($baseUrl . '/api/ShortLink/Generate'); curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-Signature: ' . $signature], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, ]); $body = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); $error = curl_error($curl); curl_close($curl); if ($body === false) throw new RuntimeException('Network error: ' . $error); if ($status < 200 || $status >= 300) throw new RuntimeException('HTTP ' . $status . ': ' . $body); $result = json_decode($body, true, 512, JSON_THROW_ON_ERROR); if (($result['resultCode'] ?? null) !== 200 || empty($result['resultData'])) { throw new RuntimeException($result['resultMsg'] ?? 'Invalid API response'); } return $baseUrl . '/' . $result['resultData']; } echo generate('https://example.com/?campaign=partner', '合作方活动', '第三方接入') . PHP_EOL; // For multiple links, call generate() for each item, with a fresh nonce and signature.