yii框架封装拼多多开放平台sdk
yii框架封装拼多多开放平台sdk
<?php
/*** Created by PhpStorm.* User: IT07* Date: 2019/1/11* Time: 14:57*/namespace common\components\vendor;use Hanson\Foundation\Http;
use yii\base\Component;
/*** Class PinDuoDuoOpen* @package common\components\vendor* @property Http $http*/
class PinDuoDuoOpen extends Component
{public $clientId;public $clientSecret;public $redirectUrl;public $memberType;public $needToken = false;/** @var string 拼多多接口地址 */const URL = 'http://gw-api.pinduoduo.com/api/router';/** @var string 获取access_token地址 */const TOKEN_API = 'http://open-api.pinduoduo.com/oauth/token';/** @var array 获取code地址 */const AUTHORIZE_API_ARR = ['MERCHANT' => 'https://mms.pinduoduo.com/open.html?','H5' => 'https://mai.pinduoduo.com/h5-login.html?','JINBAO' => 'https://jinbao.pinduoduo.com/open.html?',];//无需授权/** @var string 获取购买应用订单列表 */const API_PDD_VAS_ORDER_SEARCH = 'pdd.vas.order.search';//需授权/** @var string 获取商品列表 */const API_PDD_GOODS_LIST_GET = 'pdd.goods.list.get';/** @var string 获取商品列表 */const API_PDD_GOODS_DETAIL_GET = 'pdd.goods.detail.get';/** @var string 修改商品信息 */const API_PDD_GOODS_INFORMATION_UPDATE = 'pdd.goods.information.update';/** @var string 上传图片 */const API_PDD_GOODS_IMAGE_UPLOAD = 'pdd.goods.image.upload';/** @var string 商品编辑结果查询 */const API_PDD_GOODS_COMMIT_DETAIL_GET = 'pdd.goods.commit.detail.get';/** @var string 获取店铺信息 */const API_PDD_MALL_INFO_GET = 'pdd.mall.info.get';/** @var array 拼多多请求接口是否需要授权 */const REQUEST_API_LIST_NEED_TOKEN = [self::API_PDD_GOODS_LIST_GET => true,self::API_PDD_GOODS_DETAIL_GET => true,self::API_PDD_GOODS_INFORMATION_UPDATE => true,self::API_PDD_GOODS_IMAGE_UPLOAD => true,self::API_PDD_GOODS_COMMIT_DETAIL_GET => true,self::API_PDD_VAS_ORDER_SEARCH => false,self::API_PDD_MALL_INFO_GET => true,];/** @var int 商品编辑请求失败状态 */const GOODS_UPDATE_COMMIT_FAIL_STATUS = 3;/** @var float|int 拼多多允许上传的图片大小 */const OPEN_PIN_DUO_DUO_UPLOAD_IMG_SIZE = 1024 *1024;/** @var float|int 应用免费试用时间 */const SERVICE_PROBATION_TIME = 15 * 24 * 3600;/** @var string 购买或续费应用入口 */const SERVICE_BUY_ENTRANCE = 'https://mms.pinduoduo.com/service-market/service-detail?detailId=411';/** @var string 服务市场入口 */const SERVICE_MARKET_ENTRANCE = 'https://mms.pinduoduo.com/service-market/';/*** @throws \Exception* @author: SWZ* @time: 14:16* @date: 2019/10/15* @describe:*/public function init(){parent::init();if (!$this->clientId) {throw new \Exception('clientId is empty');}if (!$this->clientSecret) {throw new \Exception('clientSecret is empty');}if (!$this->memberType) {throw new \Exception('memberType is empty');}//$this->redirectUrl = \Yii::$app->request->hostInfo.'/open/access-token/pinDuoDuo';}/*** 生成签名* @param $params* @return string* @author: SWZ* @time: 17:46* @date: 2019/10/22* @describe:*/private function signature($params){ksort($params);$paramsStr = '';array_walk($params, function ($item, $key) use (&$paramsStr) {if ('@' != substr($item, 0, 1)) {$paramsStr .= sprintf('%s%s', $key, $item);}});return strtoupper(md5(sprintf('%s%s%s', $this->clientSecret, $paramsStr, $this->clientSecret)));}/*** 接口请求* @param $method* @param $params* @param string $access_token* @param string $data_type* @return mixed|string* @author: SWZ* @time: 17:49* @date: 2019/10/22* @describe:*/public function request($apiName, $params,$accessToken = '',$data_type = 'JSON'){$http = $this->http;$params = $this->paramsHandle($params);if (static::REQUEST_API_LIST_NEED_TOKEN[$apiName]) {$params['access_token'] = $accessToken;}$params['client_id'] = $this->clientId;$params['sign_method'] = 'md5';$params['type'] = $apiName;$params['data_type'] = $data_type;$params['timestamp'] = strval(time());$params['sign'] = $this->signature($params);$response = call_user_func_array([$http, 'post'], [self::URL, $params]);$responseBody = strval($response->getBody());return strtolower($data_type) == 'json' ? json_decode($responseBody, true) : $responseBody;}/*** @param array $params** @return array*/protected function paramsHandle(array $params){array_walk($params, function (&$item) {if (is_array($item)) {$item = json_encode($item);}if (is_bool($item)) {$item = ['false', 'true'][intval($item)];}});return $params;}/*** 跳转到拼多多授权页面获取code* @param string $state* @param null $view* @author: SWZ* @time: 17:32* @date: 2019/10/22* @describe:*/public function authorizationRedirect($state = 'state', $view = null){$url = $this->authorizationUrl($state, $view);header('Location:'.$url);}/*** 获取拼多多授权登录url* @param null $state* @param null $view* @return string* @author: SWZ* @time: 17:33* @date: 2019/10/22* @describe:*/public function authorizationUrl($state = null, $view = null){return self::AUTHORIZE_API_ARR[strtoupper($this->memberType)].http_build_query(['client_id' => $this->clientId,'response_type' => 'code','state' => $state,'redirect_uri' => $this->redirectUrl,'view' => $view,]);}/*** 获取access_token* @param null $code* @param null $state* @return mixed* @author: SWZ* @time: 17:32* @date: 2019/10/22* @describe:*/public function getAccessToken($code = null, $state = null){return $this->token(['client_id' => $this->clientId,'client_secret' => $this->clientSecret,'grant_type' => 'authorization_code','code' => $code ?: \Yii::$app->request->get('code'),'redirect_uri' => $this->redirectUrl,'state' => $state,]);}/*** 使用refresh_token刷新access_token* @param $refreshToken* @param null $state* @return mixed* @author: SWZ* @time: 17:34* @date: 2019/10/22* @describe:*/public function refreshToken($refreshToken, $state = null){return $this->token(['client_id' => $this->clientId,'client_secret' => $this->clientSecret,'grant_type' => 'refresh_token','refresh_token' => $refreshToken,'state' => $state,]);}public function token($params){$response = $this->http->json(self::TOKEN_API, $params);return json_decode(strval($response->getBody()), true);}/*** 获取http实例* @return Http* @author: SWZ* @time: 17:36* @date: 2019/10/22* @describe:*/public function getHttp(){return new Http();}
}
然后是config配置:
'pinDuoDuoOpen' => ['class' => 'common\components\vendor\PinDuoDuoOpen','clientId' => 'xxxxxxxxx','clientSecret' => 'xxxxxxxxxx','memberType' => 'MERCHANT','redirectUrl' => 'xxxxxxxx',],
使用方式—授权跳转
$pinDuoDuo = \Yii::$app->pinDuoDuoOpen;$pinDuoDuo->authorizationRedirect();
使用方式----获取token:
$pinDuoDuo = \Yii::$app->pinDuoDuoOpen;$tokenData = $pinDuoDuo->getAccessToken();
使用方式-----接口调用:
//拼多多获取商品列表$pinDuoDuo = \Yii::$app->pinDuoDuoOpen;$pinDuoDuoGoodsData = $pinDuoDuo->request(PinDuoDuoOpen::API_PDD_GOODS_LIST_GET, ['page' => $this->page, 'page_size' => $this->page_size], ‘用户的access_token’);
yii框架封装拼多多开放平台sdk相关推荐
- C#开源类库推荐:拼多多开放平台SDK,开源免费,支持.NET Core!
今天推荐一个拼多多开放平台DotNet SDK! 开源地址: https://github.com/niltor/open-pdd-net-sdk 作者:niltor 背景介绍 来源原文链接: htt ...
- 拼多多开放平台订单详情接口解析
API接口订单接口是指用于实现订单相关操作的程序接口.通过这个接口,用户可以实现创建.修改.查询和取消订单等功能. 常见的API接口订单接口包括: 创建订单接口,用于实现用户下单操作. 修改订单接口, ...
- 美团开放平台SDK自动生成技术与实践
美团开放平台为整个美团提供了20+业务场景的开放API,为了使开发者能够快速且安全的接入美团开放平台,美团开放平台提供了多种语言的SDK来提高开发者的接入效率.本文介绍了美团开放平台如何自动生成SDK ...
- 极客日报:美团拼多多等平台下架“一分钱秒杀”;全球大量网站集体宕机,一度无法访问;Swift内置对并发的支持
一分钟速览新闻点! 腾讯 QQ 红包最新支持微信支付 美团拼多多等平台下架"一分钱秒杀" 阿里云在印度尼西亚和菲律宾新增两大数据中心 华为自动驾驶相关专利获授权 可实现全天候全路况 ...
- android微信解析失败,微信开放平台sdk解析失败
时间:2017年7月20日 14:00左右,重新编译了一下Android Studio,报出了一个匪夷所思的错误: 早上还好好的,为啥下午就莫名其妙报出这个错误呢.看错误内容,应该是AS找不到指定的依 ...
- maven已支持支付宝开放平台SDK
「好消息通知」开放平台SDK 标准版 以官方的名义上传了中央仓库! 支持maven引入调用! 开发者可以参考使用中央仓库地址:https://mvnrepository.com/artifact/ ...
- 抖音 -抖店开放平台 SDK 实现及项目工程
抖音 - 抖店开放平台 SDK 抖音 - 抖店开放平台:https://op.jinritemai.com/ 该 SDK 已实现 API 接口调用和消息推送验证解析 项目地址 github: gite ...
- java系统对接方案_阿里巴巴开放平台SDK(java版). 接入授权店铺数据,对接企业内部管理系统的解决方案...
AlibabaSDK-java 阿里巴巴开放平台SDK. 接入授权店铺数据,对接企业内部管理系统的解决方案 获取授权链接 @Test public void testGetPreAuthUrl() t ...
- 淘宝,tmall,1688,抖音,拼多多等平台商品详情接口(网络爬虫数据接口调用示例)接口对接教程
淘宝,tmall,1688,抖音,拼多多等平台商品详情接口(网络爬虫数据接口调用示例)接口对接教程如下: 1.公共参数 名称 类型 必须 描述(接口代码教程wx19970108018) key Str ...
- 腾讯AI开放平台 SDK
腾讯AI开放平台 SDK Tencent AI open platform sdk Special thanks to the generous sponsorship by: Requirement ...
最新文章
- 这就是你在妈妈肚子里尿尿的样子 | 今日最佳
- STL中的lower_bound和upper_bound的理解
- Python高能小技巧:了解bytes与str的区别
- 「Vue」vuex 的使用
- DataFrame 排序
- vue-cli设置跨域代理 + 开发/生成环境简单请求接口设置
- 为什么我离开了管理岗位
- cisco的路由器上rip的被动接口,单播更新,水平分隔
- PSP伊苏VS空之轨迹 游戏ISO文件和完美通关全要素存档
- Laravel文档梳理1、安装与初步配置
- Python之灵异事件
- HP惠普笔记本Microsoft ACPI Compliant System未知设备的解决办法
- APS生产计划排产在装备制造业的应用
- LoadRunner函数大全
- 【web前端基础 | H5】HTML简介
- vscode怎样设置成中文
- 联想y7000笔记如何安装matlab,联想Y7000P笔记本怎样安装win7系统 安装win7系统操作分享...
- 计算机网络--万维网实验
- 28计算机表演赛获奖情况,20届无锡地区计算机表演赛获奖名单
- 猫影视TV 2.0.8 附稳定源地址
热门文章
- C++实现人机对战围棋(使用Leela Zero权重)-自动提子
- linux添加凤凰引导,凤凰系统率先升级内核到Linux4.9
- 苹果6严重卡顿_苹果手机变慢变卡,怎么破?
- jdk32位安装包下载_PS2018下载AdobePhotoshopCC2018安装激活教程
- 热敏电阻测温度c语言编程,NTC热敏电阻测温度 单片机C和汇编源程序
- NetSetMan Pro v5.1.1 网络参数配置工具便携版
- C/C++ typedef用法详解
- 雕虫小技 - 签名图片生成器
- 3D打印文件格式:STL、OBJ、AMF、3MF
- 【推荐系统】协同过滤算法