ecomphp / shopee-php
PHP中的非官方Shopee API客户端
v1.1.1
2024-09-25 09:59 UTC
Requires
- php: ^7.2|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.3
Requires (Dev)
- phpunit/phpunit: ^8|^9
README
Shopee客户端是Shopee API的简单SDK实现。
安装
使用Composer安装
composer require ecomphp/shopee-php
配置Shopee PHP客户端
use EcomPHP\Shopee\Client; $partner_id = 'your partner id'; $partner_key = 'your partner key'; $client = new Client($partner_id, $partner_key);
授权令牌
有一个Auth类可以帮助您使用oAuth从商店获取令牌。
$auth = $client->auth();
- 创建认证请求
$redirect_uri = 'http://your-redirect-url.com'; $auth->createAuthRequest($redirect_uri);
如果您想使函数返回认证URL而不是自动重定向,可以将参数 $return(第二个参数)设置为true。
$authUrl = $auth->createAuthRequest($redirect_uri, true); // redirect user to auth url header('Location: '.$authUrl);
- 在应用授权后重定向回
重定向回调URL
获取认证代码,并将其交换为访问令牌
$authorization_code = $_GET['code']; $shop_id = $_GET['shop_id']; $token = $auth->getToken($authorization_code, $shop_id); $access_token = $token['access_token']; $refresh_token = $token['refresh_token']; // save your access_token & refresh_token & shop_id for later use
- 获取授权商店密钥
$access_token = 'your access token'; $shop_id = 'your shop id'; $client->setAccessToken($shop_id, $access_token);
刷新您的访问令牌
访问令牌即将过期,因此您需要使用
refresh_token
刷新新令牌
$new_token = $auth->refreshNewToken($refresh_token, $shop_id); $new_access_token = $new_token['access_token']; $new_refresh_token = $new_token['refresh_token'];
API使用示例
您需要
access_token
和shop_id
才能开始使用Shopee API
$client = new Client($partner_id, $partner_key); $client->setAccessToken($shop_id, $access_token);
- 获取商品列表: API文档
$products = $client->Product->getItemList([ 'offset' => 0, 'page_size' => 50, 'item_status' => 'NORMAL', ]);
- 获取订单列表: API文档
$orders = $client->Order->getOrderList([ 'order_status' => 'READY_TO_SHIP', 'page_size' => 50, ]);