PluginProbe
PostNL for WooCommerce / 2.5.1
PostNL for WooCommerce v2.5.1
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 2.5.1, at includes/class-wcmp-rest.php

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