简单分享一下PHP对接抖音小店的方法 首先需要配置一些公共的参数 踩到不少坑,需要有一起开发的互相学习。
protected $app_key = "app_key"; // 你的key
protected $app_secret = "app_secret"; // 你的secret
protected $app_url = "https://openapi-fxg.jinritemai.com"; // 固定的抖音地址
然后 我们需要获取一下令牌 access_token
https://openapi-fxg.jinritemai.com/oauth2/access_token?app_id=KEY&app_secret=SECRET&grant_type=authorization_self
名称 | 类型 | 是否必须 | 示例值 | 描述 |
---|---|---|---|---|
app_id | string | 是 | 3409409348479354011 | 即应用key ,长度19位数字字符串 |
app_secret | string | 是 | 2ad2355c-01d0-11f8-91dc-05a8cd1054b1 | 即应用密钥字符串 |
grant_type | string | 是 | authorization_self | 授权类型,默认为authorization_code |
上边是抖音文档 下边是我写的一个小 demo access_token 根据自己的需求进行记录缓存
/**
* Created by PhpStorm.
* User: dyg
* Date: 2020/11/20
* Time: 19:50
* 获取access_token
*/
public function getAccessToken(){
$token = $this->app_url."/oauth2/access_token?app_id=$this->app_key&app_secret=$this->app_secret&grant_type=authorization_self";
$access_token = $this->request_curls($token, $method = "GET", $data = array());
return $access_token['data']['access_token'];
}
然后我们开始封装统一调用的方法了 因为timestamp 与 param_json GET请求姿势可能不对的话 会使参数发生改变,我为了粗暴点 就不要什么姿势了 就直接POST了 。 sign加密的时候一定要严格按照这个顺序来,不然你要的结果不会给你的。我简单用的md5方式 想解锁更多姿势可以去点传送门自行研究 https://op.jinritemai.com/docs/guide-docs/10/23
/**
* Created by PhpStorm.
* User: dyg
* Date: 2020/11/20
* Time: 20:12
* 抖店统一请求
*/
public function getCurl($params){
$timestamp = date("Y-m-d H:i:s",time());
$sign = md5($this->app_secret."app_key".$this->app_key."method".$params['method']."param_json".$params['param_json']."timestamp".$timestamp."v2".$this->app_secret);
$curl = $this->app_url.$params['method_url'];
$data = [
"param_json" => $params['param_json'],
"timestamp" => $timestamp,
"app_key" => $this->app_key,
"method" => $params['method'],
"access_token" => $this->getAccessToken(),
"v" => "2",
"sign" => $sign,
];
$res = $this->request_curls($curl, $method = "POST", $data);
return $res;
}
public function request_curls($curl, $method = "GET", $data){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $curl); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //3.请求方式
if ($method == "POST") {
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($ch); //获取html内容
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return json_decode($tmpInfo,1);
}
请求方式 各位随意解锁
下边 我再附带一份比较简单的请求写法 将param_json中参数(标点符号前后不能有空格)按照key字母先后顺序排序,且值必须是String,组成json
/**
* Created by PhpStorm.
* User: dyg
* Date: 2020/11/20
* Time: 21:22
*获取规格列表
*/
public function specList(){
$data['param_json'] = "{}";
$data['method'] = "spec.list";
$data['method_url'] = "/spec/list";
return app('json')->success($this->repository->getCurl($data));
}
/**
* Created by PhpStorm.
* User: dyg
* Date: 2020/11/21
* Time: 15:37
* 添加规格
*/
public function specAdd(SepcAddValidate $validate)
{
$json = $this->request->params(['name', 'specs']);
$validate->check($json);
ksort($json);
$data['param_json'] = json_encode($json, JSON_UNESCAPED_UNICODE);
$data['method'] = "spec.add";
$data['method_url'] = "/spec/add";
return app('json')->success($this->repository->getCurl($data));
}