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