1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WrapIt\Http; |
4
|
|
|
|
5
|
|
|
use WrapIt\Exceptions\WrapItParameterException; |
6
|
|
|
use WrapIt\Exceptions\WrapItHTTPException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Requester |
10
|
|
|
* |
11
|
|
|
* @package WrapIt |
12
|
|
|
*/ |
13
|
|
|
abstract class Requester { |
14
|
|
|
|
15
|
|
|
protected $domain = null; |
16
|
|
|
|
17
|
20 |
|
protected function request($data) { |
18
|
20 |
|
$data = array_merge(array( |
19
|
20 |
|
"url" => null, |
20
|
12 |
|
"get" => array(), |
21
|
12 |
|
"post" => array(), |
22
|
12 |
|
"body_type" => "application/x-www-form-urlencoded", |
23
|
12 |
|
"body_charset" => "UTF-8", |
24
|
12 |
|
"headers" => array(), |
25
|
12 |
|
"response_type" => "json", |
26
|
12 |
|
"useragent" => "WrapIt-HTTP/1.0", |
27
|
|
|
"method" => null |
28
|
20 |
|
), $data); |
29
|
|
|
|
30
|
20 |
|
if ($data["url"] === null) { |
31
|
|
|
throw new WrapItParameterException("Missing data: url"); |
32
|
|
|
} |
33
|
|
|
|
34
|
20 |
|
$url = $data["url"]; |
35
|
20 |
|
if ($data["get"] !== null && count($data["get"]) > 0) { |
36
|
5 |
|
$url .= "?" . http_build_query($data["get"]); |
37
|
3 |
|
} |
38
|
|
|
|
39
|
20 |
|
$ch = curl_init(); |
40
|
20 |
|
curl_setopt($ch, CURLOPT_URL, $url); |
41
|
20 |
|
curl_setopt($ch, CURLOPT_USERAGENT, $data["useragent"]); |
42
|
20 |
|
if ($data["post"] != null) { |
43
|
10 |
|
$data["headers"][] = "Content-type: " . $data["body_type"]; |
44
|
10 |
|
$data["headers"][] = "Charset: " . $data["body_charset"]; |
45
|
|
|
|
46
|
10 |
|
switch ($data["body_type"]) { |
47
|
10 |
|
case "application/x-www-form-urlencoded": |
48
|
10 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data["post"])); |
49
|
10 |
|
break; |
50
|
|
|
case "application/json": |
51
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data["post"])); |
52
|
|
|
break; |
53
|
|
|
case "text/plain": |
54
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]); |
55
|
|
|
break; |
56
|
|
|
case "text/xml": |
57
|
|
|
if (is_string($data["post"])) { |
58
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]); |
59
|
|
|
} else { |
60
|
|
|
throw new WrapItParameterException("XML data must be pre-processed!"); |
61
|
|
|
} |
62
|
|
|
break; |
63
|
|
|
default: |
64
|
|
|
throw new WrapItParameterException("Invalid body type"); |
65
|
6 |
|
} |
66
|
|
|
|
67
|
10 |
|
if ($data["method"] !== null) { |
68
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]); |
69
|
|
|
} else { |
70
|
10 |
|
curl_setopt($ch, CURLOPT_POST, true); |
71
|
|
|
} |
72
|
6 |
|
} |
73
|
20 |
|
if ($data["headers"] !== null && count($data["headers"]) > 0) { |
74
|
20 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $data["headers"]); |
75
|
12 |
|
} |
76
|
20 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
77
|
20 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
78
|
20 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
79
|
20 |
|
$result = curl_exec($ch); |
80
|
20 |
|
$response_header = curl_getinfo($ch); |
81
|
20 |
|
curl_close($ch); |
82
|
|
|
|
83
|
20 |
|
if ($response_header["http_code"] == 0) { |
84
|
|
|
throw new WrapItHTTPException("HTTP Connection error"); |
85
|
|
|
} |
86
|
|
|
|
87
|
20 |
|
switch ($data["response_type"]) { |
88
|
20 |
|
case "json": |
|
|
|
|
89
|
20 |
|
$json = json_decode($result, true); |
90
|
20 |
|
if ($json !== null) { |
91
|
20 |
|
return $json; |
92
|
|
|
} else { |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
default: |
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
5 |
|
public function getDomain() { |
101
|
5 |
|
return $this->domain; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
abstract public function get($api, $data); |
105
|
|
|
abstract public function post($api, $data); |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|