WC_MyParcel_REST_Client

A simple JSON REST request abstraction layer.

Defined (1)

The class is defined in the following location(s).

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