Skip to content

Commit

Permalink
release
Browse files Browse the repository at this point in the history
Initial release
  • Loading branch information
ennnnny committed Apr 11, 2018
1 parent 8bd3207 commit 9a2ae00
Show file tree
Hide file tree
Showing 35 changed files with 1,791 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/vendor
/composer.lock
.idea/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 叶子坑

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
@@ -0,0 +1,42 @@
# 阿里淘宝客SDK

> 可能是最优雅、简易的淘宝客SDK
## 安装

```shell
composer require ennnnny/tbk
```

## 使用

```php
<?php

use ETaobao\Factory;

$config = [
'appkey' => '',
'secretKey' => '',
'format' => 'json',
'sandbox' => false,
];

$app = Factory::Tbk($config);
$param = [
'fields' => 'num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick',
'q' => '蚊香'
];
$res = $app->item->get($param);

print_r($res);
```

## 支持

- 官方API文档: http://open.taobao.com/docs/api.htm?apiId=24515
- composer: https://getcomposer.org/

## License

MIT
19 changes: 19 additions & 0 deletions composer.json
@@ -0,0 +1,19 @@
{
"name": "ennnnny/tbk",
"description": "简约优雅的淘宝客SDK",
"keywords": ["淘宝客", "tbk", "sdk"],
"type": "library",
"license": "MIT",
"require": {
"php": ">=5.6.0",
"pimple/pimple": "3.0"
},
"autoload": {
"psr-4": {
"ETaobao\\": "src/"
},
"files": [
"src/Kernel/Support/Helpers.php"
]
}
}
47 changes: 47 additions & 0 deletions src/Factory.php
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the ennnnny/tbk.
*
* (c) ennnnny <kuye1130@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace ETaobao;

/**
* Class Factory.
*
* @method static Tbk\Application tbk(array $config)
*/
class Factory
{
/**
* @param string $name
* @param array $config
*
* @return \ETaobao\Kernel\ServiceContainer
*/
public static function make($name, array $config)
{
$namespace = Kernel\Support\Str::studly($name);
$application = "\\ETaobao\\{$namespace}\\Application";

return new $application($config);
}

/**
* Dynamically pass methods to the application.
*
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
return self::make($name, ...$arguments);
}
}
205 changes: 205 additions & 0 deletions src/Kernel/BaseClient.php
@@ -0,0 +1,205 @@
<?php

/*
* This file is part of the ennnnny/tbk.
*
* (c) ennnnny <kuye1130@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace ETaobao\Kernel;

use ETaobao\Kernel\Support;

/**
* Class BaseClient.
*
* @author ennnnny <kuye1130@gmail.com>
*/
class BaseClient
{
/**
* @var \ETaobao\Kernel\ServiceContainer
*/
protected $app;

/**
* @var
*/
protected $baseUri;

/**
* @var array
*/
protected $globalConfig = [];

/**
* BaseClient constructor.
*
* @param \ETaobao\Kernel\ServiceContainer $app
*/
public function __construct(ServiceContainer $app)
{
$this->app = $app;
$this->globalConfig = $this->app->getConfig();
if ($this->globalConfig['sandbox']) {
$this->baseUri = $this->globalConfig['api_box_uri'];
} else {
$this->baseUri = $this->globalConfig['api_uri'];
}
}

/**
* GET request.
*
* @param array $query
* @return ServiceContainer
*/
public function httpGet(array $query = [])
{
return $this->baseUri;
}

/**
* POST request.
*
* @param $method
* @param array $data
*/
public function httpPost($method, array $data = [])
{
//组装系统参数
$sysParams["app_key"] = $this->globalConfig['appkey'];
$sysParams["v"] = $this->globalConfig['apiVersion'];
$sysParams["format"] = $this->globalConfig['format'];
$sysParams["sign_method"] = $this->globalConfig['signMethod'];
$sysParams["method"] = $method;
$sysParams["timestamp"] = date("Y-m-d H:i:s");
$sysParams["sign"] = Support\generateSign(array_merge($data, $sysParams), $this->globalConfig['secretKey']);
$requestUrl = $this->baseUri . '?';
foreach ($sysParams as $sysParamKey => $sysParamValue) {
$requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
}
$requestUrl = substr($requestUrl, 0, -1);
try {
$resp = $this->curl($requestUrl, $data);
} catch (Exception $e) {
$result['code'] = $e->getCode();
$result['msg'] = $e->getMessage();
return json_encode($result);
}

unset($data);

// 解析返回
return $this->parseRep($resp);
}

/**
* 请求方式
* @param $url
* @param null $postFields
* @return mixed
*/
public function curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// if ($this->readTimeout) {
// curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
// }
// if ($this->connectTimeout) {
// curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
// }
// curl_setopt($ch, CURLOPT_USERAGENT, "top-sdk-php");
//https 请求
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}

if (is_array($postFields) && 0 < count($postFields)) {
$postBodyString = "";
$postMultipart = false;
foreach ($postFields as $k => $v) {
if ("@" != substr($v, 0, 1))//判断是不是文件上传
{
$postBodyString .= "$k=" . urlencode($v) . "&";
} else//文件上传用multipart/form-data,否则用www-form-urlencoded
{
$postMultipart = true;
if (class_exists('\CURLFile')) {
$postFields[$k] = new \CURLFile(substr($v, 1));
}
}
}
unset($k, $v);
curl_setopt($ch, CURLOPT_POST, true);
if ($postMultipart) {
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
} else {
$header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
}
}
$reponse = curl_exec($ch);

if (curl_errno($ch)) {
throw new Exception(curl_error($ch), 0);
} else {
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
throw new Exception($reponse, $httpStatusCode);
}
}
curl_close($ch);
return $reponse;
}

/**
* 解析数据
* @param $response
* @return array|mixed|\SimpleXMLElement
*/
public function parseRep($response)
{
//解析返回结果
$respWellFormed = false;
$respObject = array();
if ("json" == $this->globalConfig['format']) {
$respObject = json_decode($response);
if (null !== $respObject) {
$respWellFormed = true;
$respObject = @current($respObject);
// foreach ($respObject as $propKey => $propValue) {
// $respObject = $propValue;
// }
}
} else if ("xml" == $this->globalConfig['format']) {
$respObject = @simplexml_load_string($response);
if (false !== $respObject) {
$respWellFormed = true;
}
}
//返回的HTTP文本不是标准JSON或者XML,记下错误日志
if (false === $respWellFormed) {
$result['code'] = 0;
$result['msg'] = "HTTP_RESPONSE_NOT_WELL_FORMED";
return $result;
}

return $respObject;
}
}
23 changes: 23 additions & 0 deletions src/Kernel/Exceptions/Exception.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the ennnnny/tbk.
*
* (c) ennnnny <kuye1130@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace ETaobao\Kernel\Exceptions;

use Exception as BaseException;

/**
* Class Exception.
*
* @author ennnnny <kuye1130@gmail.com>
*/
class Exception extends BaseException
{
}
21 changes: 21 additions & 0 deletions src/Kernel/Exceptions/RuntimeException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the ennnnny/tbk.
*
* (c) ennnnny <kuye1130@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace ETaobao\Kernel\Exceptions;

/**
* Class RuntimeException.
*
* @author ennnnny <kuye1130@gmail.com>
*/
class RuntimeException extends Exception
{
}

0 comments on commit 9a2ae00

Please sign in to comment.