1.appkey,appSecret,shopid要根据入驻教程去获取设置
//获取Access_Token 参考 https://op.jinritemai.com/docs/guide-docs/138/21
private function getDYAccessToken($shopid){
$res = json_decode(cache($shopid),true);//获取缓存
if($res !== null && $res["access_token"] && $res["expires_in"]>time()){
return $res['access_token'];
}else {
$method = 'token.create';
$timestamp = time();
$m = array(
'code' => "",
'grant_type' => 'authorization_self',
'shop_id' => $shopid,
);
$accessToken = '';
// 序列化参数
$paramJson = $this->marshal($m);
// 计算签名
$signVal = $this->sign($this->appKey, $this->appSecret, $method, $timestamp, $paramJson);
// 发起请求
$res = $this->fetchdy($this->appKey, $this->host, $method,
$timestamp, $paramJson, $accessToken, $signVal);
$arr = json_decode($res,true)['data'];
if($arr !== null && isset($arr['access_token']) && $arr['access_token'] ){
$arr['expires_in'] = $arr['expires_in'] +time();
//将access_token 全局缓存
cache($shopid,json_encode($arr));
return $arr['access_token'];
}else {
return self::showResCode('操作失败',["data"=>$arr,"flag"=>false]);;
};
}
}
2.工具方法源码
// 调用Open Api,取回数据
private function fetchdy(string $appKey, string $host, string $method,
int $timestamp, string $paramJson, string $accessToken, string $sign):string
{
$methodPath = str_replace('.', '/', $method);
if($accessToken=="")
{
$url = $host . '/' . $methodPath .
'?method=' . urlencode($method) .
'&app_key=' . urlencode($appKey) .
'×tamp=' . urlencode(strval($timestamp)) .
'&v=' . urlencode('2') .
'&sign_method=' . urlencode('hmac-sha256').
'&sign=' . urlencode($sign);
}
else
{
$url = $host . '/' . $methodPath .
'?method=' . urlencode($method) .
'&app_key=' . urlencode($appKey) .
'&access_token=' .urlencode($accessToken) .
'×tamp=' . urlencode(strval($timestamp)) .
'&v=' . urlencode('2') .
'&sign_method=' . urlencode('hmac-sha256').
'&sign=' . urlencode($sign);
}
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Accept: */*\r\n" .
"Content-type: application/json;charset=UTF-8\r\n",
'content' => $paramJson
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
// 序列化参数,入参必须为关联数组
private function marshal(array $param): string {
$this->rec_ksort($param); // 对关联数组中的kv,执行排序,需要递归
$s = json_encode($param, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // 重新序列化,确保所有key按字典序排序
// 加入flag,确保斜杠不被escape,汉字不被escape
return $s;
}
// 关联数组排序,递归
static function rec_ksort(array &$arr) {
$kstring = true;
foreach ($arr as $k => &$v) {
if (!is_string($k)) {
$kstring = false;
}
if (is_array($v)) {
self::rec_ksort($v);
}
}
if ($kstring) {
ksort($arr);
}
}
// 计算签名
private function sign(string $appKey, string $appSecret, string $method,
int $timestamp, string $paramJson): string {
$paramPattern = 'app_key' . $appKey . 'method' . $method . 'param_json' . $paramJson . 'timestamp' . $timestamp . 'v2';
$signPattern = $appSecret . $paramPattern . $appSecret;
return hash_hmac("sha256", $signPattern, $appSecret);
}
本文介绍了如何使用PHP来获取抖音小店的accessToken。首先,你需要按照入驻教程获取appkey, appSecret和shopid。接着,文章会分享用于获取accessToken的PHP工具方法源码。"
112333060,10534829,Anaconda安装与特性介绍,"['anaconda', 'python环境', '包管理', '环境配置']
2970

被折叠的 条评论
为什么被折叠?



