SGIRequestAdapter.php
6 years ago
SGRequest.php
6 years ago
SGRequestAdapterWordpress.php
6 years ago
SGResponse.php
6 years ago
SGRequestAdapterWordpress.php
194 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 | ); |
| 59 | |
| 60 | if (!function_exists("curl_init")) { |
| 61 | $args['sslverify'] = true; |
| 62 | } |
| 63 | |
| 64 | return $args; |
| 65 | } |
| 66 | |
| 67 | public function sendPostRequest() |
| 68 | { |
| 69 | $body = null; |
| 70 | |
| 71 | if (count($this->params)) { |
| 72 | // $body = http_build_query($this->params, '', '&'); |
| 73 | $body = $this->params; |
| 74 | } |
| 75 | |
| 76 | $args = $this->getRequestArgs(); |
| 77 | $args['body'] = $body; |
| 78 | |
| 79 | $response = wp_remote_post($this->url, $args); |
| 80 | if ($this->stream && !($response instanceof WP_Error)) { |
| 81 | $this->body = file_get_contents($response['filename']); |
| 82 | } |
| 83 | else { |
| 84 | $this->body = wp_remote_retrieve_body($response); |
| 85 | } |
| 86 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 87 | |
| 88 | $headers = wp_remote_retrieve_headers($response); |
| 89 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 90 | $data = $headers->getAll(); |
| 91 | $this->contentType = $data['content-type']; |
| 92 | } |
| 93 | else if ($headers && is_array($headers)) { |
| 94 | $this->contentType = $headers['content-type']; |
| 95 | } |
| 96 | else { |
| 97 | $this->contentType = ''; |
| 98 | } |
| 99 | |
| 100 | return $this->parseResponse(); |
| 101 | } |
| 102 | |
| 103 | public function sendGetRequest() |
| 104 | { |
| 105 | $args = $this->getRequestArgs(); |
| 106 | |
| 107 | if (count($this->params)) { |
| 108 | $this->url = rtrim($this->url, '/').'/'; |
| 109 | |
| 110 | if ($this->getWithQueryParams) { //standard get url, with query params |
| 111 | $this->url .= '?'.http_build_query($this->params, '', '&'); |
| 112 | } |
| 113 | else { //mvs-styled get url |
| 114 | $this->url .= implode('/', array_values($this->params)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | $response = wp_remote_get($this->url, $args); |
| 119 | if ($this->stream && !($response instanceof WP_Error)) { |
| 120 | $this->body = file_get_contents($response['filename']); |
| 121 | } |
| 122 | else { |
| 123 | $this->body = wp_remote_retrieve_body($response); |
| 124 | } |
| 125 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 126 | |
| 127 | $headers = wp_remote_retrieve_headers($response); |
| 128 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 129 | $data = $headers->getAll(); |
| 130 | $this->contentType = $data['content-type']; |
| 131 | } |
| 132 | else if ($headers && is_array($headers)) { |
| 133 | $this->contentType = $headers['content-type']; |
| 134 | } |
| 135 | else { |
| 136 | $this->contentType = ''; |
| 137 | } |
| 138 | |
| 139 | return $this->parseResponse(); |
| 140 | } |
| 141 | |
| 142 | public function sendRequest($type) |
| 143 | { |
| 144 | $body = null; |
| 145 | |
| 146 | if (count($this->params)) { |
| 147 | // $body = http_build_query($this->params, '', '&'); |
| 148 | $body = $this->params; |
| 149 | } |
| 150 | |
| 151 | $args = $this->getRequestArgs(); |
| 152 | $args['body'] = $body; |
| 153 | $args['method'] = $type; |
| 154 | |
| 155 | $response = wp_remote_request($this->url, $args); |
| 156 | if ($this->stream && !($response instanceof WP_Error)) { |
| 157 | $this->body = file_get_contents($response['filename']); |
| 158 | } |
| 159 | else { |
| 160 | $this->body = wp_remote_retrieve_body($response); |
| 161 | } |
| 162 | $this->httpCode = wp_remote_retrieve_response_code($response); |
| 163 | |
| 164 | $headers = wp_remote_retrieve_headers($response); |
| 165 | if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) { |
| 166 | $data = $headers->getAll(); |
| 167 | $this->contentType = $data['content-type']; |
| 168 | } |
| 169 | else if ($headers && is_array($headers)) { |
| 170 | $this->contentType = $headers['content-type']; |
| 171 | } |
| 172 | else { |
| 173 | $this->contentType = ''; |
| 174 | } |
| 175 | |
| 176 | return $this->parseResponse(); |
| 177 | } |
| 178 | |
| 179 | public function parseResponse() |
| 180 | { |
| 181 | $response = new SGResponse(); |
| 182 | $response->setBody($this->body); |
| 183 | $response->setHttpStatus($this->httpCode); |
| 184 | $response->setContentType($this->contentType); |
| 185 | |
| 186 | //if the response is in json format, decode it |
| 187 | if ($this->contentType == 'application/json') { |
| 188 | $response->parseJsonBody(); |
| 189 | } |
| 190 | |
| 191 | return $response; |
| 192 | } |
| 193 | } |
| 194 |