PHP实现签名
<?php
declare(strict_types=1);
function signature(array $params, string $body, string $clientSecret): string
{
ksort($params);
$stringToBeSigned = $clientSecret;
foreach ($params as $key => $value) {
$stringToBeSigned .= $key . $value;
}
unset($key, $value);
$stringToBeSigned .= $body;
$stringToBeSigned .= $clientSecret;
return md5($stringToBeSigned);
}
$clientId = "clientId";
$clientSecret = "clientSecret";
$params = [
"Accept" => "application/json",
"Content-Type" => "application/json",
"Method" => "taobao.tbk.tpwd.create",
"Timestamp" => time()
];
$body = json_encode([
"url" => "https://s.click.taobao.com/FUYXmNu"
]);
$params["Authorization"] = "Basic " . base64_encode($clientId . ":" . signature($params, $body, $clientSecret));
$headers = [];
foreach ($params as $name => $value) {
$headers[] = $name . ": " . $value;
}
$options = [
CURLOPT_URL => "https://union.lottefuture.com/gateway",
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
];
$handle = curl_init();
curl_setopt_array($handle, $options);
$response = curl_exec($handle);
$info = curl_getinfo($handle);
if ($response === false) exit(curl_error($handle));
curl_close($handle);
$response = json_decode($response, true);
if ($info["http_code"] != 200) exit($response["message"]);
var_dump($response);
修改于 2023-01-06 02:47:26