├── LICENSE
├── README.md
├── api.php
├── auto.php
├── composer.json
├── pdd_token.txt
├── qq.png
└── src
└── Api.php
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 拼多多开放平台API类库
2 |
3 | php封装的拼多多api类库,简单好用,配合文档食用,不用再去记函数名以及参数,还会自动检查接口权限(依赖于授权后token的权限列表)
4 |
5 |
6 | ### 安装
7 |
8 | 直接下载源码或者使用 composer 安装
9 |
10 | ````
11 | {
12 | "require": {
13 | "lovefc/pddapi": "0.0.2"
14 | }
15 | }
16 | ````
17 |
18 | ### 食用方法
19 |
20 | ````
21 |
22 | // 接口配置
23 | $config = array(
24 | 'client_id' => '', //client_id
25 | 'client_secret' => '', //client_secret
26 | 'backurl' => 'http://www.xxxx.com/token.php', //回调地址
27 | 'data_type' => 'json', // 返回数据格式
28 | 'pdd_token_file' => __DIR__ . '/pdd_token.txt', // token存储文件地址
29 | );
30 |
31 | // 实例化参数,有两个,都是数组,第二个参数可以传授权后的token的json字符串,不传会读取token文件,建议授权后使用
32 | $obj = new Pdd\Api($config);
33 |
34 | // 参数
35 | $data = array(
36 | 'order_status' => 1,
37 | 'page' => 1,
38 | 'page_size' => 100
39 | );
40 |
41 | // 这里使用的函数就是拼多多的api接口名
42 | // 参考 https://open.pinduoduo.com/#/apidocument
43 | // 注意把api接口名中的点号换成下划线即可,传参请参考文档
44 |
45 | $json = $obj->pdd_order_number_list_get($data);
46 |
47 | echo $json;
48 |
49 | ````
50 |
51 | ### 交流讨论
52 |
53 | QQ群号:474310505
54 |
55 | 
56 |
--------------------------------------------------------------------------------
/api.php:
--------------------------------------------------------------------------------
1 | 'xxxx', //client_id
12 | 'client_secret' => 'xxxxxx', //client_secret
13 | 'backurl' => 'xxxxx', //回调地址
14 | 'data_type' => 'json', // 返回数据格式
15 | 'pdd_token_file' => dirname(__FILE__) . '/pdd_token.txt', // token存储文件地址
16 | );
17 |
18 | // 加载公共文件,可以使用composer加载
19 | require dirname(__FILE__) . '/src/Api.php';
20 |
21 | // 实例化参数,有两个,都是数组,第二个参数可以传授权后的token的json字符串,不传会读取token文件,建议授权后使用
22 | $obj = new Pdd\Api($config);
23 |
24 | /** 因为拉取订单有时间限制,所以你最好设置出开始和结束时间的时间戳 **/
25 | $time = date("Y-m-d");
26 | $s = "{$time} 00:00:00";
27 | $e = "{$time} 23:59:59";
28 | $stime = strtotime($s);
29 | $etime = strtotime($e);
30 |
31 | // 传参
32 | $data = array(
33 | 'order_status' => 5,
34 | 'refund_status' => 5,
35 | 'start_confirm_at' => $stime,
36 | 'end_confirm_at' => $etime,
37 | 'page' => 1,
38 | 'page_size' => 100
39 | );
40 |
41 | // 这里使用的函数就是拼多多的api接口名
42 | // 参考 https://open.pinduoduo.com/application/document/api?id=pdd.order.list.get
43 | // 注意把api接口名中的点号换成下划线即可,传参请参考文档
44 | $json = $obj->pdd_order_list_get($data);
45 |
46 | echo $json;
47 |
--------------------------------------------------------------------------------
/auto.php:
--------------------------------------------------------------------------------
1 | '', //client_id
12 | 'client_secret' => '', //client_secret
13 | 'backurl' => 'http://www.xxxx.com/token.php', //回调地址
14 | 'data_type' => 'json', // 返回数据格式
15 | 'pdd_token_file' => dirname(__FILE__) . '/pdd_token.txt', // token存储文件地址
16 | );
17 |
18 |
19 | // 加载公共文件,内含文件加载,无需多行引入
20 | require dirname(__FILE__) . '/src/Api.php';
21 |
22 | // 实例化参数,有两个,都是数组,第二个参数可以传授权后的token的json字符串,不传会读取token文件,建议授权后使用
23 | $obj = new Pdd\Api($config);
24 |
25 | $href = $obj->getHref(); // 授权链接地址
26 |
27 |
28 | echo '商家授权
';
29 |
30 | echo '多多客授权';
31 |
32 | /** 检测有没有code的值,一般这个值是回调地址传过来的,我这里只是展示下使用代码 */
33 |
34 | $code = isset($_GET['code']) ? $_GET['code'] : '';
35 |
36 | if (!empty($code)) {
37 | // 获取到access_token
38 | $token = $obj->getToken($code);
39 | echo $token;
40 | // 调用这个方法,将会保存token到你设置的文件。
41 | $obj->saveToken($token);
42 | }
43 |
44 | // 拼多多的token一般都有24个小时的保质期,为了避免过期,可以每隔一段时间,刷新下
45 | // 比如你可以判断文件时间是否过期而刷新
46 | // 你如果有刷新token,就可以直接调用这个方法进行刷新新的token了
47 | /*
48 | $token = $obj->getNewToken();
49 | // 调用这个方法,将会保存token到你设置的文件。。
50 | $obj->saveToken($token);
51 | */
52 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "lovefc/pddapi",
3 | "description": "拼多多开放平台基础api类",
4 | "keywords": ["php","拼多多","拼多多开放平台","拼多多开放平台基础使用api类"],
5 | "version": "0.0.2",
6 | "homepage": "https://github.com/lovefc/pddapi.git",
7 | "license": "MIT",
8 | "require": {
9 | "php": ">=5.3.0"
10 | },
11 | "authors": [
12 | {
13 | "name": "lovefc",
14 | "email": "1102952084@qq.com",
15 | "role": "Developer"
16 | }
17 | ],
18 | "autoload": {
19 | "psr-4": { "Pdd\\": "src"}
20 | }
21 | }
--------------------------------------------------------------------------------
/pdd_token.txt:
--------------------------------------------------------------------------------
1 | {"scope":["pdd.delete.draft.commit","pdd.virtual.mobile.charge.notify","pdd.voucher.virtual.card.verification","pdd.sms.add.crowd","pdd.voucher.appointment.info.send","pdd.goods.relation.set","pdd.voucher.voucher.complain","pdd.goods.country.get","pdd.ad.history.unit.report.get","pdd.ad.history.report.get","pdd.goods.detail.get","pdd.ad.creative.query","pdd.waybill.cancel","pdd.ad.plan.info.list.get","pdd.express.depot.list.get","pdd.sms.short.statistic.query","pdd.wms.order.query","pdd.goods.logistics.ser.template.list","pdd.cloudprint.customares.get","pdd.ad.delete.location.bid","pdd.express.depot.info.get","pdd.goods.list.get","pdd.sms.template.query","pdd.stock.ware.info.list","pdd.ad.history.rt.keyword.report.get","pdd.goods.commit.status.get","pdd.refund.address.list.get","pdd.logistics.online.send","pdd.goods.sale.status.set","pdd.mall.info.bonded.warehouse.get","pdd.stock.ware.sku.update","pdd.pmc.user.get","pdd.wms.order.callback","pdd.goods.edit.goods.commit","pdd.util.divide.base64.image","pdd.ad.history.rt.plan.report.get","pdd.logistics.online.create","pdd.ad.unit.delete","pdd.wms.inborder.confirm","pdd.goods.cats.get","pdd.goods.outer.cat.mapping.get","pdd.sms.crowds.query","pdd.ddy.pdp.user.delete","pdd.invoice.detail.query","pdd.waybill.query.by.waybillcode","pdd.refund.status.check","pdd.ad.balance.get","pdd.ad.creative.create","pdd.ad.delete.bid","pdd.ad.update.location.bid","pdd.ad.creative.report.get","pdd.express.change.depot.info","pdd.ad.keyword.relevance.get","pdd.ad.unit.opt.status.update","pdd.ad.keyword.delete","pdd.sms.sell.record.list.query","pdd.ad.history.rt.report.get","pdd.goods.spec.id.get","pdd.order.number.list.get","pdd.logistics.companies.get","pdd.ad.keyword.rcommended.get","pdd.voucher.voucher.info.send","pdd.wms.returnorder.create","pdd.ad.history.keyword.report.get","pdd.logistics.ordertrace.get","pdd.ddy.pdp.users.get","pdd.promotion.coupon.close","pdd.stock.ware.list","pdd.sms.remain.detail.query","pdd.cloudprint.stdtemplates.get","pdd.refund.list.increment.get","pdd.goods.cps.mall.unit.pause","pdd.gooods.sku.measurement.list","pdd.goods.add","pdd.express.mall.depot.simple.get","pdd.goods.latest.commit.status.get","pdd.waybill.update","pdd.promotion.goods.coupon.list.get","pdd.sms.crowds.people.number.query","pdd.wms.inborder.create","pdd.sms.sell.setting","pdd.ad.creative.update","pdd.wms.returnorder.confirm","pdd.sms.sell.setting.detail.query","pdd.kaola.order.price.get","pdd.wms.outbound.changedate","pdd.stock.goods.id.to.sku.query","pdd.order.number.list.increment.get","pdd.goods.fabric.get","pdd.rdc.pddgenius.sendgoods.cancel","pdd.goods.logistics.template.create","pdd.goods.commit.list.get","pdd.stock.ware.delete","pdd.trade.customs.declare","pdd.goods.information.update","pdd.one.express.cost.template","pdd.sms.custom.setting","pdd.express.search.depot","pdd.wms.deliveryorder.confirm","pdd.logistics.ticket.image.upload","pdd.stock.ware.detail.query","pdd.goods.cps.mall.unit.query","pdd.goods.logistics.template.get","pdd.goods.cps.unit.query","pdd.refund.address.list.modify","pdd.goods.logistics.ser.template.delete","pdd.voucher.physical.goods.send","pdd.ad.bid.query.profile","pdd.ad.history.rt.unit.report.get","pdd.goods.get.relation","pdd.order.status.get","pdd.stock.ware.create","pdd.ad.keyword.create","pdd.goods.image.upload","pdd.wms.ware.synchronize","pdd.sms.sell.deleting","pdd.invoice.application.update","pdd.stock.depot.priority.list","pdd.sms.send.record.list.query","pdd.ad.query.location.bid.pv.list","pdd.goods.logistics.ser.template.create","pdd.finance.balance.daily.bill.url.get","pdd.express.add.depot","pdd.wms.wareinform.send","pdd.goods.authorization.cats","pdd.ad.can.create.plan","pdd.wms.orderstate.send","pdd.invoice.detail.upload","pdd.goods.fabric.content.get","pdd.ad.plan.name.update","pdd.order.information.get","pdd.ad.create.location.bid","pdd.ad.query.bid.list","pdd.ad.keyword.daily.report","pdd.ad.query.location.bid.history.report","pdd.wms.deliveryorder.create","pdd.promotion.merchant.coupon.list.get","pdd.goods.cps.mall.unit.create","pdd.mall.info.get","pdd.goods.sku.price.update","pdd.logistics.address.get","pdd.sms.create.custom.template","pdd.goods.quantity.update","pdd.goods.cps.unit.create","pdd.wms.ouborder.create","pdd.goods.cps.mall.unit.change","pdd.sms.custom.template.query","pdd.goods.cps.mall.unit.resume","pdd.goods.submit.goods.commit","pdd.ad.history.plan.report.get","pdd.pmc.user.permit","pdd.ad.unit.get.by.plan.id","pdd.pmc.user.cancel","pdd.promotion.coupon.quantity.add","pdd.goods.information.get","pdd.ad.plan.opt.status.update","pdd.ad.unit.create","pdd.ad.plan.delete","pdd.ddy.pdp.user.add","pdd.goods.cps.unit.change","pdd.wms.order.cancel","pdd.goods.commit.detail.get","pdd.stock.ware.update","pdd.trade.customs.get","pdd.delete.goods.commit","pdd.order.list.get","pdd.invoice.application.query","pdd.ad.plan.max.cost.update","pdd.sms.remain.setting","pdd.ad.query.unit.bid.history.report","pdd.erp.order.sync","pdd.ad.create.unit.bid","pdd.ad.creative.delete","pdd.waybill.get","pdd.ad.plan.discount.update","pdd.ad.update.bid","pdd.ad.creative.history.report.get","pdd.time.get","pdd.sms.remain.setting.detail.query","pdd.logistics.online.status.query","pdd.goods.video.upload","pdd.stock.depot.priority.update","pdd.ad.plan.create","pdd.ad.keyword.update","pdd.goods.cat.template.get","pdd.goods.logistics.ser.template.update","pdd.goods.spec.get","pdd.refund.address.list.add","pdd.waybill.search","pdd.promotion.goods.coupon.create","pdd.wms.ouborder.confirm","pdd.goods.logistics.ser.template.detail","pdd.promotion.home.coupon.create","pdd.ad.query.location.bid.list","pdd.sms.sell.setting.record.cancel","pdd.stock.ware.move"],"access_token":"","expires_in":31535999,"refresh_token":"","owner_id":"","owner_name":""}
--------------------------------------------------------------------------------
/qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lovefc/pddapi/b673c500ae3439439c1ca806b4d98666b2d729d7/qq.png
--------------------------------------------------------------------------------
/src/Api.php:
--------------------------------------------------------------------------------
1 | configuration($config, $token_json);
47 | $this->restToken();
48 | }
49 | if (!empty($token_json) && is_array($token_json)) {
50 | $this->arrToken($token_json);
51 | }
52 | $this->api_url = 'https://gw-api.pinduoduo.com/api/router';
53 | }
54 |
55 | // 解析配置
56 | public function configuration($config, $token_json)
57 | {
58 | $this->client_id = isset($config['client_id']) ? $config['client_id'] : '';
59 | $this->client_secret = isset($config['client_secret']) ? $config['client_secret'] : '';
60 | $this->backurl = isset($config['backurl']) ? $config['backurl'] : '';
61 | $this->data_type = isset($config['data_type']) ? strtoupper($config['data_type']) : 'JSON';
62 | $this->pdd_token_file = isset($config['pdd_token_file']) ? $config['pdd_token_file'] : '';
63 | $this->poi_key = isset($config['poi_key']) ? $config['poi_key'] : '';
64 | }
65 |
66 | // token转数组
67 | public function arrToken($token_json)
68 | {
69 | $config = json_decode($token_json, true);
70 | $this->expires_in = isset($config['expires_in']) ? $config['expires_in'] : 0;
71 | $this->access_token = isset($config['access_token']) ? $config['access_token'] : '';
72 | $this->refresh_token = isset($config['refresh_token']) ? $config['refresh_token'] : '';
73 | $this->scope = isset($config['scope']) ? $config['scope'] : '';
74 | $this->owner_id = isset($config['owner_id']) ? $config['owner_id'] : '';
75 | $this->owner_name = isset($config['owner_name']) ? $config['owner_name'] : '';
76 | }
77 |
78 | // 获取解析token
79 | public function restToken()
80 | {
81 | if (is_file($this->pdd_token_file) && empty($this->access_token)) {
82 | $token_json = file_get_contents($this->pdd_token_file);
83 | $this->arrToken($token_json);
84 | }
85 | }
86 |
87 | // 保存token
88 | public function saveToken($str)
89 | {
90 | $arr = json_decode($str, true);
91 | $token = isset($arr['access_token']) ? $arr['access_token'] : false;
92 | if ($token) {
93 | file_put_contents($this->pdd_token_file, $str);
94 | $this->restToken();
95 | return true;
96 | }
97 | return false;
98 | }
99 |
100 | // 创建密匙
101 | public function creSign($query)
102 | {
103 | ksort($query);
104 | $str = '';
105 | foreach ($query as $k => $v) {
106 | $str .= "{$k}{$v}";
107 | }
108 | return strtoupper(md5($this->client_secret . $str . $this->client_secret));
109 | }
110 |
111 | // 生成链接
112 | public function creQuery($data)
113 | {
114 | $arr = array(
115 | 'data_type' => $this->data_type,
116 | 'timestamp' => time(),
117 | 'client_id' => $this->client_id,
118 | 'access_token' => $this->access_token,
119 | );
120 | $data += $arr;
121 | $sign = $this->creSign($data);
122 | $data['sign'] = $sign;
123 | return $data;
124 | }
125 |
126 | // 提交请求
127 | public function post($url, $data = '', $head = 'application/x-www-form-urlencoded')
128 | {
129 | $ch = curl_init();
130 | curl_setopt($ch, CURLOPT_URL, $url);
131 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:{$head};charset=utf-8;"));
132 | curl_setopt($ch, CURLOPT_TIMEOUT, 30);
133 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
134 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
135 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
136 | curl_setopt($ch, CURLOPT_HEADER, 0);
137 | if (!empty($data)) {
138 | curl_setopt($ch, CURLOPT_POST, 1);
139 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
140 | }
141 | $output = curl_exec($ch);
142 | if ($output === false) {
143 | $this->error('接口错误'.curl_error($ch));
144 | }
145 | curl_close($ch);
146 | return $output;
147 | }
148 |
149 | // 组合提交
150 | public function submit($data)
151 | {
152 | $data = $this->creQuery($data);
153 | $data = http_build_query($data, null, '&');
154 | return $this->post($this->api_url, $data);
155 | }
156 |
157 | // 检测是否有接口权限
158 | public function checkApi($name)
159 | {
160 | if (!$this->scope) {
161 | return true;
162 | }
163 | if (!in_array($name, $this->scope)) {
164 | return false;
165 | }
166 | return true;
167 | }
168 |
169 | // 魔术方法,自动判断接口权限并执行函数
170 | public function __call($method, $args)
171 | {
172 | $name = str_replace('_', '.', $method);
173 | if($this->checkApi($name) === false){
174 | $this->error("没有{$name}接口调用权限");
175 | }
176 | return $this->runPddApi($name, $args);
177 | }
178 |
179 | // 执行pddapi
180 | public function runPddApi($name, $data = '')
181 | {
182 | $query = array(
183 | 'type' => $name,
184 | );
185 | if (!empty($data)) {
186 | $query = $query + $data[0];
187 | }
188 | return $this->submit($query);
189 | }
190 |
191 | //生成登录链接
192 | public function getHref($type = '')
193 | {
194 | $query = 'response_type=code&client_id=' . $this->client_id . '&redirect_uri=' . urlencode($this->backurl) . '&state=1212';
195 | if ($type == 'ddk') {
196 | return 'https://jinbao.pinduoduo.com/open.html?' . $query; // 拼客客
197 | }
198 | if ($this->isMobile() != true) {
199 | $url = 'https://mms.pinduoduo.com/open.html?' . $query; // pc端
200 | } else {
201 | $url = 'https://mai.pinduoduo.com/h5-login.html?' . $query . '&view=h5'; // 手机端
202 | }
203 | return $url;
204 | }
205 |
206 | //根据code取登录token
207 | public function getToken($code)
208 | {
209 | $url = 'http://open-api.pinduoduo.com/oauth/token';
210 | $data = array(
211 | "client_id" => $this->client_id,
212 | "code" => $code,
213 | "grant_type" => "authorization_code",
214 | "client_secret" => $this->client_secret,
215 | );
216 | $data = json_encode($data);
217 | $head = 'application/json';
218 | return $this->post($url, $data, $head);
219 | }
220 |
221 | //刷新token
222 | public function getNewToken($state = 1212)
223 | {
224 | $url = 'http://open-api.pinduoduo.com/oauth/token';
225 | $data = array(
226 | "client_id" => $this->client_id,
227 | "client_secret" => $this->client_secret,
228 | "grant_type" => "refresh_token",
229 | "refresh_token" => $this->refresh_token,
230 | "state" => $state,
231 | );
232 | $data = json_encode($data);
233 | $head = 'application/json';
234 | return $this->post($url, $data, $head);
235 | }
236 |
237 | // 判断是否手机访问
238 | public function isMobile()
239 | {
240 | $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
241 | $mobile_browser = '0';
242 | if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
243 | $mobile_browser++;
244 | if ((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') !== false))
245 | $mobile_browser++;
246 | if (isset($_SERVER['HTTP_X_WAP_PROFILE']))
247 | $mobile_browser++;
248 | if (isset($_SERVER['HTTP_PROFILE']))
249 | $mobile_browser++;
250 | $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
251 | $mobile_agents = array(
252 | 'w3c ', 'acs-', 'alav', 'alca', 'amoi', 'audi', 'avan', 'benq', 'bird', 'blac',
253 | 'blaz', 'brew', 'cell', 'cldc', 'cmd-', 'dang', 'doco', 'eric', 'hipt', 'inno',
254 | 'ipaq', 'java', 'jigs', 'kddi', 'keji', 'leno', 'lg-c', 'lg-d', 'lg-g', 'lge-',
255 | 'maui', 'maxo', 'midp', 'mits', 'mmef', 'mobi', 'mot-', 'moto', 'mwbp', 'nec-',
256 | 'newt', 'noki', 'oper', 'palm', 'pana', 'pant', 'phil', 'play', 'port', 'prox',
257 | 'qwap', 'sage', 'sams', 'sany', 'sch-', 'sec-', 'send', 'seri', 'sgh-', 'shar',
258 | 'sie-', 'siem', 'smal', 'smar', 'sony', 'sph-', 'symb', 't-mo', 'teli', 'tim-',
259 | 'tosh', 'tsm-', 'upg1', 'upsi', 'vk-v', 'voda', 'wap-', 'wapa', 'wapi', 'wapp',
260 | 'wapr', 'webc', 'winw', 'winw', 'xda', 'xda-'
261 | );
262 | if (in_array($mobile_ua, $mobile_agents))
263 | $mobile_browser++;
264 | if (strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
265 | $mobile_browser++;
266 | if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
267 | $mobile_browser = 0;
268 | if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
269 | $mobile_browser++;
270 | if ($mobile_browser > 0)
271 | return true;
272 | else
273 | return false;
274 | }
275 |
276 | //打印错误
277 | public function error($error,$error_code = 1)
278 | {
279 | $error = array(
280 | 'error_msg' => $error,
281 | 'error_code' => $error_code
282 |
283 | );
284 | die(json_encode($error));
285 | }
286 | }
287 |
--------------------------------------------------------------------------------