SGIRequestAdapter.php
5 years ago
SGRequest.php
5 years ago
SGRequestAdapterWordpress.php
5 years ago
SGResponse.php
5 years ago
SGRequestAdapterWordpress.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | require_once(SG_REQUEST_PATH.'SGIRequestAdapter.php'); |
| 4 | require_once(SG_REQUEST_PATH.'SGResponse.php'); |
| 5 | |
| 6 | class SGRequestAdapterWordpress implements SGIRequestAdapter |
| 7 | { |
| 8 | private $headers = array(); |
| 9 | private $params = array(); |
| 10 | private $url; |
| 11 | private $getWithQueryParams = true; |
| 12 | |
| 13 | private $body; |
| 14 | private $httpCode; |
| 15 | private $contentType; |
| 16 | |
| 17 | private $stream = false; |
| 18 | |
| 19 | public function __construct() |
| 20 | { |
| 21 | $reloadMethod = SGConfig::get('SG_BACKGROUND_RELOAD_METHOD'); |
| 22 | if ($reloadMethod == SG_RELOAD_METHOD_STREAM) { |
| 23 | $this->stream = true; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public function setGetWithQueryParams($getWithQueryParams) |
| 28 | { |
| 29 | $this->getWithQueryParams = $getWithQueryParams; |
| 30 | } |
| 31 | |
| 32 | public function addHeader($header) |
| 33 | { |
| 34 | $this->headers[] = $header; |
| 35 | } |
| 36 | |
| 37 | public function setHeaders($headers) |
| 38 | { |
| 39 | $this->headers = $headers; |
| 40 | } |
| 41 | |
| 42 | public function setUrl($url) |
| 43 | { |
| 44 | $this->url = $url; |
| 45 | } |
| 46 | |
| 47 | public function setParams($params) |
| 48 | { |
| 49 | $this->params = $params; |
| 50 | } |
| 51 | |
| 52 | public function getRequestArgs() |
| 53 | { |
| 54 | $args = array( |
| 55 | 'headers' => $this->headers, |
| 56 | 'sslverify' => false, |
| 57 | 'stream' => $this->stream, |
| 58 | 'timeout' => 30 |
| 59 | ); |
| 60 | |
| 61 | if (!function_exists("curl_init")) { |
| 62 | $args['sslverify'] = true; |
| 63 | } |
| 64 | |
| 65 | return $args; |
| 66 | } |
| 67 | |
| 68 | public function sendPostRequest() |
| 69 | { |
| 70 | $body = null; |
| 71 | |
| 72 | if (count($this->params)) { |
| 73 | // $body = http_build_query($this->params, '', '&'); |
| 74 | $body = $this->params; |
| 75 | } |
| 76 | |
| 77 | $args = $this->getRequestArgs(); |
| 78 | $args['body'] = $body; |
| 79 | |
| 80 | $response = wp_remote_post($this->url, $args); |
| 81 | if ($this->stream && !($response instanceof WP_Error)) { |
| 82 | $this->body = file_get_contents($response['filename']); |
| 83 | } |
| 84 | else { |
| 85 | $this->body = wp_remote_retrieve_body($response); |
| 86 | } |
| 87 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 88 | |
| 89 | $headers = wp_remote_retrieve_headers($response); |
| 90 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 91 | $data = $headers->getAll(); |
| 92 | $this->contentType = $data['content-type']; |
| 93 | } |
| 94 | else if ($headers && is_array($headers)) { |
| 95 | $this->contentType = $headers['content-type']; |
| 96 | } |
| 97 | else { |
| 98 | $this->contentType = ''; |
| 99 | } |
| 100 | |
| 101 | return $this->parseResponse(); |
| 102 | } |
| 103 | |
| 104 | public function sendGetRequest() |
| 105 | { |
| 106 | $args = $this->getRequestArgs(); |
| 107 | |
| 108 | if (count($this->params)) { |
| 109 | $this->url = rtrim($this->url, '/').'/'; |
| 110 | |
| 111 | if ($this->getWithQueryParams) { //standard get url, with query params |
| 112 | $this->url .= '?'.http_build_query($this->params, '', '&'); |
| 113 | } |
| 114 | else { //mvs-styled get url |
| 115 | $this->url .= implode('/', array_values($this->params)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $response = wp_remote_get($this->url, $args); |
| 120 | if ($this->stream && !($response instanceof WP_Error)) { |
| 121 | $this->body = file_get_contents($response['filename']); |
| 122 | } |
| 123 | else { |
| 124 | $this->body = wp_remote_retrieve_body($response); |
| 125 | } |
| 126 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 127 | |
| 128 | $headers = wp_remote_retrieve_headers($response); |
| 129 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 130 | $data = $headers->getAll(); |
| 131 | $this->contentType = $data['content-type']; |
| 132 | } |
| 133 | else if ($headers && is_array($headers)) { |
| 134 | $this->contentType = $headers['content-type']; |
| 135 | } |
| 136 | else { |
| 137 | $this->contentType = ''; |
| 138 | } |
| 139 | |
| 140 | return $this->parseResponse(); |
| 141 | } |
| 142 | |
| 143 | public function sendRequest($type) |
| 144 | { |
| 145 | $body = null; |
| 146 | |
| 147 | if ($this->params) { |
| 148 | // $body = http_build_query($this->params, '', '&'); |
| 149 | $body = $this->params; |
| 150 | } |
| 151 | |
| 152 | $args = $this->getRequestArgs(); |
| 153 | $args['body'] = $body; |
| 154 | $args['method'] = $type; |
| 155 | |
| 156 | $response = wp_remote_request($this->url, $args); |
| 157 | if ($this->stream && !($response instanceof WP_Error)) { |
| 158 | $this->body = file_get_contents($response['filename']); |
| 159 | } |
| 160 | else { |
| 161 | $this->body = wp_remote_retrieve_body($response); |
| 162 | } |
| 163 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 164 | |
| 165 | $headers = wp_remote_retrieve_headers($response); |
| 166 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 167 | $data = $headers->getAll(); |
| 168 | $this->contentType = $data['content-type']; |
| 169 | } |
| 170 | else if ($headers && is_array($headers)) { |
| 171 | $this->contentType = $headers['content-type']; |
| 172 | } |
| 173 | else { |
| 174 | $this->contentType = ''; |
| 175 | } |
| 176 | |
| 177 | return $this->parseResponse(); |
| 178 | } |
| 179 | |
| 180 | public function parseResponse() |
| 181 | { |
| 182 | $response = new SGResponse(); |
| 183 | $response->setBody($this->body); |
| 184 | $response->setHttpStatus($this->httpCode); |
| 185 | $response->setContentType($this->contentType); |
| 186 | |
| 187 | //if the response is in json format, decode it |
| 188 | if ($this->contentType == 'application/json') { |
| 189 | $response->parseJsonBody(); |
| 190 | } |
| 191 | |
| 192 | return $response; |
| 193 | } |
| 194 | } |
| 195 |