├── .gitignore ├── .gitattributes ├── tests └── index.php ├── src ├── Kernel │ ├── Exceptions │ │ ├── RuntimeException.php │ │ └── Exception.php │ ├── ServiceContainer.php │ ├── Support │ │ ├── Helpers.php │ │ └── Str.php │ └── BaseClient.php ├── Tbk │ ├── Dg │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Ju │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Sc │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Item │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Order │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Shop │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Tpwd │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Uatm │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Coupon │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Rebate │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Spread │ │ ├── ServiceProvider.php │ │ └── Client.php │ ├── Content │ │ ├── ServiceProvider.php │ │ └── Client.php │ └── Application.php └── Factory.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /composer.lock 3 | .idea/ -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /tests/index.php: -------------------------------------------------------------------------------- 1 | '', 8 | 'secretKey' => '', 9 | 'format' => 'json', 10 | 'session' => '', 11 | 'sandbox' => false, 12 | ]; 13 | $app = Factory::Tbk($config); 14 | $param = [ 15 | 'text' => '长度大于5个字符', 16 | 'url' => 'https://uland.taobao.com/' 17 | ]; 18 | $res = $app->tpwd->create($param); 19 | 20 | 21 | print_r($res); 22 | -------------------------------------------------------------------------------- /src/Kernel/Exceptions/RuntimeException.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel\Exceptions; 13 | 14 | /** 15 | * Class RuntimeException. 16 | * 17 | * @author ennnnny 18 | */ 19 | class RuntimeException extends Exception 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ennnnny/tbk", 3 | "description": "简约优雅的淘宝客SDK", 4 | "keywords": ["淘宝客", "tbk", "sdk"], 5 | "type": "library", 6 | "license": "MIT", 7 | "require": { 8 | "php": ">=5.6.0", 9 | "pimple/pimple": "^3.0", 10 | "ext-json": "*" 11 | }, 12 | "autoload": { 13 | "psr-4": { 14 | "ETaobao\\": "src/" 15 | }, 16 | "files": [ 17 | "src/Kernel/Support/Helpers.php" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Kernel/Exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel\Exceptions; 13 | 14 | use Exception as BaseException; 15 | 16 | /** 17 | * Class Exception. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Exception extends BaseException 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /src/Tbk/Dg/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Dg; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['dg'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Ju/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Ju; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['ju'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Sc/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Sc; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['sc'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Item/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Item; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['item'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Order/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Order; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['order'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Shop/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Shop; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['shop'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Tpwd/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Tpwd; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['tpwd'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Uatm/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Uatm; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['uatm'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Coupon/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Coupon; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['coupon'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Rebate/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Rebate; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['rebate'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Spread/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Spread; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['spread'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Content/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Content; 13 | 14 | use Pimple\Container; 15 | use Pimple\ServiceProviderInterface; 16 | 17 | /** 18 | * Class ServiceProvider. 19 | * 20 | * @author ennnnny 21 | */ 22 | class ServiceProvider implements ServiceProviderInterface 23 | { 24 | /** 25 | * {@inheritdoc}. 26 | */ 27 | public function register(Container $app) 28 | { 29 | $app['content'] = function ($app) { 30 | return new Client($app); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tbk/Ju/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Ju; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | 24 | /** 25 | * taobao.tbk.ju.tqg.get (淘抢购api) 26 | * @line https://open.taobao.com/api.htm?docId=27543&docType=2 27 | * @param array $params 28 | * @return array|mixed|\SimpleXMLElement|string 29 | */ 30 | public function getTqg(array $params) 31 | { 32 | $res = $this->httpPost('taobao.tbk.ju.tqg.get', $params); 33 | return $res; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/Tbk/Coupon/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Coupon; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.coupon.get (淘宝客-公用-阿里妈妈推广券详情查询) 25 | * @line https://open.taobao.com/api.htm?docId=31106&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function get(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.coupon.get', $params); 32 | return $res; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Tbk/Tpwd/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Tpwd; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | 24 | /** 25 | * taobao.tbk.tpwd.create (淘宝客-公用-淘口令生成) 26 | * @line https://open.taobao.com/api.htm?docId=31127&docType=2 27 | * @param array $params 28 | * @return array|mixed|\SimpleXMLElement|string 29 | */ 30 | public function create(array $params) 31 | { 32 | $res = $this->httpPost('taobao.tbk.tpwd.create', $params); 33 | return $res; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 叶子坑 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Factory.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao; 13 | 14 | /** 15 | * Class Factory. 16 | * 17 | * @method static Tbk\Application tbk(array $config) 18 | */ 19 | class Factory 20 | { 21 | /** 22 | * @param string $name 23 | * @param array $config 24 | * 25 | * @return \ETaobao\Kernel\ServiceContainer 26 | */ 27 | public static function make($name, array $config) 28 | { 29 | $namespace = Kernel\Support\Str::studly($name); 30 | $application = "\\ETaobao\\{$namespace}\\Application"; 31 | 32 | return new $application($config); 33 | } 34 | 35 | /** 36 | * Dynamically pass methods to the application. 37 | * 38 | * @param string $name 39 | * @param array $arguments 40 | * 41 | * @return mixed 42 | */ 43 | public static function __callStatic($name, $arguments) 44 | { 45 | return self::make($name, ...$arguments); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Tbk/Spread/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Spread; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.spread.get (淘宝客-公用-长链转短链) 25 | * @line https://open.taobao.com/api.htm?docId=27832&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function getSpread(array $params) 30 | { 31 | if (isset($params['requests']) && is_array($params['requests'])) { 32 | foreach ($params['requests'] as &$item) { 33 | $item = json_encode($item); 34 | } 35 | $data['requests'] = json_encode($params['requests']); 36 | } else { 37 | $data['requests'] = $params; 38 | } 39 | $res = $this->httpPost('taobao.tbk.spread.get', $params); 40 | return $res; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Tbk/Shop/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Shop; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.shop.get (淘宝客-推广者-店铺搜索) 25 | * @line https://open.taobao.com/api.htm?docId=24521&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function get(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.shop.get', $params); 32 | return $res; 33 | } 34 | 35 | /** 36 | * taobao.tbk.shop.recommend.get (淘宝客-公用-店铺关联推荐) 37 | * @line https://open.taobao.com/api.htm?docId=24522&docType=2 38 | * @param array $params 39 | * @return array|mixed|\SimpleXMLElement|string 40 | */ 41 | public function getRecommend(array $params) 42 | { 43 | $res = $this->httpPost('taobao.tbk.shop.recommend.get', $params); 44 | return $res; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Tbk/Rebate/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Rebate; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.rebate.auth.get (淘宝客-推广者-返利商家授权查询) 25 | * @line https://open.taobao.com/api.htm?docId=24525&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function getAuth(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.rebate.auth.get', $params); 32 | return $res; 33 | } 34 | 35 | /** 36 | * taobao.tbk.rebate.order.get (淘宝客-推广者-返利订单查询) 37 | * @line https://open.taobao.com/api.htm?docId=24526&docType=2 38 | * @param array $params 39 | * @return array|mixed|\SimpleXMLElement|string 40 | */ 41 | public function getOrder(array $params) 42 | { 43 | $res = $this->httpPost('taobao.tbk.rebate.order.get', $params); 44 | return $res; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Tbk/Application.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk; 13 | 14 | use ETaobao\Kernel\ServiceContainer; 15 | 16 | /** 17 | * Class Application. 18 | * 19 | * @author overtrue 20 | * 21 | * @property \ETaobao\Tbk\Item\Client $item 22 | * @property \ETaobao\Tbk\Shop\Client $shop 23 | * @property \ETaobao\Tbk\Rebate\Client $rebate 24 | * @property \ETaobao\Tbk\Uatm\Client $uatm 25 | * @property \ETaobao\Tbk\Ju\Client $ju 26 | * @property \ETaobao\Tbk\Spread\Client $spread 27 | * @property \ETaobao\Tbk\Dg\Client $dg 28 | * @property \ETaobao\Tbk\Coupon\Client $coupon 29 | * @property \ETaobao\Tbk\Tpwd\Client $tpwd 30 | * @property \ETaobao\Tbk\Content\Client $content 31 | * @property \ETaobao\Tbk\Sc\Client $sc 32 | * @property \ETaobao\Tbk\Order\Client $order 33 | */ 34 | class Application extends ServiceContainer 35 | { 36 | /** 37 | * @var array 38 | */ 39 | protected $providers = [ 40 | Item\ServiceProvider::class, 41 | Shop\ServiceProvider::class, 42 | Rebate\ServiceProvider::class, 43 | Uatm\ServiceProvider::class, 44 | Ju\ServiceProvider::class, 45 | Spread\ServiceProvider::class, 46 | Dg\ServiceProvider::class, 47 | Coupon\ServiceProvider::class, 48 | Tpwd\ServiceProvider::class, 49 | Content\ServiceProvider::class, 50 | Sc\ServiceProvider::class, 51 | Order\ServiceProvider::class, 52 | ]; 53 | } 54 | -------------------------------------------------------------------------------- /src/Tbk/Order/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Order; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | 24 | /** 25 | * taobao.tbk.order.get (淘宝客订单查询) 26 | * @line https://open.taobao.com/api.htm?docId=24527&docType=2&scopeId=11650 27 | * @param array $params 28 | * @return array|mixed|\SimpleXMLElement|string 29 | */ 30 | public function get(array $params) 31 | { 32 | $res = $this->httpPost('taobao.tbk.order.get', $params); 33 | return $res; 34 | } 35 | 36 | /** 37 | * taobao.tbk.relation.refund (淘宝客-推广者-维权退款订单查询) 38 | * @line https://open.taobao.com/api.htm?docId=40121&docType=2 39 | * @param array $params 40 | * @return array|mixed|\SimpleXMLElement|string 41 | */ 42 | public function getRefund(array $params) 43 | { 44 | if (isset($params['search_option']) && is_array($params['search_option'])) { 45 | $data['search_option'] = json_encode($params['search_option']); 46 | } elseif (is_array($params)) { 47 | $data['search_option'] = json_encode($params); 48 | } else { 49 | $data['search_option'] = $params; 50 | } 51 | $res = $this->httpPost('taobao.tbk.relation.refund', $data); 52 | return $res; 53 | } 54 | 55 | /** 56 | * taobao.tbk.order.details.get (淘宝客-推广者-所有订单查询) 57 | * @line https://open.taobao.com/api.htm?docId=43328&docType=2 58 | * @param array $params 59 | * @return array|mixed|\SimpleXMLElement|string 60 | */ 61 | public function getDetails(array $params) 62 | { 63 | $res = $this->httpPost('taobao.tbk.order.details.get', $params); 64 | return $res; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Tbk/Uatm/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Uatm; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.uatm.event.get (枚举正在进行中的定向招商的活动列表) 25 | * @line http://open.taobao.com/docs/api.htm?apiId=26449&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function getEvent(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.uatm.event.get', $params); 32 | return $res; 33 | } 34 | 35 | /** 36 | * taobao.tbk.uatm.event.item.get (获取淘宝联盟定向招商的宝贝信息) 37 | * @line http://open.taobao.com/docs/api.htm?apiId=26616&docType=2 38 | * @param array $params 39 | * @return array|mixed|\SimpleXMLElement|string 40 | */ 41 | public function getItemEvent(array $params) 42 | { 43 | $res = $this->httpPost('taobao.tbk.uatm.event.item.get', $params); 44 | return $res; 45 | } 46 | 47 | /** 48 | * taobao.tbk.uatm.favorites.item.get (淘宝客-推广者-选品库宝贝信息) 49 | * @line https://open.taobao.com/api.htm?docId=26619&docType=2 50 | * @param array $params 51 | * @return array|mixed|\SimpleXMLElement|string 52 | */ 53 | public function getItemFavorites(array $params) 54 | { 55 | $res = $this->httpPost('taobao.tbk.uatm.favorites.item.get', $params); 56 | return $res; 57 | } 58 | 59 | /** 60 | * taobao.tbk.uatm.favorites.get (淘宝客-推广者-选品库宝贝列表) 61 | * @line https://open.taobao.com/api.htm?docId=26620&docType=2 62 | * @param array $params 63 | * @return array|mixed|\SimpleXMLElement|string 64 | */ 65 | public function getFavorites(array $params) 66 | { 67 | $res = $this->httpPost('taobao.tbk.uatm.favorites.get', $params); 68 | return $res; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Tbk/Content/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Content; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.content.get (淘宝客-推广者-图文内容输出) 25 | * @line https://open.taobao.com/api.htm?docId=31137&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function get(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.content.get', $params); 32 | return $res; 33 | } 34 | 35 | /** 36 | * taobao.tbk.activitylink.get (淘宝客-推广者-官方活动转链) 37 | * @line https://open.taobao.com/api.htm?docId=41918&docType=2 38 | * @param array $params 39 | * @return array|mixed|\SimpleXMLElement|string 40 | */ 41 | public function getActivityLink(array $params) 42 | { 43 | $res = $this->httpPost('taobao.tbk.activitylink.get', $params); 44 | return $res; 45 | } 46 | 47 | /** 48 | * taobao.tbk.content.effect.get (淘宝客-推广者-图文内容效果数据) 49 | * @link https://open.taobao.com/api.htm?docId=37130&docType=2 50 | * @param array $params 51 | * @return array|mixed|\SimpleXMLElement|string 52 | */ 53 | public function getEffect(array $params) 54 | { 55 | if (isset($params['option']) && is_array($params['option'])) { 56 | $data['option'] = json_encode($params['option']); 57 | } elseif (is_array($params)) { 58 | $data['option'] = json_encode($params); 59 | } else { 60 | $data['option'] = $params; 61 | } 62 | $res = $this->httpPost('taobao.tbk.content.effect.get', $data); 63 | return $res; 64 | } 65 | 66 | /** 67 | * taobao.tbk.activity.info.get( 淘宝客-推广者-官方活动信息获取 ) 68 | * @line https://open.taobao.com/api.htm?docId=48340&docType=2 69 | * @param array $params 70 | * @return array|mixed|\SimpleXMLElement|string|null 71 | */ 72 | public function getActivityInfo(array $params) 73 | { 74 | $res = $this->httpPost('taobao.tbk.activity.info.get', $params); 75 | return $res; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Kernel/ServiceContainer.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel; 13 | 14 | use Pimple\Container; 15 | 16 | /** 17 | * Class ServiceContainer. 18 | * 19 | * @author ennnnny 20 | * 21 | */ 22 | class ServiceContainer extends Container 23 | { 24 | /** 25 | * @var array 26 | */ 27 | protected $providers = []; 28 | 29 | /** 30 | * @var array 31 | */ 32 | protected $defaultConfig = [ 33 | 'appkey' => '', 34 | 'secretKey' => '', 35 | 'api_uri' => 'https://eco.taobao.com/router/rest', 36 | 'api_box_uri' => 'https://gw.api.tbsandbox.com/router/rest', 37 | 'format' => 'json', 38 | 'signMethod' => 'md5', 39 | 'apiVersion' => '2.0', 40 | 'sandbox' => false, 41 | ]; 42 | 43 | /** 44 | * @var array 45 | */ 46 | protected $userConfig = []; 47 | 48 | /** 49 | * Constructor. 50 | * 51 | * @param array $config 52 | * @param array $prepends 53 | */ 54 | public function __construct(array $config = [], array $prepends = []) 55 | { 56 | $this->registerProviders($this->getProviders()); 57 | 58 | parent::__construct($prepends); 59 | 60 | if (isset($config['signMethod'])) { 61 | unset($config['signMethod']); 62 | } 63 | $this->userConfig = $config; 64 | 65 | } 66 | 67 | /** 68 | * @return array 69 | */ 70 | public function getConfig() 71 | { 72 | return array_merge($this->defaultConfig, $this->userConfig); 73 | } 74 | 75 | /** 76 | * Return all providers. 77 | * 78 | * @return array 79 | */ 80 | public function getProviders() 81 | { 82 | return array_merge([], $this->providers); 83 | } 84 | 85 | /** 86 | * Magic get access. 87 | * 88 | * @param string $id 89 | * 90 | * @return mixed 91 | */ 92 | public function __get($id) 93 | { 94 | return $this->offsetGet($id); 95 | } 96 | 97 | /** 98 | * Magic set access. 99 | * 100 | * @param string $id 101 | * @param mixed $value 102 | */ 103 | public function __set($id, $value) 104 | { 105 | $this->offsetSet($id, $value); 106 | } 107 | 108 | /** 109 | * @param array $providers 110 | */ 111 | public function registerProviders(array $providers) 112 | { 113 | foreach ($providers as $provider) { 114 | parent::register(new $provider()); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Tbk/Item/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Item; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | 24 | /** 25 | * taobao.tbk.item.get (淘宝客商品查询) 26 | * @line http://open.taobao.com/docs/api.htm?apiId=24515&docType=2 27 | * @param array $params 28 | * @return array|mixed|\SimpleXMLElement|string 29 | */ 30 | public function get(array $params) 31 | { 32 | $res = $this->httpPost('taobao.tbk.item.get', $params); 33 | return $res; 34 | } 35 | 36 | /** 37 | * taobao.tbk.item.convert (淘宝客-推广者-商品链接转换) 38 | * @line https://open.taobao.com/api.htm?docId=24516&docType=2 39 | * @param array $params 40 | * @return array|mixed|\SimpleXMLElement|string 41 | */ 42 | public function convert(array $params) 43 | { 44 | $res = $this->httpPost('taobao.tbk.item.convert', $params); 45 | return $res; 46 | } 47 | 48 | /** 49 | * taobao.tbk.item.recommend.get (淘宝客-公用-商品关联推荐) 50 | * @line https://open.taobao.com/api.htm?docId=24517&docType=2 51 | * @param array $params 52 | * @return array|mixed|\SimpleXMLElement|string 53 | */ 54 | public function getRecommend(array $params) 55 | { 56 | $res = $this->httpPost('taobao.tbk.item.recommend.get', $params); 57 | return $res; 58 | } 59 | 60 | /** 61 | * taobao.tbk.item.info.get (淘宝客-公用-淘宝客商品详情查询(简版)) 62 | * @line https://open.taobao.com/api.htm?docId=24518&docType=2 63 | * @param array $params 64 | * @return array|mixed|\SimpleXMLElement|string 65 | */ 66 | public function getInfo(array $params) 67 | { 68 | $res = $this->httpPost('taobao.tbk.item.info.get', $params); 69 | return $res; 70 | } 71 | 72 | /** 73 | * taobao.tbk.item.guess.like (淘宝客商品猜你喜欢) 74 | * @line http://open.taobao.com/docs/api.htm?apiId=29528&docType=2 75 | * @param array $params 76 | * @return array|mixed|\SimpleXMLElement|string 77 | */ 78 | public function likeGuess(array $params) 79 | { 80 | $res = $this->httpPost('taobao.tbk.item.guess.like', $params); 81 | return $res; 82 | } 83 | 84 | /** 85 | * 从长链接或短链接中解析出open_iid 86 | * taobao.tbk.item.click.extract(淘宝客-公用-链接解析出商品id) 87 | * @line https://open.taobao.com/api.htm?docId=28156&docType=2 88 | * @param array $params 89 | * @return array|mixed|\SimpleXMLElement|string 90 | */ 91 | public function clickExtract(array $params) 92 | { 93 | $res = $this->httpPost('taobao.tbk.item.click.extract', $params); 94 | return $res; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/Kernel/Support/Helpers.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel\Support; 13 | 14 | /* 15 | * helpers. 16 | * 17 | * @author ennnnny 18 | */ 19 | 20 | /** 21 | * Generate a signature. 22 | * 23 | * @param array $attributes 24 | * @param string $key 25 | * @param string $encryptMethod 26 | * 27 | * @return string 28 | */ 29 | function generate_sign(array $attributes, $key, $encryptMethod = 'md5') 30 | { 31 | ksort($attributes); 32 | 33 | $attributes['key'] = $key; 34 | 35 | return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))])); 36 | } 37 | 38 | function generateSign(array $attributes,$secretKey) 39 | { 40 | ksort($attributes); 41 | 42 | $stringToBeSigned = $secretKey; 43 | foreach ($attributes as $k => $v) 44 | { 45 | if(!is_array($v) && "@" != substr($v, 0, 1)) 46 | { 47 | $stringToBeSigned .= "$k$v"; 48 | } 49 | } 50 | unset($k, $v); 51 | $stringToBeSigned .= $secretKey; 52 | 53 | return strtoupper(md5($stringToBeSigned)); 54 | } 55 | 56 | /** 57 | * Get client ip. 58 | * 59 | * @return string 60 | */ 61 | function get_client_ip() 62 | { 63 | if (!empty($_SERVER['REMOTE_ADDR'])) { 64 | $ip = $_SERVER['REMOTE_ADDR']; 65 | } else { 66 | // for php-cli(phpunit etc.) 67 | $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); 68 | } 69 | 70 | return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; 71 | } 72 | 73 | /** 74 | * Get current server ip. 75 | * 76 | * @return string 77 | */ 78 | function get_server_ip() 79 | { 80 | if (!empty($_SERVER['SERVER_ADDR'])) { 81 | $ip = $_SERVER['SERVER_ADDR']; 82 | } elseif (!empty($_SERVER['SERVER_NAME'])) { 83 | $ip = gethostbyname($_SERVER['SERVER_NAME']); 84 | } else { 85 | // for php-cli(phpunit etc.) 86 | $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); 87 | } 88 | 89 | return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; 90 | } 91 | 92 | /** 93 | * Return current url. 94 | * 95 | * @return string 96 | */ 97 | function current_url() 98 | { 99 | $protocol = 'http://'; 100 | 101 | if ((!empty($_SERVER['HTTPS']) && 'off' !== $_SERVER['HTTPS']) || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : 'http') === 'https') { 102 | $protocol = 'https://'; 103 | } 104 | 105 | return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 106 | } 107 | 108 | /** 109 | * Return random string. 110 | * 111 | * @param string $length 112 | * 113 | * @return string 114 | * @throws \ETaobao\Kernel\Exceptions\RuntimeException 115 | */ 116 | function str_random($length) 117 | { 118 | return Str::random($length); 119 | } 120 | 121 | /** 122 | * @param string $content 123 | * @param string $publicKey 124 | * 125 | * @return string 126 | */ 127 | function rsa_public_encrypt($content, $publicKey) 128 | { 129 | $encrypted = ''; 130 | openssl_public_encrypt($content, $encrypted, openssl_pkey_get_public($publicKey), OPENSSL_PKCS1_OAEP_PADDING); 131 | 132 | return base64_encode($encrypted); 133 | } 134 | -------------------------------------------------------------------------------- /src/Kernel/Support/Str.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel\Support; 13 | 14 | use ETaobao\Kernel\Exceptions\RuntimeException; 15 | 16 | /** 17 | * Class Str. 18 | */ 19 | class Str 20 | { 21 | /** 22 | * The cache of snake-cased words. 23 | * 24 | * @var array 25 | */ 26 | protected static $snakeCache = []; 27 | 28 | /** 29 | * The cache of camel-cased words. 30 | * 31 | * @var array 32 | */ 33 | protected static $camelCache = []; 34 | 35 | /** 36 | * The cache of studly-cased words. 37 | * 38 | * @var array 39 | */ 40 | protected static $studlyCache = []; 41 | 42 | /** 43 | * Convert a value to camel case. 44 | * 45 | * @param string $value 46 | * 47 | * @return string 48 | */ 49 | public static function camel($value) 50 | { 51 | if (isset(static::$camelCache[$value])) { 52 | return static::$camelCache[$value]; 53 | } 54 | 55 | return static::$camelCache[$value] = lcfirst(static::studly($value)); 56 | } 57 | 58 | /** 59 | * Generate a more truly "random" alpha-numeric string. 60 | * 61 | * @param int $length 62 | * 63 | * @return string 64 | * 65 | * @throws RuntimeException 66 | */ 67 | public static function random($length = 16) 68 | { 69 | $string = ''; 70 | 71 | while (($len = strlen($string)) < $length) { 72 | $size = $length - $len; 73 | 74 | $bytes = static::randomBytes($size); 75 | 76 | $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size); 77 | } 78 | 79 | return $string; 80 | } 81 | 82 | /** 83 | * Generate a more truly "random" bytes. 84 | * 85 | * @param int $length 86 | * 87 | * @return string 88 | * 89 | * @throws RuntimeException 90 | * 91 | * @codeCoverageIgnore 92 | * 93 | * @throws \Exception 94 | */ 95 | public static function randomBytes($length = 16) 96 | { 97 | if (function_exists('random_bytes')) { 98 | $bytes = random_bytes($length); 99 | } elseif (function_exists('openssl_random_pseudo_bytes')) { 100 | $bytes = openssl_random_pseudo_bytes($length, $strong); 101 | if (false === $bytes || false === $strong) { 102 | throw new RuntimeException('Unable to generate random string.'); 103 | } 104 | } else { 105 | throw new RuntimeException('OpenSSL extension is required for PHP 5 users.'); 106 | } 107 | 108 | return $bytes; 109 | } 110 | 111 | /** 112 | * Generate a "random" alpha-numeric string. 113 | * 114 | * Should not be considered sufficient for cryptography, etc. 115 | * 116 | * @param int $length 117 | * 118 | * @return string 119 | */ 120 | public static function quickRandom($length = 16) 121 | { 122 | $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 123 | 124 | return substr(str_shuffle(str_repeat($pool, $length)), 0, $length); 125 | } 126 | 127 | /** 128 | * Convert the given string to upper-case. 129 | * 130 | * @param string $value 131 | * 132 | * @return string 133 | */ 134 | public static function upper($value) 135 | { 136 | return mb_strtoupper($value); 137 | } 138 | 139 | /** 140 | * Convert the given string to title case. 141 | * 142 | * @param string $value 143 | * 144 | * @return string 145 | */ 146 | public static function title($value) 147 | { 148 | return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); 149 | } 150 | 151 | /** 152 | * Convert a string to snake case. 153 | * 154 | * @param string $value 155 | * @param string $delimiter 156 | * 157 | * @return string 158 | */ 159 | public static function snake($value, $delimiter = '_') 160 | { 161 | $key = $value.$delimiter; 162 | 163 | if (isset(static::$snakeCache[$key])) { 164 | return static::$snakeCache[$key]; 165 | } 166 | 167 | if (!ctype_lower($value)) { 168 | $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value)); 169 | } 170 | 171 | return static::$snakeCache[$key] = trim($value, '_'); 172 | } 173 | 174 | /** 175 | * Convert a value to studly caps case. 176 | * 177 | * @param string $value 178 | * 179 | * @return string 180 | */ 181 | public static function studly($value) 182 | { 183 | $key = $value; 184 | 185 | if (isset(static::$studlyCache[$key])) { 186 | return static::$studlyCache[$key]; 187 | } 188 | 189 | $value = ucwords(str_replace(['-', '_'], ' ', $value)); 190 | 191 | return static::$studlyCache[$key] = str_replace(' ', '', $value); 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 阿里淘宝客SDK 2 | [![Latest Stable Version](https://poser.pugx.org/ennnnny/tbk/v/stable)](https://packagist.org/packages/ennnnny/tbk) 3 | [![Total Downloads](https://poser.pugx.org/ennnnny/tbk/downloads)](https://packagist.org/packages/ennnnny/tbk) 4 | ![php>=5.6](https://img.shields.io/badge/php->%3D5.6-orange.svg?maxAge=2592000) 5 | [![License](https://poser.pugx.org/ennnnny/tbk/license)](https://packagist.org/packages/ennnnny/tbk) 6 | 7 | > 可能是最优雅、简易的淘宝客SDK 8 | 9 | ## 安装 10 | 11 | ```shell 12 | composer require ennnnny/tbk 13 | ``` 14 | 15 | ## 使用 16 | 17 | ```php 18 | '', 24 | 'secretKey' => '', 25 | 'format' => 'json', 26 | 'session' => '',//授权接口(sc类的接口)需要带上 27 | 'sandbox' => false, 28 | ]; 29 | 30 | $app = Factory::Tbk($config); 31 | $param = [ 32 | 'fields' => 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick', 33 | 'q' => '蚊香' 34 | ]; 35 | $res = $app->item->get($param); 36 | 37 | print_r($res); 38 | ``` 39 | 40 | ## 说明文档 41 | 42 | | 接口名称 | 对应方法 | 43 | | -------- | ---- | 44 | | taobao.tbk.item.get (淘宝客商品查询)[**官网貌似已移除此接口**] | \$app->item->get() | 45 | | taobao.tbk.item.convert (淘宝客-推广者-商品链接转换) | \$app->item->convert() | 46 | | taobao.tbk.item.recommend.get (淘宝客-公用-商品关联推荐) | \$app->item->getRecommend() | 47 | | taobao.tbk.item.info.get (淘宝客-公用-淘宝客商品详情查询(简版)) | \$app->item->getInfo() | 48 | | taobao.tbk.shop.get (淘宝客-推广者-店铺搜索) | \$app->shop->get() | 49 | | taobao.tbk.shop.recommend.get (淘宝客-公用-店铺关联推荐) | \$app->shop->getRecommend() | 50 | | taobao.tbk.rebate.auth.get (淘宝客-推广者-返利商家授权查询) | \$app->rebate->getAuth() | 51 | | taobao.tbk.rebate.order.get (淘宝客-推广者-返利订单查询) | \$app->rebate->getOrder() | 52 | | taobao.tbk.uatm.event.get (枚举正在进行中的定向招商的活动列表)[**官网貌似已移除此接口**] | \$app->uatm->getEvent() | 53 | | taobao.tbk.uatm.event.item.get (获取淘宝联盟定向招商的宝贝信息)[**官网貌似已移除此接口**] | \$app->uatm->getItemEvent() | 54 | | taobao.tbk.uatm.favorites.item.get (淘宝客-推广者-选品库宝贝信息) | \$app->uatm->getItemFavorites() | 55 | | taobao.tbk.uatm.favorites.get (淘宝客-推广者-选品库宝贝列表) | \$app->uatm->getFavorites() | 56 | | taobao.tbk.ju.tqg.get (淘抢购api) | \$app->ju->getTqg() | 57 | | taobao.tbk.item.click.extract (淘宝客-公用-链接解析出商品id) | \$app->item->clickExtract() | 58 | | taobao.tbk.spread.get (淘宝客-公用-长链转短链) | \$app->spread->getSpread() | 59 | | taobao.tbk.item.guess.like (淘宝客商品猜你喜欢) | \$app->item->likeGuess() | 60 | | taobao.tbk.dg.item.coupon.get (好券清单API【导购】)[**官网貌似已移除此接口**] | \$app->dg->getCoupon() | 61 | | taobao.tbk.coupon.get (淘宝客-公用-阿里妈妈推广券详情查询) | \$app->coupon->get() | 62 | | taobao.tbk.tpwd.create (淘宝客-公用-淘口令生成) | \$app->tpwd->create() | 63 | | taobao.tbk.content.get (淘宝客-推广者-图文内容输出) | \$app->content->get() | 64 | | taobao.tbk.dg.newuser.order.get (淘宝客-推广者-新用户订单明细查询) | \$app->dg->getOrderNewUser() | 65 | | taobao.tbk.sc.newuser.order.get (淘宝客-服务商-新用户订单明细查询) | \$app->sc->getOrderNewUser() | 66 | | taobao.tbk.sc.material.optional (淘宝客-服务商-物料搜索) | \$app->sc->materialOptional() | 67 | | taobao.tbk.dg.optimus.material (淘宝客-推广者-物料精选) | \$app->dg->materialOptimus() | 68 | | taobao.tbk.dg.material.optional (淘宝客-推广者-物料搜索) | \$app->dg->materialOptional() | 69 | | taobao.tbk.dg.newuser.order.sum (淘宝客-推广者-拉新活动对应数据查询) | \$app->dg->sumOrderNewUser() | 70 | | taobao.tbk.sc.newuser.order.sum (淘宝客-服务商-拉新活动对应数据查询) | \$app->sc->sumOrderNewUser() | 71 | | taobao.tbk.sc.optimus.material (淘宝客-服务商-物料精选) | \$app->sc->materialOptimus() | 72 | | taobao.tbk.sc.publisher.info.save (淘宝客-公用-私域用户备案) | \$app->sc->savePublisherInfo() | 73 | | taobao.tbk.sc.publisher.info.get (淘宝客-公用-私域用户备案信息查询) | \$app->sc->getPublisherInfo() | 74 | | taobao.tbk.sc.invitecode.get (淘宝客-公用-私域用户邀请码生成) | \$app->sc->getInviteCode() | 75 | | taobao.tbk.sc.groupchat.message.send(淘宝客-服务商-手淘群发单) | \$app->sc->sendGroupchat() | 76 | | taobao.tbk.sc.groupchat.create(淘宝客-服务商-手淘群创建) | \$app->sc->createGroupchat() | 77 | | taobao.tbk.sc.groupchat.get(淘宝客-服务商-手淘群查询) | \$app->sc->getGroupchat() | 78 | | taobao.tbk.offline.sc.info.save( 线下新零售渠道备案 )[**官网貌似已移除此接口**] | \$app->sc->saveOfflineInfo() | 79 | | taobao.tbk.content.effect.get( 淘宝客-推广者-图文内容效果数据 ) | \$app->content->getEffect() | 80 | | taobao.tbk.dg.vegas.tlj.create( 淘宝客-推广者-淘礼金创建 ) | \$app->dg->createTlj() | 81 | | taobao.tbk.activitylink.get( 淘宝客-推广者-官方活动转链 ) | \$app->content->getActivityLink() | 82 | | taobao.tbk.sc.activitylink.toolget( 淘宝客-服务商-官方活动转链 ) | \$app->sc->getActivityTool() | 83 | | taobao.tbk.dg.punish.order.get( 淘宝客-推广者-处罚订单查询 ) | \$app->dg->getPunishOrder() | 84 | | taobao.tbk.order.get( 淘宝客订单查询 )[**官网貌似已移除此接口**] | \$app->order->get() | 85 | | taobao.tbk.relation.refund(淘宝客-推广者-维权退款订单查询) | \$app->order->getRefund() | 86 | | taobao.tbk.order.details.get(淘宝客-推广者-所有订单查询) | \$app->order->getDetails() | 87 | | taobao.tbk.dg.vegas.tlj.instance.report(淘宝客-推广者-淘礼金发放及使用报表) | \$app->dg->getTljReport() | 88 | | taobao.tbk.dg.wish.update(媒体导购单选品) | \$app->dg->updateWish() | 89 | | taobao.tbk.dg.wish.list(媒体淘客导购单查询) | \$app->dg->getWishList() | 90 | | taobao.tbk.activity.info.get(淘宝客-推广者-官方活动信息获取) | \$app->content->getActivityInfo() | 91 | 92 | ## 支持 93 | 94 | - 官方API文档: https://open.taobao.com/api.htm?docId=24517&docType=2 95 | - 淘宝客订单API: https://open.taobao.com/api.htm?docId=43328&docType=2 96 | - composer: https://getcomposer.org/ 97 | 98 | ## License 99 | 100 | MIT 101 | -------------------------------------------------------------------------------- /src/Tbk/Sc/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Sc; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | 24 | /** 25 | * taobao.tbk.sc.newuser.order.get (淘宝客-服务商-新用户订单明细查询) 26 | * @line https://open.taobao.com/api.htm?docId=33897&docType=2 27 | * @param array $params 28 | * @return array|mixed|\SimpleXMLElement|string 29 | */ 30 | public function getOrderNewUser(array $params) 31 | { 32 | $res = $this->httpPost('taobao.tbk.sc.newuser.order.get', $params, true); 33 | return $res; 34 | } 35 | 36 | /** 37 | * taobao.tbk.sc.material.optional (淘宝客-服务商-物料搜索) 38 | * @line https://open.taobao.com/api.htm?docId=35263&docType=2 39 | * @param array $params 40 | * @return array|mixed|\SimpleXMLElement|string 41 | */ 42 | public function materialOptional(array $params) 43 | { 44 | $res = $this->httpPost('taobao.tbk.sc.material.optional', $params, true); 45 | return $res; 46 | } 47 | 48 | /** 49 | * taobao.tbk.sc.newuser.order.sum (淘宝客-服务商-拉新活动对应数据查询) 50 | * @line https://open.taobao.com/api.htm?docId=36837&docType=2 51 | * @param array $params 52 | * @return array|mixed|\SimpleXMLElement|string 53 | */ 54 | public function sumOrderNewUser(array $params) 55 | { 56 | $res = $this->httpPost('taobao.tbk.sc.newuser.order.sum', $params, true); 57 | return $res; 58 | } 59 | 60 | /** 61 | * taobao.tbk.sc.optimus.material (淘宝客-服务商-物料精选) 62 | * @line https://open.taobao.com/api.htm?docId=37884&docType=2 63 | * @param array $params 64 | * @return array|mixed|\SimpleXMLElement|string 65 | */ 66 | public function materialOptimus(array $params) 67 | { 68 | $res = $this->httpPost('taobao.tbk.sc.optimus.material', $params, true); 69 | return $res; 70 | } 71 | 72 | /** 73 | * taobao.tbk.sc.publisher.info.save (淘宝客-公用-私域用户备案) 74 | * @line https://open.taobao.com/api.htm?docId=37988&docType=2 75 | * @param array $params 76 | * @return array|mixed|\SimpleXMLElement|string 77 | */ 78 | public function savePublisherInfo(array $params) 79 | { 80 | $res = $this->httpPost('taobao.tbk.sc.publisher.info.save', $params, true); 81 | return $res; 82 | } 83 | 84 | /** 85 | * taobao.tbk.sc.publisher.info.get (淘宝客-公用-私域用户备案信息查询) 86 | * @line https://open.taobao.com/api.htm?docId=37989&docType=2 87 | * @param array $params 88 | * @return array|mixed|\SimpleXMLElement|string 89 | */ 90 | public function getPublisherInfo(array $params) 91 | { 92 | $res = $this->httpPost('taobao.tbk.sc.publisher.info.get', $params, true); 93 | return $res; 94 | } 95 | 96 | /** 97 | * taobao.tbk.sc.invitecode.get (淘宝客-公用-私域用户邀请码生成) 98 | * @line https://open.taobao.com/api.htm?docId=38046&docType=2 99 | * @param array $params 100 | * @return array|mixed|\SimpleXMLElement|string 101 | */ 102 | public function getInviteCode(array $params) 103 | { 104 | $res = $this->httpPost('taobao.tbk.sc.invitecode.get', $params, true); 105 | return $res; 106 | } 107 | 108 | /** 109 | * taobao.tbk.sc.groupchat.message.send (淘宝客-服务商-手淘群发单) 110 | * @line https://open.taobao.com/api.htm?docId=38243&docType=2 111 | * @param array $params 112 | * @return array|mixed|\SimpleXMLElement|string 113 | */ 114 | public function sendGroupchat(array $params) 115 | { 116 | $res = $this->httpPost('taobao.tbk.sc.groupchat.message.send', $params, true); 117 | return $res; 118 | } 119 | 120 | /** 121 | * taobao.tbk.sc.groupchat.create (淘宝客-服务商-手淘群创建) 122 | * @line https://open.taobao.com/api.htm?docId=38262&docType=2 123 | * @param array $params 124 | * @return array|mixed|\SimpleXMLElement|string 125 | */ 126 | public function createGroupchat(array $params) 127 | { 128 | $res = $this->httpPost('taobao.tbk.sc.groupchat.create', $params, true); 129 | return $res; 130 | } 131 | 132 | /** 133 | * taobao.tbk.sc.groupchat.get (淘宝客-服务商-手淘群查询) 134 | * @line https://open.taobao.com/api.htm?docId=38263&docType=2 135 | * @param array $params 136 | * @return array|mixed|\SimpleXMLElement|string 137 | */ 138 | public function getGroupchat(array $params) 139 | { 140 | $res = $this->httpPost('taobao.tbk.sc.groupchat.get', $params, true); 141 | return $res; 142 | } 143 | 144 | /** 145 | * 线下新零售渠道备案 146 | * taobao.tbk.offline.sc.info.save( 线下新零售渠道备案 ) 147 | * @line http://open.taobao.com/api.htm?docId=40129&docType=2 148 | * @param array $params 149 | * @return array|mixed|\SimpleXMLElement|string 150 | */ 151 | public function saveOfflineInfo(array $params) 152 | { 153 | $res = $this->httpPost('taobao.tbk.offline.sc.info.save', $params, true); 154 | return $res; 155 | } 156 | 157 | /** 158 | * taobao.tbk.sc.activitylink.toolget (淘宝客-服务商-官方活动转链) 159 | * @line https://open.taobao.com/api.htm?docId=41921&docType=2 160 | * @param array $params 161 | * @return array|mixed|\SimpleXMLElement|string 162 | */ 163 | public function getActivityTool(array $params) 164 | { 165 | $res = $this->httpPost('taobao.tbk.sc.activitylink.toolget', $params, true); 166 | return $res; 167 | } 168 | 169 | } 170 | -------------------------------------------------------------------------------- /src/Tbk/Dg/Client.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Tbk\Dg; 13 | 14 | use ETaobao\Kernel\BaseClient; 15 | 16 | /** 17 | * Class Client. 18 | * 19 | * @author ennnnny 20 | */ 21 | class Client extends BaseClient 22 | { 23 | /** 24 | * taobao.tbk.dg.item.coupon.get (好券清单API【导购】) 25 | * @line http://open.taobao.com/docs/api.htm?apiId=29821&docType=2 26 | * @param array $params 27 | * @return array|mixed|\SimpleXMLElement|string 28 | */ 29 | public function getCoupon(array $params) 30 | { 31 | $res = $this->httpPost('taobao.tbk.dg.item.coupon.get', $params); 32 | return $res; 33 | } 34 | 35 | /** 36 | * taobao.tbk.dg.newuser.order.get (淘宝客-推广者-新用户订单明细查询) 37 | * @line https://open.taobao.com/api.htm?docId=33892&docType=2 38 | * @param array $params 39 | * @return array|mixed|\SimpleXMLElement|string 40 | */ 41 | public function getOrderNewUser(array $params) 42 | { 43 | $res = $this->httpPost('taobao.tbk.dg.newuser.order.get', $params); 44 | return $res; 45 | } 46 | 47 | /** 48 | * taobao.tbk.dg.material.optional (淘宝客-推广者-物料搜索) 49 | * @line https://open.taobao.com/api.htm?docId=35896&docType=2 50 | * @param array $params 51 | * @return array|mixed|\SimpleXMLElement|string 52 | */ 53 | public function materialOptional(array $params) 54 | { 55 | $res = $this->httpPost('taobao.tbk.dg.material.optional', $params); 56 | return $res; 57 | } 58 | 59 | /** 60 | * taobao.tbk.sc.newuser.order.sum (淘宝客-推广者-拉新活动对应数据查询) 61 | * @line https://open.taobao.com/api.htm?docId=36836&docType=2 62 | * @param array $params 63 | * @return array|mixed|\SimpleXMLElement|string 64 | */ 65 | public function sumOrderNewUser(array $params) 66 | { 67 | $res = $this->httpPost('taobao.tbk.dg.newuser.order.sum', $params); 68 | return $res; 69 | } 70 | 71 | /** 72 | * 通用物料推荐,传入官方公布的物料id,可获取指定物料 73 | * taobao.tbk.dg.optimus.material( 淘宝客-推广者-物料精选 ) 74 | * @line https://open.taobao.com/api.htm?docId=33947&docType=2 75 | * @param array $params 76 | * @return array|mixed|\SimpleXMLElement|string 77 | */ 78 | public function materialOptimus(array $params) 79 | { 80 | $res = $this->httpPost('taobao.tbk.dg.optimus.material', $params); 81 | return $res; 82 | } 83 | 84 | /** 85 | * taobao.tbk.dg.vegas.tlj.create( 淘宝客-推广者-淘礼金创建 ) 86 | * @line https://open.taobao.com/api.htm?docId=40173&docType=2 87 | * @param array $params 88 | * @return array|mixed|\SimpleXMLElement|string 89 | */ 90 | public function createTlj(array $params) 91 | { 92 | $res = $this->httpPost('taobao.tbk.dg.vegas.tlj.create', $params); 93 | return $res; 94 | } 95 | 96 | /** 97 | * taobao.tbk.dg.punish.order.get (淘宝客-推广者-处罚订单查询) 98 | * @line https://open.taobao.com/api.htm?docId=42050&docType=2 99 | * @param array $params 100 | * @return array|mixed|\SimpleXMLElement|string 101 | */ 102 | public function getPunishOrder(array $params) 103 | { 104 | if (isset($params['af_order_option']) && is_array($params['af_order_option'])) { 105 | $data['af_order_option'] = json_encode($params['af_order_option']); 106 | } elseif (is_array($params)) { 107 | $data['af_order_option'] = json_encode($params); 108 | } else { 109 | $data['af_order_option'] = $params; 110 | } 111 | $res = $this->httpPost('taobao.tbk.dg.punish.order.get', $data); 112 | return $res; 113 | } 114 | 115 | /** 116 | * taobao.tbk.dg.vegas.tlj.instance.report (淘宝客-推广者-淘礼金发放及使用报表) 117 | * @link https://open.taobao.com/api.htm?docId=43317&docType=2 118 | * @param array $params 119 | * @return array|mixed|\SimpleXMLElement|string 120 | */ 121 | public function getTljReport(array $params) 122 | { 123 | $res = $this->httpPost('taobao.tbk.dg.vegas.tlj.instance.report', $params); 124 | return $res; 125 | } 126 | 127 | /** 128 | * taobao.tbk.dg.wish.update (媒体导购单选品) 129 | * @link https://open.taobao.com/api.htm?docId=46750&docType=2 130 | * @param array $params 131 | * @return array|mixed|\SimpleXMLElement|string 132 | */ 133 | public function updateWish(array $params) 134 | { 135 | if (isset($params['param0']) && is_array($params['param0'])) { 136 | $data['param0'] = json_encode($params['param0']); 137 | } elseif (is_array($params)) { 138 | $data['param0'] = json_encode($params); 139 | } else { 140 | $data['param0'] = $params; 141 | } 142 | $res = $this->httpPost('taobao.tbk.dg.wish.update', $data); 143 | return $res; 144 | } 145 | 146 | /** 147 | * taobao.tbk.dg.wish.list( 媒体淘客导购单查询 ) 148 | * @link https://open.taobao.com/api.htm?docId=46751&docType=2 149 | * @param array $params 150 | * @return array|mixed|\SimpleXMLElement|string 151 | */ 152 | public function getWishList(array $params) 153 | { 154 | if (isset($params['param0']) && is_array($params['param0'])) { 155 | $data['param0'] = json_encode($params['param0']); 156 | } elseif (is_array($params)) { 157 | $data['param0'] = json_encode($params); 158 | } else { 159 | $data['param0'] = $params; 160 | } 161 | $res = $this->httpPost('taobao.tbk.dg.wish.list', $data); 162 | return $res; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/Kernel/BaseClient.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace ETaobao\Kernel; 13 | 14 | use ETaobao\Kernel\Exceptions\Exception; 15 | use ETaobao\Kernel\Support; 16 | 17 | /** 18 | * Class BaseClient. 19 | * 20 | * @author ennnnny 21 | */ 22 | class BaseClient 23 | { 24 | /** 25 | * @var \ETaobao\Kernel\ServiceContainer 26 | */ 27 | protected $app; 28 | 29 | /** 30 | * @var 31 | */ 32 | protected $baseUri; 33 | 34 | /** 35 | * @var array 36 | */ 37 | protected $globalConfig = []; 38 | 39 | /** 40 | * BaseClient constructor. 41 | * 42 | * @param \ETaobao\Kernel\ServiceContainer $app 43 | */ 44 | public function __construct(ServiceContainer $app) 45 | { 46 | $this->app = $app; 47 | $this->globalConfig = $this->app->getConfig(); 48 | if ($this->globalConfig['sandbox']) { 49 | $this->baseUri = $this->globalConfig['api_box_uri']; 50 | } else { 51 | $this->baseUri = $this->globalConfig['api_uri']; 52 | } 53 | } 54 | 55 | /** 56 | * GET request. 57 | * 58 | * @param array $query 59 | * @return ServiceContainer 60 | */ 61 | public function httpGet(array $query = []) 62 | { 63 | return $this->baseUri; 64 | } 65 | 66 | /** 67 | * POST request. 68 | * @param $method 接口名称 69 | * @param array $data 请求参数 70 | * @param bool $auth 是否需要授权 71 | * @return array|mixed|\SimpleXMLElement|string 72 | */ 73 | public function httpPost($method, array $data = [], $auth = false) 74 | { 75 | //组装系统参数 76 | $sysParams["app_key"] = $this->globalConfig['appkey']; 77 | $sysParams["v"] = $this->globalConfig['apiVersion']; 78 | $sysParams["format"] = $this->globalConfig['format']; 79 | $sysParams["sign_method"] = $this->globalConfig['signMethod']; 80 | $sysParams["method"] = $method; 81 | $sysParams["timestamp"] = date("Y-m-d H:i:s"); 82 | if ($auth){ 83 | if (isset($this->globalConfig['session']) && !empty($this->globalConfig['session'])){ 84 | $sysParams["session"] = $this->globalConfig['session']; 85 | }else{ 86 | $result['code'] = 0; 87 | $result['msg'] = "NEED TO SET SESSION"; 88 | return $result; 89 | } 90 | } 91 | $sysParams["sign"] = Support\generateSign(array_merge($data, $sysParams), $this->globalConfig['secretKey']); 92 | $requestUrl = $this->baseUri . '?'; 93 | foreach ($sysParams as $sysParamKey => $sysParamValue) { 94 | $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; 95 | } 96 | $requestUrl = substr($requestUrl, 0, -1); 97 | try { 98 | $resp = $this->curl($requestUrl, $data); 99 | } catch (Exception $e) { 100 | $result['code'] = $e->getCode(); 101 | $result['msg'] = $e->getMessage(); 102 | return json_encode($result); 103 | } 104 | 105 | unset($data); 106 | 107 | // 解析返回 108 | return $this->parseRep($resp); 109 | } 110 | 111 | /** 112 | * 请求方式 113 | * @param $url 114 | * @param null $postFields 115 | * @return mixed 116 | */ 117 | public function curl($url, $postFields = null) 118 | { 119 | $ch = curl_init(); 120 | curl_setopt($ch, CURLOPT_URL, $url); 121 | curl_setopt($ch, CURLOPT_FAILONERROR, false); 122 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 123 | // if ($this->readTimeout) { 124 | // curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); 125 | // } 126 | // if ($this->connectTimeout) { 127 | // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); 128 | // } 129 | // curl_setopt($ch, CURLOPT_USERAGENT, "top-sdk-php"); 130 | //https 请求 131 | if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { 132 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 133 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 134 | } 135 | 136 | if (is_array($postFields) && 0 < count($postFields)) { 137 | $postBodyString = ""; 138 | $postMultipart = false; 139 | foreach ($postFields as $k => $v) { 140 | if ("@" != substr($v, 0, 1))//判断是不是文件上传 141 | { 142 | $postBodyString .= "$k=" . urlencode($v) . "&"; 143 | } else//文件上传用multipart/form-data,否则用www-form-urlencoded 144 | { 145 | $postMultipart = true; 146 | if (class_exists('\CURLFile')) { 147 | $postFields[$k] = new \CURLFile(substr($v, 1)); 148 | } 149 | } 150 | } 151 | unset($k, $v); 152 | curl_setopt($ch, CURLOPT_POST, true); 153 | if ($postMultipart) { 154 | if (class_exists('\CURLFile')) { 155 | curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); 156 | } else { 157 | if (defined('CURLOPT_SAFE_UPLOAD')) { 158 | curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 159 | } 160 | } 161 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 162 | } else { 163 | $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); 164 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 165 | curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1)); 166 | } 167 | } 168 | $reponse = curl_exec($ch); 169 | 170 | if (curl_errno($ch)) { 171 | throw new Exception(curl_error($ch), 0); 172 | } else { 173 | $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 174 | if (200 !== $httpStatusCode) { 175 | throw new Exception($reponse, $httpStatusCode); 176 | } 177 | } 178 | curl_close($ch); 179 | return $reponse; 180 | } 181 | 182 | /** 183 | * 解析数据 184 | * @param $response 185 | * @return array|mixed|\SimpleXMLElement 186 | */ 187 | public function parseRep($response) 188 | { 189 | //解析返回结果 190 | $respWellFormed = false; 191 | $respObject = array(); 192 | if ("json" == $this->globalConfig['format']) { 193 | $respObject = json_decode($response); 194 | if (null !== $respObject) { 195 | $respWellFormed = true; 196 | $respObject = @current($respObject); 197 | // foreach ($respObject as $propKey => $propValue) { 198 | // $respObject = $propValue; 199 | // } 200 | } 201 | } else if ("xml" == $this->globalConfig['format']) { 202 | $respObject = @simplexml_load_string($response); 203 | if (false !== $respObject) { 204 | $respWellFormed = true; 205 | } 206 | } 207 | //返回的HTTP文本不是标准JSON或者XML,记下错误日志 208 | if (false === $respWellFormed) { 209 | $result['code'] = 0; 210 | $result['msg'] = "HTTP_RESPONSE_NOT_WELL_FORMED"; 211 | return $result; 212 | } 213 | 214 | return $respObject; 215 | } 216 | } 217 | --------------------------------------------------------------------------------