shopee api
2022-07-14 09:42 天心PHP 阅读(715) 评论(0) 编辑 收藏 举报文档地址:https://open.shopee.com/documents/v2/Introduction?module=87&type=2
境外接口:
页面地址授权:
<?php if($model->partner_id && $model->secret_key): $url = '/api/v2/shop/auth_partner'; $redirect ="http://publish.xxxx.com/services/shopee/shopeenew/gettoken/id/{$model->id}"; $timestamp= time(); $base_string = sprintf("%s%s%s%s%s",$model->partner_id,$url,$timestamp,'',''); $sign = hash_hmac('sha256', $base_string,$model->secret_key); $url= 'https://partner.shopeemobile.com'.$url."?timestamp={$timestamp}&partner_id={$model->partner_id}&redirect={$redirect}&sign={$sign}"; ?> <li> <div class="buttonActive"> <div class="buttonContent"> <button type="button" onclick="checkAccessToken('<?php echo $url; ?>')"><?php echo Yii::t('system', '账号权限验证') ?></button> </div> </div> </li> <?php endif; ?>
<script>
function checkAccessToken(url){
window.open(url);
}
</script>
class ShopeenewController extends YbController { public function getModel($account) { return new ShopeeNewAPI($account); } /* * /services/shopee/shopeenew/gettoken */ public function actionGettoken() { $code = Yii::app()->request->getParam('code'); $id = Yii::app()->request->getParam('id'); $shop_id = Yii::app()->request->getParam('shop_id'); $url = '/api/v2/auth/token/get'; $account = YbModel::model('ShopeeAccount')->findByPk($id); if (!$account) { exit('none'); } $api = $this->getModel($account); $reponse = $api->getToken($code); $reponse = json_decode($reponse, true); if ($reponse['access_token']) { YbModel::model('ShopeeAccount')->updateAll([ 'token' => $reponse['access_token'], 'refresh_token' => $reponse['refresh_token'], 'expire_in' => time() + $reponse['expire_in'] ], "shop_id = {$shop_id}"); } else { VHelper::dump($reponse, ['message' => $reponse['message'], 'error' => $reponse['error']]); } } /* * /services/shopee/shopeenew/refreshtoken/id/48 * 四小时过期 */ public function actionRefreshtoken() { $id = Yii::app()->request->getParam('id'); if (!$id) { $account = YbModel::model("ShopeeAccount")->querypairs("id,seller_name", "activate_status=1 and refresh_token!='' "); if (!$account){ exit('no account'); } foreach ($account as $key=>$val){ $url = sprintf('%s/services/shopee/shopeenew/refreshtoken?id=%s', $_SERVER['HTTP_HOST'], $key); print_r($url); MHelper::curl_post_async($url); } } else { $account = YbModel::model('ShopeeAccount')->findByPk($id); } $api = $this->getModel($account); $reponse = $api->refreshToken($account); $reponse = json_decode($reponse, true); if ($reponse['access_token']) { $account->token = $reponse['access_token']; $account->refresh_token = $reponse['refresh_token']; $account->expire_in = time() + $reponse['expire_in']; $account->save(); VHelper::dump($reponse); } else { VHelper::dump($reponse, ['message' => $reponse['message'], 'error' => $reponse['error']]); } } }
class ShopeeNewAPI { public $test_host = 'https://partner.shopeemobile.com'; private $shop_id,$partner_id,$secret_key,$url; function __construct($account){ //获取配置信息 $this->shop_id = intval($account->shop_id); $this->partner_id = intval($account->partner_id); $this->secret_key = trim($account->secret_key); //基础接口域名,可能会根据账号而不同 $this->host = Yii::app()->params['shopee_v2_host_url'];//配置文件 https://partner.shopeemobile.com $this->token = $account->token; } private function signature($url,$timestamp,$access_token='',$shop_id=''){ $base_string = sprintf("%s%s%s%s%s",$this->partner_id,$url,$timestamp,$access_token,$shop_id); $sign = hash_hmac('sha256', $base_string,$this->secret_key); return $sign; } static function curlPost1($url,$post_data, $header,$timeout=0){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); if($timeout >= 1){ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } if($header){ curl_setopt($ch, CURLOPT_HTTPHEADER,$header); }else{ curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json;', 'Accept:application/json' ) ); } // post数据 curl_setopt($ch, CURLOPT_POST, 1); // post的变量 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); //返回获得的数据 return $output; } public static function http_get($url){ $curl = curl_init(); $headers[] = "Content-type: application/json"; // $headers[] = "Accept:application/json"; curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // curl_setopt($curl, CURLOPT_NOBODY, FALSE); $reponse = curl_exec($curl); curl_close($curl); return json_decode($reponse,true); } public function getParams($account,$secret_key,$url){ $params['partner_id'] = (int)$account->partner_id; $params['shop_id'] = (int)$account->shop_id; $params['access_token']= $account->token; $timestamp = time(); $base_string = sprintf("%s%s%s%s%s",$params['partner_id'],$url,$timestamp,$params['access_token'],$params['shop_id']); // $base_string = sprintf("%s%s%s",$params['partner_id'],$url,$timestamp); $params['sign'] = hash_hmac('sha256', $base_string,$secret_key); return $URL = $this->test_host.$url."?timestamp={$timestamp}&".http_build_query($params); } public function Auth($account) { $url = '/api/v2/shop/auth_partner'; $redirect ="http://publish.xxxxxx.com/services/shopee/shopeenew/gettoken/id/{$account->id}"; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&redirect={$redirect}&sign={$sign}"; return $url; } public function getToken($code) { $url = '/api/v2/auth/token/get'; $redirect ="'http://publish.xxxxxx.com'"; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url=$this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}"; $api = NEW ShopeeNewAPI(); $data = [ 'code' => $code, 'shop_id' => $this->shop_id, // 'main_account_id'=>$this->main_account_id, 'partner_id' => $this->partner_id ]; $reponse = $this->curlPost1($url,json_encode($data)); return $reponse; } public function refreshToken($account) { $url = '/api/v2/auth/access_token/get'; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}"; $data = [ 'refresh_token' => $account->refresh_token, 'shop_id' => (int)$this->shop_id, 'partner_id' => (int)$this->partner_id ]; $reponse = $this->curlPost1($url,json_encode($data)); return $reponse; } public function getCategory(){ $lanaguage = 'zh-hans'; $url = '/api/v2/product/get_category'; // $URL = $this->getParams($account,$this->secret_key,$url); $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $URL.= "&language={$lanaguage}"; $reponse = $this->http_get($URL); return $reponse; } //realpath public function uploadImage($images,$exit=[]){ $url = '/api/v2/media_space/upload_image'; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}"; // $img = 'D:\yunyi\erp\images/1539687148314.jpg'; $header = array( 'Content-Type: multipart/form-data', ); $plat_img = []; foreach($images as $img){ $base_url= md5($img); if($exit[$base_url]){ $plat_img[$base_url] = $exit[$base_url]; continue; } $real_path = realpath($img); $data = [ 'image' => new CURLFile($real_path), ]; $reponse = $this->curlPost1($url,$data,$header); $reponse = json_decode($reponse,true); if(!empty($reponse['response']['image_info']['image_id'])){ $plat_img[$base_url] = $reponse['response']['image_info']['image_id']; } } return $plat_img; } //------------------------------------------------------活动接口 //获取捆绑交易活动列表(xsh) public function getbundledeallist(){ $url = '/api/v2/bundle_deal/get_bundle_deal_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['page_size']= 100; $params['time_status']= 3; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取捆绑交易活动的产品(XSH) public function getbundledealitem($bundle_deal_id){ $url = '/api/v2/bundle_deal/get_bundle_deal_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['bundle_deal_id']= $bundle_deal_id;//活动ID $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //修改捆绑活动产品状态 public function updatebundledealitem($data){ $url = '/api/v2/bundle_deal/update_bundle_deal_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //获取附加交易活动列表(xsh) public function getaddondeallist(){ $url = '/api/v2/add_on_deal/get_add_on_deal_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['page_size']= 100; $params['promotion_status']= 'ongoing'; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取附件交易活动的主产品(xsh) public function getaddondealmainitem($add_on_deal_id){ $url = '/api/v2/add_on_deal/get_add_on_deal_main_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['add_on_deal_id']= $add_on_deal_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取附件交易活动的子产品(xsh) public function getaddondealsubitem($add_on_deal_id){ $url = '/api/v2/add_on_deal/get_add_on_deal_sub_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['add_on_deal_id']= $add_on_deal_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //修改附件交易活动主产品状态 public function updateaddondealmainitem($data){ $url = '/api/v2/add_on_deal/update_add_on_deal_main_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //修改附件交易活动子产品状态 public function updateaddondealsubitem($data){ $url = '/api/v2/add_on_deal/update_add_on_deal_sub_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //------------------------------------------------------产品接口 //获取产品属性(xsh) public function getAttributes($category_id){ $lanaguage = 'zh-hans'; $url = '/api/v2/product/get_attributes'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['category_id']= $category_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $URL.= "&language={$lanaguage}"; $reponse = $this->http_get($URL); return $reponse; } //获取品牌(xsh) public function getbrandlist($category_id,$offset,$page_size){ $lanaguage = 'zh-hans'; $url = '/api/v2/product/get_brand_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['category_id']= $category_id; $params['status'] = 1; $params['offset'] = $offset; $params['page_size'] = $page_size; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $URL.= "&language={$lanaguage}"; $reponse = $this->http_get($URL); return $reponse; } //获取发货限制(xsh) public function getdtslimit($category_id){ $url = '/api/v2/product/get_dts_limit'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['category_id']= $category_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取产品上传限制(xsh) public function getitemlimit(){ $url = '/api/v2/product/get_item_limit'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取产品列表(xsh) public function getitemlist($offset,$page_size){ $url = '/api/v2/product/get_item_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['offset']= $offset; $params['page_size']= $page_size; $params['item_status']= 'NORMAL'; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取产品基本信息 public function getitembaseinfo($item_id_list){ $url = '/api/v2/product/get_item_base_info'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['item_id_list']= $item_id_list; $params['need_tax_info']= true; $params['need_complaint_policy']= true; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取产品额外信息 public function getitemextrainfo($item_id_list){ $url = '/api/v2/product/get_item_extra_info'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['item_id_list']= $item_id_list; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //添加产品(xsh) public function additem($data){ $url = '/api/v2/product/add_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //更新产品信息 public function updateitem($data){ $url = '/api/v2/product/update_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //删除产品信息 public function deleteitem($item_id){ $url = '/api/v2/product/delete_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$item_id); return $reponse; } //创建/修改产品层级结构 public function inittiervariation($data){ $url = '/api/v2/product/init_tier_variation'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //修改产品层级内容 public function updatetiervariation($data){ $url = '/api/v2/product/update_tier_variation'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //获取模型列表 public function getmodellist($item_id){ $url = '/api/v2/product/get_model_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['item_id']= $item_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //添加模型 public function addmodel($data){ $url = '/api/v2/product/add_model'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //更新模型 public function updatemodel($data){ $url = '/api/v2/product/update_model'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //删除模型 public function deletemodel($data){ $url = '/api/v2/product/delete_model'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //取消产品 public function unlistitem($data){ $url = '/api/v2/product/unlist_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //更新价格(xsh) public function updateprice($data){ $url = '/api/v2/product/update_price'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //更新库存(xsh) public function updatestock($data){ $url = '/api/v2/product/update_stock'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //提升产品 public function boostitem($item_id_list){ $url = '/api/v2/product/boost_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$item_id_list); return $reponse; } //获取提示产品列表 public function getboostedlist(){ $url = '/api/v2/product/get_boosted_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //获取产品促销信息 public function getitempromotion($item_id_list){ $url = '/api/v2/product/get_item_promotion'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['item_id_list']= $item_id_list; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //更新sip项目价格 public function updatesipitemprice($data){ $url = '/api/v2/product/update_sip_item_price'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //------------------------------------------------------折扣接口 //添加折扣活动 public function adddiscount($data){ $url = '/api/v2/discount/add_discount'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //添加折扣活动产品 public function adddiscountitem($data){ $url = '/api/v2/discount/add_discount_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } // 删除折扣活动 public function deletediscount($discount_id){ $url = '/api/v2/discount/delete_discount'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$discount_id); return $reponse; } //删除折扣活动中的产品 public function deletediscountitem($data){ $url = '/api/v2/discount/delete_discount_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //获取接口列表 public function getdiscountlist($page_no=100,$page_size=1){ $url = '/api/v2/discount/get_discount_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $params['discount_status']= 'ongoing';//upcoming/ongoing/expired/all. 即将开始/正在进行/过期的/全部 $params['page_no']= $page_no; $params['page_size']= $page_size; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //更新折扣活动 public function updatediscount($data){ $url = '/api/v2/discount/update_discount'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //更新折扣活动产品 public function updatediscountitem($data){ $url = '/api/v2/discount/update_discount_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //结束折扣活动 public function enddiscount($data){ $url = '/api/v2/discount/end_discount'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //------------------------------------------------------热门精选 //获得热门精选 public function gettoppickslist(){ $url = '/api/v2/top_picks/get_top_picks_list'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } //添加一个热门精选 list最少需要四项 public function addtoppicks($data){ $url = '/api/v2/top_picks/add_top_picks'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //修改热门精选 list最少需要四项 public function updatetoppicks($data){ $url = '/api/v2/top_picks/update_top_picks'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //删除热门精选 public function deletetoppicks($data){ $url = '/api/v2/top_picks/delete_top_picks'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } //listing置顶 public function topping($data){ $url = '/api/v2/product/boost_item'; $params['partner_id'] = $this->partner_id; $params['shop_id'] = $this->shop_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->shop_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->curlPost1($URL,$data); return $reponse; } } ?>
CB卖家接口
主账户授权了所有的子账户
授权得到上面的token对于所有的子账户都可以用,但当这个token过期了 各个子账户的token是单独刷新的 (用子账户的 shop_id)
https://partner.shopeemobile.com/api/v2/auth/access_token/get?partner_id=1000016×tamp=1657263479&sign=9c685bc7e4a74e90f45fe1933f1d72b2d9705acda4093a9fb1ec7e2b57ccea2a {"shop_id":54804, "refresh_token":"456e416149664b76745a6a794156794a", "partner_id":1000016 }
文档:https://open.shopee.com/developer-guide/20
<?php if($model->partner_id && $model->secret_key): $url = '/api/v2/shop/auth_partner'; $redirect ="http://publish.xxxxxx.com/services/shopee/shopeeglobal/gettoken/id/{$model->id}"; $timestamp= time(); $base_string = sprintf("%s%s%s%s%s",$model->partner_id,$url,$timestamp,'',''); $sign = hash_hmac('sha256', $base_string,$model->secret_key); $url= 'https://partner.shopeemobile.com'.$url."?timestamp={$timestamp}&partner_id={$model->partner_id}&redirect={$redirect}&sign={$sign}"; ?> <li> <div class="buttonActive"> <div class="buttonContent"> <button type="button" onclick="checkAccessToken('<?php echo $url; ?>')"><?php echo Yii::t('system', '账号权限验证') ?></button> </div> </div> </li> <?php endif; ?>
<script>
function checkAccessToken(url){
window.open(url);
}
</script>
class ShopeeglobalController extends YbController { public function getModel($account) { return new ShopeeGlobal($account); } public function getApimodel($account) { return new ShopeeNewAPI($account); } /* */services/shopee/shopeeglobal/authglobal/id/1 */ public function actionAuthglobal() { $id = Yii::app()->request->getParam('id'); if (!$id) { exit('no account'); } $account = YbModel::model('ShopeeAccountGlobal')->findByPk($id); $api = $this->getModel($account); $url = $api->Auth($account); VHelper::dump($url); } /* */services/shopee/shopeeglobal/gettoken */ public function actionGettoken() { $code = Yii::app()->request->getParam('code'); $id = Yii::app()->request->getParam('id'); $main_id = (int)Yii::app()->request->getParam('main_account_id'); $account = YbModel::model('ShopeeAccountGlobal')->findByPk($id);//"partner_name='{$shop_id}'"); if (!$account) { exit('none'); } $api = $this->getModel($account); $reponse = $api->getToken($code); $reponse = json_decode($reponse, true); if ($reponse['access_token']) { $merchant_id_list = implode(',', $reponse['merchant_id_list']); YbModel::model('ShopeeAccountGlobal')->updateAll([ 'token' => $reponse['access_token'], 'refresh_token' => $reponse['refresh_token'], 'expire_in' => time() + $reponse['expire_in'], ], "merchant_account_id in ({$merchant_id_list})"); VHelper::dump($reponse); } else { VHelper::dump($reponse, ['message' => $reponse['message'], 'error' => $reponse['error']]); } } /* * /services/shopee/shopeeglobal/refreshglobaltoken/id/1 */ public function actionRefreshglobaltoken() { $id = Yii::app()->request->getParam('id'); $time = time() + 20 * 3600; if (!$id) { $account = YbModel::model('ShopeeAccountGlobal')->find("account_status=1 and main_account_id!=0 and expire_in<{$time}"); if (!$account) exit('no account'); } else { $account = YbModel::model('ShopeeAccountGlobal')->findByPk($id); } $api = $this->getModel($account); $reponse = $api->refreshGlobalToken($account); $reponse = json_decode($reponse, true); if ($reponse['access_token']) { YbModel::model('ShopeeAccountGlobal')->updateAll([ 'token' => $reponse['access_token'], 'refresh_token' => $reponse['refresh_token'], 'expire_in' => time() + $reponse['expire_in'] ],"merchant_account_id={$account->merchant_account_id}"); if (!$id) { $url = sprintf('%s/services/shopee/shopeeglobal/refreshglobaltoken', $_SERVER['HTTP_HOST']); MHelper::curl_post_async($url); } VHelper::dump($reponse); } else { VHelper::dump($reponse, ['message' => $reponse['message'], 'error' => $reponse['error']]); } } }
class ShopeeGlobal { private $merchant_id,$partner_id,$secret_key,$main_account_id; function __construct($account){ //获取配置信息 $this->partner_id = intval($account->partner_id); $this->secret_key = trim($account->secret_key); //基础接口域名,可能会根据账号而不同 $this->host = Yii::app()->params['shopee_v2_host_url']; $this->token = $account->token; $this->main_account_id = (int)$account->main_account_id; $this->merchant_id = (int)$account->merchant_account_id; } private function signature($url,$timestamp,$access_token='',$shop_id=''){ $base_string = sprintf("%s%s%s%s%s",$this->partner_id,$url,$timestamp,$access_token,$shop_id); $sign = hash_hmac('sha256', $base_string,$this->secret_key); return $sign; } static function curlPost1($url,$post_data, $header,$timeout=0){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); if($timeout >= 1){ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } if($header){ curl_setopt($ch, CURLOPT_HTTPHEADER,$header); }else{ curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json;', 'Accept:application/json' ) ); } // post数据 curl_setopt($ch, CURLOPT_POST, 1); // post的变量 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); //返回获得的数据 return $output; } public static function http_get($url){ $curl = curl_init(); $headers[] = "Content-type: application/json"; //$headers[] = "Accept:application/json"; curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //curl_setopt($curl, CURLOPT_NOBODY, FALSE); $reponse = curl_exec($curl); curl_close($curl); return json_decode($reponse,true); } public function Auth($account) { $url = '/api/v2/shop/auth_partner'; $redirect ="http://publish.xxxx.com/services/shopee/shopeeglobal/gettoken/id/{$account->id}"; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&redirect={$redirect}&sign={$sign}"; return $url; } public function getToken($code) { $url = '/api/v2/auth/token/get'; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url=$this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}"; $data = [ 'code' => $code, //'shop_id' => (int)$account->shop_id, 'main_account_id'=>$this->main_account_id, 'partner_id' => $this->partner_id ]; $reponse = $this->curlPost1($url,json_encode($data)); return $reponse; } public function refreshGlobalToken($account) { $url = '/api/v2/auth/access_token/get'; $timestamp= time(); $sign = $this->signature($url,$timestamp); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}"; $data = [ 'refresh_token' => $account->refresh_token, 'merchant_id' => (int)$this->merchant_id, 'partner_id' => (int)$this->partner_id ]; $reponse = $this->curlPost1($url,json_encode($data)); return $reponse; } public function getCategory(){ $lanaguage = 'zh-hans'; $url = '/api/v2/global_product/get_category'; //$URL = $this->getParams($account,$this->secret_key,$url); $params['partner_id'] = $this->partner_id; $params['merchant_id'] = $this->merchant_id; $params['access_token']= $this->token; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $URL.= "&language={$lanaguage}"; $reponse = $this->http_get($URL); return $reponse; } public function getAttributes($category_id){ $lanaguage = 'zh-hans'; $url = '/api/v2/global_product/get_attributes'; $timestamp= time(); $params['partner_id'] = $this->partner_id; $params['merchant_id'] = $this->merchant_id; $params['access_token']= $this->token; $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $params['category_id'] = $category_id; $params['language'] = $lanaguage; $params['need_region_mandatory'] = true; $url= $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($url); return $reponse; } public function getBrand($category_id){ $lanaguage = 'zh-hans'; $url = '/api/v2/global_product/get_brand_list'; $timestamp= time(); $params['partner_id'] = $this->partner_id; $params['merchant_id'] = $this->merchant_id; $params['access_token']= $this->token; $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $params['category_id'] = $category_id; $params['offset'] = 0; $params['page_size'] = 20; $params['status'] = 1; $url= $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($url); return $reponse; } public function addItem($data){ $url = '/api/v2/global_product/add_global_item'; $timestamp= time(); $sign = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}&merchant_id={$this->merchant_id}&access_token={$this->token}"; $reponse = $this->curlPost1($url,$data); return $reponse; } public function addModel($data){ $url = '/api/v2/global_product/add_global_model'; $timestamp= time(); $sign = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $url= $this->host.$url."?timestamp={$timestamp}&partner_id={$this->partner_id}&sign={$sign}&merchant_id={$this->merchant_id}&access_token={$this->token}"; $reponse = $this->curlPost1($url,$data); return $reponse; } public function getIteminfo($global_item_id){ $url = '/api/v2/global_product/get_global_item_info'; $params['partner_id'] = $this->partner_id; $params['merchant_id'] = $this->merchant_id; $params['access_token']= $this->token; $params['global_item_id_list']= $global_item_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } public function getModellist($global_item_id){ $url = '/api/v2/global_product/get_global_model_list'; $params['partner_id'] = $this->partner_id; $params['merchant_id'] = $this->merchant_id; $params['access_token']= $this->token; $params['global_item_id']= $global_item_id; $timestamp = time(); $params['sign'] = $this->signature($url,$timestamp,$this->token,$this->merchant_id); $URL = $this->host.$url."?timestamp={$timestamp}&".http_build_query($params); $reponse = $this->http_get($URL); return $reponse; } }
去掉产品名称和介绍的特殊字符
$resdata['response']['item_name'] = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $resdata['response']['item_name']); $resdata['response']['description'] = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $resdata['response']['description']);