1 | <?php |
||
13 | abstract class Requester { |
||
14 | |||
15 | protected $domain = null; |
||
16 | |||
17 | 4 | protected function request($data) { |
|
18 | 4 | $data = array_merge(array( |
|
19 | 4 | "url" => null, |
|
20 | 4 | "get" => array(), |
|
21 | 4 | "post" => array(), |
|
22 | 4 | "body_type" => "application/x-www-form-urlencoded", |
|
23 | 4 | "body_charset" => "UTF-8", |
|
24 | 4 | "headers" => array(), |
|
25 | 4 | "response_type" => "json", |
|
26 | 4 | "useragent" => "WrapIt-HTTP/1.0", |
|
27 | "method" => null |
||
28 | 4 | ), $data); |
|
29 | |||
30 | 4 | if ($data["url"] == null) { |
|
|
|||
31 | throw new WrapItParameterException("Missing data: url"); |
||
32 | } |
||
33 | |||
34 | 4 | $url = $data["url"]; |
|
35 | 4 | if ($data["get"] != null && count($data["get"]) > 0) { |
|
36 | 1 | $url .= "?" . http_build_query($data["get"]); |
|
37 | 1 | } |
|
38 | |||
39 | 4 | $ch = curl_init(); |
|
40 | 4 | curl_setopt($ch, CURLOPT_URL, $url); |
|
41 | 4 | curl_setopt($ch, CURLOPT_USERAGENT, $data["useragent"]); |
|
42 | 4 | if ($data["post"] != null) { |
|
43 | 2 | $data["headers"][] = "Content-type: " . $data["body_type"]; |
|
44 | 2 | $data["headers"][] = "Charset: " . $data["body_charset"]; |
|
45 | |||
46 | 2 | switch ($data["body_type"]) { |
|
47 | 2 | case "application/x-www-form-urlencoded": |
|
48 | 2 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data["post"])); |
|
49 | 2 | 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 | break; |
||
66 | 2 | } |
|
67 | |||
68 | 2 | if ($data["method"] != null) { |
|
69 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]); |
||
70 | } else { |
||
71 | 2 | curl_setopt($ch, CURLOPT_POST, true); |
|
72 | } |
||
73 | 2 | } |
|
74 | 4 | if ($data["headers"] != null && count($data["headers"]) > 0) { |
|
75 | 4 | curl_setopt($ch, CURLOPT_HTTPHEADER, $data["headers"]); |
|
76 | 4 | } |
|
77 | 4 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
78 | 4 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
79 | 4 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
80 | 4 | $result = curl_exec($ch); |
|
81 | 4 | $response_header = curl_getinfo($ch); |
|
82 | 4 | curl_close($ch); |
|
83 | |||
84 | 4 | if ($response_header["http_code"] == 0) { |
|
85 | throw new WrapItHTTPException("HTTP Connection error"); |
||
86 | } |
||
87 | |||
88 | 4 | switch ($data["response_type"]) { |
|
89 | 4 | case "json": |
|
90 | 4 | $json = json_decode($result, true); |
|
91 | 4 | if ($json !== null) { |
|
92 | 4 | return $json; |
|
93 | } else { |
||
94 | return $result; |
||
95 | } |
||
96 | break; |
||
97 | default: |
||
98 | return $result; |
||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | 1 | public function getDomain() { |
|
106 | |||
107 | abstract public function get($api, $data); |
||
109 | |||
110 | } |
||
111 |