PageRenderTime 478ms CodeModel.GetById 14ms RepoModel.GetById 3ms app.codeStats 0ms

/classes/restclient.php

https://github.com/MilkZoft/zan
PHP | 181 lines | 181 code | 0 blank | 0 comment | 1 complexity | 2f4e9f0b9e92305e8db9e4d4941fe820 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_RESTClient extends ZP_Load
  6. {
  7. private $auth = false;
  8. public function DELETE($data = false, $return = false)
  9. {
  10. if ($data !== true) {
  11. $data = is_array($data) ? http_build_query($data) : $data;
  12. if (is_null($this->URL) or is_null($data)) {
  13. return false;
  14. }
  15. } else {
  16. if (is_null($this->URL)) {
  17. return false;
  18. }
  19. }
  20. if ($ch = curl_init($this->URL)) {
  21. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  22. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  23. curl_setopt($ch, CURLOPT_HEADER, false);
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  26. if ($this->auth) {
  27. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  28. curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
  29. }
  30. $response = curl_exec($ch);
  31. $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  32. curl_close($ch);
  33. if ($status === 200) {
  34. if ($return) {
  35. return $response;
  36. }
  37. if (strstr($response, "xml")) {
  38. return new SimpleXMLElement($response);
  39. } else {
  40. return json_decode($response);
  41. }
  42. }
  43. }
  44. return false;
  45. }
  46. public function GET($return = false)
  47. {
  48. if (is_null($this->URL)) {
  49. return false;
  50. }
  51. $ch = curl_init($this->URL);
  52. if ($ch) {
  53. curl_setopt($ch, CURLOPT_URL, $this->URL);
  54. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
  55. if ($this->auth) {
  56. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  57. curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
  58. }
  59. $response = curl_exec($ch);
  60. $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  61. curl_close($ch);
  62. if ($status === 200) {
  63. if ($return) {
  64. return $response;
  65. }
  66. return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);
  67. }
  68. }
  69. return false;
  70. }
  71. public function POST($data = null, $return = false)
  72. {
  73. if (is_null($this->URL) or is_null($data)) {
  74. return false;
  75. }
  76. if ($ch = curl_init($this->URL)) {
  77. curl_setopt($ch, CURLOPT_URL, $this->URL);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($ch, CURLOPT_POST, true);
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  81. if (_get("environment") <= 3) {
  82. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  83. }
  84. if ($this->auth) {
  85. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  86. curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
  87. }
  88. $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  89. $response = curl_exec($ch);
  90. curl_close($ch);
  91. $ch = curl_init();
  92. if ($status === 200) {
  93. if ($return) {
  94. return $response;
  95. }
  96. return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);
  97. }
  98. }
  99. return false;
  100. }
  101. public function PUT($data = null, $return = false)
  102. {
  103. $data = is_array($data) ? http_build_query($data) : $data;
  104. if (is_null($this->URL) or is_null($data)) {
  105. return false;
  106. }
  107. if ($ch = curl_init($this->URL)) {
  108. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  110. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Length: ". strlen($data)));
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  112. if ($this->auth) {
  113. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  114. curl_setopt($ch, CURLOPT_USERPWD, $this->username .":". $this->password);
  115. }
  116. $response = curl_exec($ch);
  117. $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
  118. curl_close($ch);
  119. if ($status === 200) {
  120. if ($return) {
  121. return $response;
  122. }
  123. return (strstr($response, "xml")) ? new SimpleXMLElement($response) : json_decode($response, true);
  124. }
  125. }
  126. return false;
  127. }
  128. public function setAuth($username = null, $password = null)
  129. {
  130. if (!is_null($username) and !is_null($password)) {
  131. $this->auth = true;
  132. $this->username = $username;
  133. $this->password = $password;
  134. } else {
  135. $this->auth = false;
  136. }
  137. return false;
  138. }
  139. public function setURL($URL)
  140. {
  141. $this->URL = $URL;
  142. }
  143. }