tiki东南亚电商api接口
2022-07-08 18:10 天心PHP 阅读(389) 评论(0) 编辑 收藏 举报文档:https://open.tiki.vn/docs/docs/current/
回调地址:http://localhost/services/tiki/author/getcode/appid/2 #本地和测试 可以用http 线上必须要用https
https://api.tiki.vn/sc/oauth2/auth?response_type=code&client_id=1234567899&redirect_uri=http://localhost/services/tiki/author/getcode/appid/2&scope=offline product order&state=RJvROw5fL 授权地址
授权之后 会返回到回调地址 并带有参数 code 例如: http://localhost/services/tiki/author/getcode/appid/2?code=qF_4Q1K4ms0x4BuCoj_Q34K3XLKAQ86-GAqkfqtc9Hk.jf5hLgrBIwIR54ERQiJWS4wxurHYrKj6DnnG_k-kmvY&scope=offline%20product%20order&state=RJvROw5fL
如果线上是http 也可以填写对应的https(虽然不通),返回带有code 的回调链接时,改https为 http就可以调通了 并获取到了对应的 code (code获取token时候只能用一次,再次获取token要重新授权获取新的code)
1.获取token
$url = 'https://api.tiki.vn/sc/oauth2/token'; $headers = array( "Accept:application/json", "Authorization: Basic ".base64_encode("3044623232733343423276236922:W3d3sdfsde4bu-Jwwh9vsdfsdf9hHZb0sydfgdfjnkeJyBCkV"),//app_key:app_secret "Content-Type: application/x-www-form-urlencoded" ); $data = array( 'grant_type'=>urlencode('authorization_code'), 'code'=>urlencode('qF_4Q1K4ms0x4BuCoj_Q34K3XLKAQ86-GAqkfqtc9Hk.jf5hLgrBIwIdfgdfgR54ERQiJWS4wxdfgdfgdurHYrKj6DnnG_k-kmvY'), 'redirect_uri'=>('http://localhost/services/tiki/author/getcode/appid/2'), 'client_id'=>urlencode('3044627332766922'), ); print_r('<pre>'); print_r($headers); print_r('</pre>'); print_r('<pre>'); print_r($data); print_r('</pre>'); $res = mypost($url,$headers, $data); print_r('<pre>'); print_r(json_decode($res,true)); print_r('</pre>'); exit(); function mypost($url,$headers, $data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); if($headers!=""){ curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers); }else { curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点 } curl_setopt($curl, CURLOPT_HEADER, FALSE);//不抓取头部信息。只返回数据 curl_setopt($curl, CURLOPT_TIMEOUT,1000);//超时设置 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, TRUE); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); $response = curl_exec($curl); if (curl_errno($curl)) { return curl_error($curl); } curl_close($curl); return $response; }
结果:
Array ( [0] => Accept:application/json [1] => Authorization: Basic MzA0NDYyNzMzMjsdfsdfc2NjkyMjpXM2RlNGJ1LUp3dsdfsdfsdf2g5djloSFpiMHN5am5rZUp5QkNrVg== [2] => Content-Type: application/x-www-form-urlencoded ) Array ( [grant_type] => authorization_code [code] => qF_4Q1K4ms0x4BuCoj_Q34K3XLKAsdfsdfQ86-GAqkfqtc9Hk.jf5hLgrBIwIR54ERQiJWS4wxurHYrKj6DnnG_k-kmvY [redirect_uri] => http://localhost/services/tiki/author/getcode/appid/2 [client_id] => 3044627332766922 ) Array ( [access_token] => KQMIpIvyQFXKZYTejsdfsddcsdfsR1U2ZcjCxbis-dSjz-fgNYak.FuV7jOQoPrkkeNSMWtHSFS8d5xJdP7Sft8VFZZnj-wo [expires_in] => 1296000 [refresh_token] => CgdHxc6c5v3zX4lxiGsdfsdfsdim9Dd_1rrVCKbraT30_Z7eBSs.Sp_yQ69TdrxjbngMcTisV78eDP4akSfpyz_A3yZ-Dy4 [scope] => offline product order [token_type] => bearer )
2.刷新token
$header = array( "Authorization: Basic ".base64_encode($app->app_key.":".$app->app_secret), "Content-Type: application/x-www-form-urlencoded" ); $param['grant_type'] = 'refresh_token'; $param['refresh_token'] = $account->api_refresh_token; $param['client_id'] = $app->app_key; print_r('<pre>'); print_r($header); print_r('</pre>'); print_r('<pre>'); print_r($param); print_r('</pre>'); $res = json_decode($this->cur_request($url,$header,$param),true); print_r('<pre>'); print_r($res); print_r('</pre>'); exit();
结果:
Array ( [0] => Authorization: Basic MzA0NDYyNzMzMjc2NjkyMjpXM2RlNGJ1LUp3d2g5djloSFpiMHN5am5rZUp5QkNrVg== [1] => Content-Type: application/x-www-form-urlencoded ) Array ( [grant_type] => refresh_token [refresh_token] => F5GjCa25fyQgh4bKaVB8uiM4Tdq3LZy3nCzJDXJETw4.9bOng91XOx451CKI83iCeFymA3Gj_4v0j3L7Bep5_ls [client_id] => 3044627332766922 ) Array ( [access_token] => btQHrLJ0dlWrQ7vG6gkDspK5AIIDlm3eVXQk0_uyyog.CMGcZnthMliAI-_IMDpFfhpQgNXUlo5b_athtK5s_iA [expires_in] => 1295999 [refresh_token] => b2aCR6NuJyp5iRWuFEAk2STOKbh9F2lg8hzbHUoWKnI.lqK2XqpS6LG6NSJmpLmPiH7wlxPcFK2ze2eDVMJAjlA [scope] => offline product order [token_type] => bearer )
2.client_credentials类型
public function actionGetclienttoken(){ $accountModel = new TikiAccount(); $account = $accountModel->getDbConnection()->createCommand() ->select('id,app_id') ->from($accountModel->tableName()) ->queryAll(); $url = 'https://api.tiki.vn/sc/oauth2/token'; if(!empty($account)){ foreach ($account as $val){ $app = (new TikiApp())->findByPk($val['app_id']); if(!empty($app)){ $header = array( "Authorization: Basic ".base64_encode($app->app_key.":".$app->app_secret), "Content-Type: application/x-www-form-urlencoded" ); $param['grant_type'] = 'client_credentials'; $res = json_decode($this->cur_request($url,$header,$param),true); if(isset($res['access_token'])){ $accountModel->updateAll(['modify_time'=>date('Y-m-d H:i:s'),'api_token'=>$res['access_token'],'expires_time'=>(time()+$res['expires_in'])],'app_id='.$val['app_id']); } } } echo '获取token成功'; Yii::app()->end(); } echo $this->failureJson(array( 'message'=>'获取token失败' )); Yii::app()->end(); }
结果:
Array ( [access_token] => 9k27Bw9UGMlAc6S8vqL3Prsz3FsKVzLODYJ8H0IcnXM.W0qQqyTeROFd-D4PjdldVNacp3peYzbPxKmnnJuIdDM [expires_in] => 1295999 [scope] => [token_type] => bearer )