-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPRequest.php
More file actions
63 lines (51 loc) · 2.12 KB
/
HTTPRequest.php
File metadata and controls
63 lines (51 loc) · 2.12 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
<?php
/**
* Created by PhpStorm.
* User: 23c
* Date: 16/11/8
* Time: 下午1:38
*/
class HTTPRequest
{
public static function __callStatic($name, $arguments)
{
$name = strtoupper($name);
$methods = array('GET', 'PUT', 'DELETE','POST');
if (empty($arguments)) {
return json_decode(json_encode(['error_code' => '-10000', 'msg' => '请求的参数错误' ], JSON_UNESCAPED_UNICODE));
}
if (!in_array($name, $methods)) {
return json_decode(json_encode(['error_code' => '-10000', 'msg' => '请求的方法错误'], JSON_UNESCAPED_UNICODE));
}
$url = $arguments[0];
$data = empty($arguments[1]) ? array() : $arguments[1];
try {
$config = \Phalcon\DI::getDefault()->get("config");
$webApi = $config->webApi;
$scheme = 'http';
$parseURI = parse_url($webApi->domain);
$version = empty($arguments[2]) ? $webApi->version : $arguments[2];
if (isset($parseURI['scheme'])) {
$scheme = $parseURI['scheme'];
}
$url = $webApi->domain . $url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ( $curl, CURLOPT_CUSTOMREQUEST, $name );
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$aHeader[] = "Content-Type: application/json";
$aHeader[] = "DCAPPID: {$webApi->appId}";
$aHeader[] = "{$webApi->versionKey}: {$version}";
$aHeader[] = "DCTOKEN: " . md5($webApi->apiKeys . json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $aHeader);
$data = curl_exec($curl);
curl_close($curl);
return json_decode($data, true);
} catch (\Exception $exception) {
return json_decode(json_encode(['error_code' => '10001', 'msg' => $exception->getMessage(), 'data' => []]));
}
}
}