本文提供基於官方 PHP SDK 的淘寶開放平台 taobao.user.seller.get 介面呼叫完整範例,涵蓋沙箱/正式環境切換、關鍵參數配置與常見坑點。
TopSdk.php 與 top/ 目錄置於專案同層路徑。appkey、appsecret;沙箱測試用官方測試帳號 sandbox_c_1。<?php
header('Content-Type: text/html; charset=utf-8');
require_once 'TopSdk.php';
// 環境切換:修改 TopClient.php 第 8 行 $gatewayUrl
// 沙箱:https://gw.api.tbsandbox.com/router/rest
// 正式:https://gw.api.taobao.com/router/rest
$c = new TopClient();
$c->appkey = '你的_appkey'; // 必填
$c->secretKey = '你的_appsecret'; // 必填
$sessionKey = '授權後的_session_key'; // 沙箱測試帳號授權獲得
$req = new UserSellerGetRequest();
$req->setFields('nick,user_id,type'); // 指定回傳欄位
// $req->setNick('sandbox_c_1'); // 沙箱指定 nick
try {
$resp = $c->execute($req, $sessionKey);
echo 'API 回應:';
print_r($resp);
echo '';
} catch (Exception $e) {
echo '呼叫失敗:' . $e->getMessage();
}
?>
| 項目 | 沙箱環境 | 正式環境 |
|---|---|---|
| 網關網址 | gw.api.tbsandbox.com | gw.api.taobao.com |
| AppKey/Secret | 官方測試值 | 自建應用獲取 |
| SessionKey | 測試帳號授權 | 真實用戶 OAuth 授權 |
| Nick 參數 | sandbox_c_1 | 實際淘寶登入名 |
setFields() 必須包含介面要求的必填欄位。遵循此範例可快速完成淘寶 API 串接,專注業務邏輯開發而非底層通訊細節。