开发流程如下图 为方便开发 我用的是沙盒模式 开放平台前三个接口开发流程如下,我的代码没做相关优化 代码如下 1、 接口调用凭证 //接口调用凭证 public function get
开发流程如下图
为方便开发 我用的是沙盒模式
开放平台前三个接口开发流程如下,我的代码没做相关优化
代码如下
1、 接口调用凭证
//接口调用凭证 public function getAccessToken() { $uri = 'https://open-sandbox.douyin.com/api/apps/v2/token'; $options = [ 'body' => JSON_encode([ 'appid' => 'tt5d842b39c2bb31', 'secret' => '9208a4e3130eb24cf9fcbc4a4906765c1', 'grant_type' => 'client_credential']), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = (new \GuzzleHttp\Client)->post($uri, $options); $body = $response->getBody(); $stringBody = (string)$body; $result = json_decode($stringBody, true); if ($result['err_no'] == 0) { return $result['data']['access_token']; } return null; }
2、登录 (通过小程序login接口获取到登录凭证后,开发者可以通过服务器发送请求的方式获取 session_key 和 openId)。
//通过小程序login接口获取到登录凭证后,开发者可以通过服务器发送请求的方式获取 session_key 和 openId。 public function code2Session() { $uri = 'https://open-sandbox.douyin.com/api/apps/v2/jscode2session'; //客户端获取并传过来 $code = 'T9loKB1gsyl92x5s7wiTwKteJgeA5Pjq21JNWPtXfx1BUXz0PIzl2ruw2fnPjc9VDA97LlXkKvQW6QtM-0jUNN-frfnVZ5nBluYdVos3FJw8mGe0iaHJHkXq7i4'; $options = [ 'body' => json_encode([ 'appid' => 'tt5d842b39c2b6', 'secret' => '9208a4e3130eb2408fcf9fcbc4a496', 'code' => $code]), 'headers' => [ 'Content-Type' => 'application/json' ] ]; $response = (new \GuzzleHttp\Client)->post($uri, $options); $body = $response->getBody(); $stringBody = (string)$body; $result = json_decode($stringBody, true); if ($result['err_no'] == 0) { return $result['data']; } return ['openid' => '_000DO0RDLCqmW1_EcB4GXidO-bl8T', 'session_key' => '']; }
3、数据缓存
//数据缓存public function setUserstorage() { $user = $this->code2Session(); $body = [ 'kv_list' => [ ['key' => 'name', 'value' => 'yang'] ] ]; //用户登录态签名 $signature = hash_hMac('sha256', json_encode($body), $user['session_key']); $uri = 'https://open-sandbox.douyin.com/api/apps/set_user_storage'; $response = (new \GuzzleHttp\Client)->post($uri, [ 'query' => [ 'access_token' => $this->getAccessToken(), 'openid' => $user['openid'], 'signature' => $signature, 'sig_method' => 'hmac_sha256', ], 'body' => json_encode($body), 'headers' => [ 'Content-Type' => 'application/json' ] ]); $body = $response->getBody(); $stringBody = (string)$body; $result = json_decode($stringBody, true); dd($result); }
来源地址:https://blog.csdn.net/qq_41688060/article/details/129797016
--结束END--
本文标题: 抖音小程 服务端OpenAPI 开发示例
本文链接: https://lsjlt.com/news/384977.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0