通过 PHP 接入天猫可获取订单,步骤包括:注册天猫开放平台账号。创建应用并获取 App Key 和 App Secret。配置 PHP SDK。初始化 API 客户端。使用 TmallTradeGet 类获取指定订单 ID 的订单数据。

如何通过 PHP 接入天猫获取订单
接入步骤
- 注册天猫开放平台账号:前往天猫开放平台官网(https://open.tmall.com/)注册账号。
- 创建应用:登录开放平台后,点击“创建应用”按钮,填写应用信息创建应用。
- 获取 App Key 和 App Secret:在创建的应用中,获取 App Key 和 App Secret。
-
配置 PHP SDK:使用 PHP SDK 调用天猫 API。可通过 composer 安装 SDK:
composer require alitop/tmall - 初始化 API 客户端:使用 App Key、App Secret 和 API 网关 URL 初始化 API 客户端。
获取订单
初始化 API 客户端后,可以使用 Tmall\Top\Api\TmallTradeGet 类获取订单:
立即学习“PHP免费学习笔记(深入)”;
use Tmall\Top\Api\TmallTradeGet;
use Tmall\Top\Request\TmallTradeGetRequest;
$req = new TmallTradeGetRequest();
$req->setTid(12345678); // 订单 ID
$client = new Tmall\Top\ApiClient();
$client->appkey = 'YOUR_APP_KEY';
$client->secretKey = 'YOUR_APP_SECRET';
$client->gatewayUrl = 'https://eco.taobao.com/router/rest';
$resp = $client->execute($req);
if ($resp->isSuccess()) {
$order = $resp->trade;
// 处理订单数据
}注意事项
- 确保 PHP 版本大于或等于 5.6。
- 正确配置 API 网关 URL,针对国内环境为
https://eco.taobao.com/router/rest,针对国外环境为https://eco.tmall.com/router/rest。 - API 客户端需要支持 SSL 连接。
- 订单 ID 必须存在且有效。











