AlipayMobilePublicMultiMediaClient.php
4.9 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* 多媒体文件客户端
* @author yikai.hu
* @version $Id: AlipayMobilePublicMultiMediaClient.php, v 0.1 Aug 15, 2014 10:19:01 AM yikai.hu Exp $
*/
//namespace alipay\api ;
include("AlipayMobilePublicMultiMediaExecute.php");
class AlipayMobilePublicMultiMediaClient{
private $DEFAULT_CHARSET = 'UTF-8';
private $METHOD_POST = "POST";
private $METHOD_GET = "GET";
private $SIGN = 'sign'; //get name
private $timeout = 10 ;// 超时时间
private $serverUrl;
private $appId;
private $privateKey;
private $prodCode;
private $format = 'json'; //todo
private $sign_type = 'RSA'; //todo
private $charset;
private $apiVersion = "1.0";
private $apiMethodName = "alipay.mobile.public.multimedia.download";
private $media_id = "L21pZnMvVDFQV3hYWGJKWFhYYUNucHJYP3Q9YW13ZiZ4c2lnPTU0MzRhYjg1ZTZjNWJmZTMxZGJiNjIzNDdjMzFkNzkw575";
//此处写死的,实际开发中,请传入
private $connectTimeout = 3000;
private $readTimeout = 15000;
function __construct($serverUrl = '', $appId = '', $partner_private_key = '', $format = '', $charset = 'GBK'){
$this -> serverUrl = $serverUrl;
$this -> appId = $appId;
$this -> privateKey = $partner_private_key;
$this -> format = $format;
$this -> charset = $charset;
}
/**
* getContents 获取网址内容
* @param $request
* @return text | bin
*/
public function getContents(){
//自己的服务器如果没有 curl,可用:fsockopen() 等
//1:
//2: 私钥格式
$datas = array(
"app_id" => $this -> appId,
"method" => $this -> METHOD_POST,
"sign_type" => $this -> sign_type,
"version" => $this -> apiVersion,
"timestamp" => date('Y-m-d H:i:s') ,//yyyy-MM-dd HH:mm:ss
"biz_content" => '{"mediaId":"'. $this -> media_id .'"}',
"charset" => $this -> charset
);
//要提交的数据
$data_sign = $this -> buildGetUrl( $datas );
$post_data = $data_sign;
//初始化 curl
$ch = curl_init();
//设置目标服务器
curl_setopt($ch, CURLOPT_URL, $this -> serverUrl );
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, $this-> timeout);
if( $this-> METHOD_POST == 'POST'){
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo $output;
//分离头部
//list($header, $body) = explode("\r\n\r\n", $output, 2);
$datas = explode("\r\n\r\n", $output, 2);
$header = $datas[0];
if( $httpCode == '200'){
$body = $datas[1];
}else{
$body = '';
}
return $this -> execute( $header, $body, $httpCode );
}
/**
*
* @param $request
* @return text | bin
*/
public function execute( $header = '', $body = '', $httpCode = '' ){
$exe = new AlipayMobilePublicMultiMediaExecute( $header, $body, $httpCode );
return $exe;
}
public function buildGetUrl( $query = array() ){
if( ! is_array( $query ) ){
//exit;
}
//排序参数,
$data = $this -> buildQuery( $query );
// 私钥密码
$passphrase = '';
$key_width = 64;
//私钥
$privateKey = $this -> privateKey;
$p_key = array();
//如果私钥是 1行
if( ! stripos( $privateKey, "\n" ) ){
$i = 0;
while( $key_str = substr( $privateKey , $i * $key_width , $key_width) ){
$p_key[] = $key_str;
$i ++ ;
}
}else{
//echo '一行?';
}
$privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . implode("\n", $p_key) ;
$privateKey = $privateKey ."\n-----END RSA PRIVATE KEY-----";
// echo "\n\n私钥:\n";
// echo( $privateKey );
// echo "\n\n\n";
//私钥
$private_id = openssl_pkey_get_private( $privateKey , $passphrase);
// 签名
$signature = '';
if("RSA2"==$this->sign_type){
openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA256 );
}else{
openssl_sign($data, $signature, $private_id, OPENSSL_ALGO_SHA1 );
}
openssl_free_key( $private_id );
//加密后的内容通常含有特殊字符,需要编码转换下
$signature = base64_encode($signature);
$signature = urlencode( $signature );
//$signature = 'XjUN6YM1Mc9HXebKMv7GTLy7gmyhktyOgKk2/Jf+cz4DtP6udkzTdpkjW2j/Z4ZSD7xD6CNYI1Spz4yS93HPT0a5X9LgFWYY8SaADqe+ArXg+FBSiTwUz49SE//Xd9+LEiIRsSFkbpkuiGoO6mqJmB7vXjlD5lx6qCM3nb41wb8=';
$out = $data .'&'. $this -> SIGN .'='. $signature;
// echo "\n\n 加密后:\n";
// echo( $out );
// echo "\n\n\n";
return $out ;
}
/*
* 查询参数排序 a-z
* */
public function buildQuery( $query ){
if ( !$query ) {
return null;
}
//将要 参数 排序
ksort( $query );
//重新组装参数
$params = array();
foreach($query as $key => $value){
$params[] = $key .'='. $value ;
}
$data = implode('&', $params);
return $data;
}
}