PluginProbe
PostNL for WooCommerce / 3.1.4
PostNL for WooCommerce v3.1.4
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / class-wcmp-rest.php

class-wcmp-rest.php in PostNL for WooCommerce 3.1.4, at includes/class-wcmp-rest.php

190 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined('ABSPATH')) exit; // Exit if accessed directly
4
5 /**
6 * A simple JSON REST request abstraction layer
7 */
8 class WC_PostNL_REST_Client {
9
10 /**
11 * Handle for the current cURL session
12 * @var
13 */
14 private $curl = null;
15 /**
16 * Default cURL settings
17 * @var
18 */
19 protected $curlDefaults = array(
20 // BOOLEANS
21 CURLOPT_AUTOREFERER => true, // Update referer on redirects
22 CURLOPT_FAILONERROR => false, // Return false on HTTP code > 400
23 CURLOPT_FOLLOWLOCATION => false, // DON'T Follow redirects
24 CURLOPT_RETURNTRANSFER => true,
25 CURLOPT_FRESH_CONNECT => true, // Don't use cached connection
26 CURLOPT_FORBID_REUSE => true, // Close connection
27
28 // INTEGERS
29 CURLOPT_TIMEOUT => 10, // cURL timeout
30 CURLOPT_CONNECTTIMEOUT => 10, // Connection timeout
31
32 // STRINGS
33 CURLOPT_ENCODING => "", // "identity", "deflate", and "gzip"
34 CURLOPT_SSL_VERIFYPEER => false, // if all else fails :)
35 );
36
37 /**
38 * Basic constructor
39 * Checks for cURL and initialize options
40 * @return void
41 */
42 function __construct() {
43 if ( ! function_exists("curl_init")) {
44 throw new Exception("cURL is not installed on this system");
45 }
46
47 $this->curl = curl_init();
48 if ( ! is_resource($this->curl) || ! isset($this->curl)) {
49 throw new Exception("Unable to create cURL session");
50 }
51
52 $options = $this->curlDefaults;
53 $options[CURLOPT_CAINFO] = dirname(__FILE__) . 'lib/ca-bundle.pem'; // Use bundled PEM file to avoid issues with Windows servers
54
55 if ((ini_get('open_basedir') == '') AND ( ! ini_get('safe_mode'))) {
56 $options[CURLOPT_FOLLOWLOCATION] = true;
57 }
58
59 $success = curl_setopt_array($this->curl, $options);
60 if ($success !== true) {
61 throw new Exception("cURL Error: " . curl_error($this->curl));
62 }
63 }
64
65 /**
66 * Closes the current cURL connection
67 */
68 public function close() {
69 @curl_close($this->curl);
70 }
71
72 function __destruct() {
73 $this->close();
74 }
75
76 /**
77 * Returns last error message
78 * @return string Error message
79 */
80 public function error() {
81 return curl_error($this->curl);
82 }
83
84 /**
85 * Returns last error code
86 * @return int
87 */
88 public function errno() {
89 return curl_errno($this->curl);
90 } // end function
91
92 public function get($url, $headers = array(), $raw = false) {
93 return $this->request($url, "GET", $headers, null, null, $raw);
94 }
95
96 public function post($url, $post, $headers = array(), $raw = false) {
97 return $this->request($url, "POST", $headers, $post, null, $raw);
98 }
99
100 public function put($url, $body, $headers = array(), $raw = false) {
101 return $this->request($url, "PUT", $headers, null, $body, $raw);
102 }
103
104 public function delete($url, $headers = array(), $raw = false) {
105 return $this->request($url, "GET", $headers, null, null, $raw);
106 }
107
108 public function request($url, $method = "GET", $headers = array(), $post, $body = null, $raw = false) {
109 // Set the method and related options
110 switch($method) {
111 case "PUT":
112 throw new Exception('Can not put PostNL shipment', 500);
113 break;
114
115 case "POST":
116 $response = wp_remote_post( $url, array('body' => $post, 'headers' => $headers));
117 break;
118
119 case "DELETE":
120 throw new Exception('Can not delete PostNL shipment', 500);
121 break;
122
123 case "GET":
124 default:
125 $response = wp_remote_get($url, $headers);
126 break;
127 }
128
129 // Close any open resource handle
130 if (isset($f) && is_resource($f)) {
131 @fclose($f);
132 }
133
134 $status = $response["response"]["code"];
135 $body = $response['body'];
136
137 if ($raw !== true) {
138 $body = json_decode($body, true); // The second parameter set to true returns objects as associative arrays
139 }
140
141 if ($status > 400) {
142 if ($raw === true) {
143 $body = json_decode($body, true);
144 }
145
146 if ( ! empty($body["errors"])) {
147 $error = $this->parse_errors($body);
148 } else if ( ! empty($body["message"])) {
149 $error = $body["message"];
150 } else {
151 $error = "Unknown error";
152 }
153 throw new Exception($error, $status);
154 }
155
156 return array("code" => $status, "body" => $body, "headers" => $response["headers"]);
157 }
158
159 public function parse_errors($body) {
160 $errors = $body['errors'];
161 $message = isset($body['message']) ? $body['message'] : '';
162
163 $parsed_errors = array();
164 foreach ($errors as $error) {
165 $code = isset($error['code']) ? $error['code'] : '';
166
167 if (isset($error['human']) && is_array($error['human'])) {
168 foreach ($error['human'] as $key => $human_error) {
169 $parsed_errors[$code] = "{$human_error} (<strong>Code {$code}</strong>)";
170 }
171 } else if (isset($error['message'])) {
172 $parsed_errors[$code] = "{$error['message']} (<strong>Code {$code}</strong>)";
173 } else {
174 $parsed_errors[$code] = "{$message} (<strong>Code {$code}</strong>)";
175 }
176 }
177
178 if (count($parsed_errors) == 1) {
179 $html = array_shift($parsed_errors);
180 } else {
181 foreach ($parsed_errors as &$parsed_error) {
182 $parsed_error = "<li>{$parsed_error}</li>";
183 }
184 $html = sprintf("<ul>%s</ul>", implode("\n", $parsed_errors));
185 }
186
187 return $html;
188 }
189 }
190