Client.php
5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
namespace common\lib\YohoApi;
use Yii;
use yii\helpers\ArrayHelper;
/**
* 有货接口
* @author wuxiao
*
* @文档地址 http://git.yoho.cn/yoho-documents/api-interfaces
*/
class Client{
private static $_domain = array(
'prod' => 'http://api.yoho.cn/',
'dev' => 'http://192.168.102.205:8080/gateway/',
);
/**
* 私钥列表
* @var array
*/
private static $privateKey = array(
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
'web' => '0ed29744ed318fd28d2c07985d3ba633',
'h5' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'other' => '6tjjbg7ecrcd3ulgqizbqavfrutixhm7',
);
/**
* 默认客户端类型
*/
const client_type = 'web';
/**
* $_requestUrl
* 请求url
* @var string
*/
protected static $_requestUrl = '';
/**
* $_rawResponse
* 原始的返回信息
* @var string
*/
protected static $_rawResponse = '';
/**
* $_timeOut
* 设置连接主机的超时时间
* @var int 数量级:秒
* */
protected static $_timeOut = 2;
/**
* getRequestUrl
* 获取请求url
*/
public static function getRequestUrl()
{
return self::$_requestUrl;
}
/**
* getRawResponse
* 获取原始的返回信息
*/
public static function getRawResponse()
{
return self::$_rawResponse;
}
/**
* 实例化自身
*/
public static function self()
{
static $self;
if (empty($self)){
$self = new self();
}
return $self;
}
private function __construct(){
;
}
/**
* 根据多个skn查询的接口
* @param type $productSkn
* @return type
*/
public function h5ProductBatch($productSkn){
$productSkn = (array)$productSkn;
$params = [
'client_type'=>self::client_type,
'method'=>'h5.product.batch',
'productSkn'=>implode(',',$productSkn),
];
return $this->send($params);
}
/**
* 根据多个优惠券id查询的接口
* @param type $couponId
* @return type
*/
public function couponList($couponId){
$couponId = (array)$couponId;
$url = 'http://192.168.102.210:8088/platform/coupon/queryBroadCouponList';
$params = [
'client_type'=>self::client_type,
'couponIds'=>implode(',',$couponId),
];
$params = $this->makeUrl($params);
return $this->_sendRequest($url,$params);
}
protected function send(array $params)
{
$params = $this->makeUrl($params);
$domain = $this->getDomain();
$ret = self::_sendRequest($domain,$params,'GET');
if (!is_array($ret) || ($ret['code'] !== 200)){
return false;
}
return $ret['data'];
}
protected function makeUrl(array $package){
$package['private_key'] = self::$privateKey[self::client_type];
ksort($package);
reset($package);
$packageList = array();
foreach ($package as $key => $val) {
$packageList[] = trim($key . '=' . $val);
}
$client_secret = strtolower(md5(implode('&', $packageList)));
$package['client_secret'] = $client_secret;
unset($package['private_key']);
return $package;
}
protected function getDomain(){
return ArrayHelper::getValue(self::$_domain, YII_ENV,self::$_domain['prod']);
}
/**
* _sendRequest
* @param string $url 请求url
* @param array $paramArray 请求参数
* @param string $method 请求方法
* @return
*/
protected static function _sendRequest($url, $paramArray, $method = 'POST')
{
$ch = curl_init();
if ($method == 'POST')
{
$paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
}
else
{
$url .= '?' . http_build_query($paramArray);
}
self::$_requestUrl = $url;
Yii::$app->response->headers->add('X-YohoApi',$url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT,self::$_timeOut);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (false !== strpos($url, "https")) {
// 证书
// curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$resultStr = curl_exec($ch);
self::$_rawResponse = $resultStr;
if (!$resultStr)
{
return $resultStr;
}
$result = json_decode($resultStr, true);
if (!$result)
{
return $resultStr;
}
return $result;
}
}