muhanz/shoapi

Laravel Shopee 开放 API SDK

v1.0.1 2023-10-13 19:23 UTC

This package is auto-updated.

Last update: 2024-09-16 07:13:20 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require StyleCI

Laravel Shopee API SDK (V2)

这是一个为 Laravel 9|10 定制的非官方 Shopee PHP 客户端,目前支持 API V2

ShoAPi 允许您在 Laravel 中开发 Shopee 开放 API。ShoAPi 是为对 Shopee 开放 API 感兴趣的开发者创建的基于 HTTP 的接口。要了解更多关于 Shopee 开放 API 的信息,请参阅官方 Shopee 开放平台 网站的介绍。

安装

通过 Composer

$ composer require muhanz/shoapi

开始之前

在您的 .env 中配置变量(推荐)或您可以发布配置文件并在那里更改它。

SHOPEE_PRODUCTION=false // Default
SHOPEE_HOST_URL='https://partner.shopeemobile.com'
SHOPEE_SANDBOX_HOST_URL='https://partner.test-stable.shopeemobile.com'
SHOPEE_CALLBACK_URL='/shopee/auth/get_access_token' // Example: for redirect after auth
SHOPEE_PARTNER_ID=<YOUR_PARTNER_ID>
SHOPEE_PARTNER_KEY=<YOUR_PARTNER_KEY>

(可选)您可以通过此命令发布配置文件

php artisan vendor:publish --provider="Muhanz\Shoapi\ShoapiServiceProvider" --tag="shoapi.config"

授权和认证

// Add this facades on top controller
use Muhanz\Shoapi\Facades\Shoapi;

public function auth_partner()
{
    return Shoapi::call('shop')->access('auth_partner')->redirect(); // auto redirect
    
    // or
    
    return Shoapi::call('shop')->access('auth_partner')->getUrl();
    
    // response:
    // https://partner.test-stable.shopeemobile.com/api/v2/shop/auth_partner?partner_id=partner_idtimestamp=1697215282&sign=sign_code&redirect=redirect_url   
}

取消/断开认证合作伙伴

return Shoapi::call('shop')->access('cancel_auth_partner')->redirect();

获取访问令牌 & 刷新访问令牌

访问令牌是一个动态令牌,您需要传递访问令牌来调用非公开 API。每个访问令牌有效期为 4 小时,并在 4 小时 内可以使用多次。然而,您需要在访问令牌过期之前通过调用 RefreshAccessToken 来刷新访问令牌,以获取新的访问令牌。

刷新令牌是用于刷新访问令牌的参数。每个刷新令牌有效期为 30 天

⚠️ 注意:每个 shop_id 和 merchant_id 的访问令牌和刷新令牌需要分别保存。

获取访问令牌

// After authorization, the front-end page will redirect to the redirect URL in your authorization link:
// https://open.shopee.com/?code=xxxxxxxxxx&shop_id=xxxxxx

$params =  [
	'code'  =>  "54494572544875766********", 
	'shop_id'  =>  (int)  000000,
];

$response = Shoapi::call('auth')
		->access('get_access_token')
		->shop(000000)
		->request($params)
		->response();
				
dd($response);

获取刷新访问令牌

$params =  [
	'refresh_token'  =>  "527a424f54494572544875766*******",
	'shop_id'  =>  (int)  000000,
];

$response =  Shoapi::call('auth')
		->access('refresh_access_token')
		->shop(000000)
		->request($params)
		->response();

dd($response);

示例响应

{
	api_status:"success",
	partner_id:000000,
	refresh_token:"527a424f54494572544875766*******",
	access_token:"786b4c74526e52426555616e*******",
	expire_in:14400,
	request_id:"84ec4d8971735d62dca40c0*******",
	shop_id:000000
}

基本用法

// Add this facades on top controller
use Muhanz\Shoapi\Facades\Shoapi;

public function get_shop_info()
{
	
	// path api: /api/v2/shop(use in call)/get_shop_info(use in access)

  return Shoapi::call('shop')
		->access('get_shop_info', <YOUR_ACCESS_TOKEN>)
		->shop(<YOUR_SHOP_ID>)
		->response();
}

其他示例

// Add this facades on top controller
use Muhanz\Shoapi\Facades\Shoapi;

public  function  get_category()
{

	$params =  [
		'language'  =>  'id'  // en
	];

	$response = Shoapi::call('product')
			->access('get_category',  <YOUR_ACCESS_TOKEN>)
			->shop(<YOUR_SHOP_ID>)
			->request($params)
			->response();
	dd($response);
}

示例响应

{
	api_status:"success",
	category_list:[
		{
			category_id:100001,
			parent_category_id:0,
			original_category_name:"Health",
			display_category_name:"Kesehatan",
			has_children:true

		}
	]
}

支持方法

目前支持所有方法,除了 GlobalProduct(CB卖家专用),请打开 Shopee 开放平台

文档

请参阅官方 Shopee 开放平台 的文档

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

请参阅 contributing.md 了解详细信息及待办事项列表

鸣谢

许可证

MIT。有关更多信息,请参阅 许可证文件