PageRenderTime 55ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/Helpers/EasyCurlHelperClasses.php

https://github.com/aajiwani/Easy-CURL-for-PHP
PHP | 274 lines | 229 code | 34 blank | 11 comment | 21 complexity | 84ccfca670e0e6fbe9d7dbb1de49a8d7 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Amir Ali Jiwani <amir.ali@pi-labs.net>
  4. * @copyright 2011
  5. * @link http://www.facebook.com/aajiwani
  6. * @version 1.0
  7. */
  8. class EasyCurlHTTPVersion
  9. {
  10. const VERSION_NONE = CURL_HTTP_VERSION_NONE;
  11. const VERSION_1_0 = CURL_HTTP_VERSION_1_0;
  12. const VERSION_1_1 = CURL_HTTP_VERSION_1_1;
  13. }
  14. class EasyCurlRequestType
  15. {
  16. const GET = 1; // CURLOPT_HTTPGET -> true
  17. const POST = 2; // CURLOPT_POST -> true
  18. }
  19. class EasyCurlCookie
  20. {
  21. const DOMAIN_ATTR = 'domain';
  22. const PATH_ATTR = 'path';
  23. const EXPIRES_ATTR = 'expires';
  24. const MAX_AGE_ATTR = 'max-age';
  25. const VERSION_ATTR = 'version';
  26. const SECURE_ATTR = 'secure';
  27. const HTTPONLY_ATTR = 'httponly';
  28. public $Domain;
  29. public $Expires = null;
  30. public $Path;
  31. public $Version;
  32. public $IsSecure;
  33. public $HttpOnly;
  34. public $CookieName;
  35. public $CookieValue;
  36. public function __construct($name, $value)
  37. {
  38. if (is_string($name) && is_string($value))
  39. {
  40. $this->CookieName = $name;
  41. $this->CookieValue = $value;
  42. }
  43. else
  44. {
  45. throw new EasyCurlCookieException("Name and value provided for cookie must be of type string.");
  46. }
  47. }
  48. public function SetMaxAge($seconds)
  49. {
  50. $this->Expires = time() + $seconds;
  51. }
  52. public function SetExpires($expiresValue)
  53. {
  54. $this->Expires = strtotime($expiresValue);
  55. }
  56. public function SetSecure($value = true)
  57. {
  58. $this->IsSecure = $value;
  59. }
  60. public function SetHttpOnly($value = true)
  61. {
  62. $this->HttpOnly = $value;
  63. }
  64. public function GetDateWithFormat($format)
  65. {
  66. if ($this->Expires)
  67. {
  68. return date($format, $this->Expires);
  69. }
  70. return null;
  71. }
  72. }
  73. class EasyCurlPostParameter
  74. {
  75. public $ParamName;
  76. public $ParamValue;
  77. private $ParamType;
  78. const VALUE_PARAMETER = 1;
  79. const FILE_PARAMETER = 2;
  80. public function __construct($name, $value, $type = EasyCurlPostParameter::VALUE_PARAMETER)
  81. {
  82. $this->ParamType = $type;
  83. if ($type == self::FILE_PARAMETER)
  84. {
  85. $this->ParamName = $name;
  86. $this->ParamValue = '@' . $value;
  87. }
  88. else if ($type == self::VALUE_PARAMETER)
  89. {
  90. $this->ParamName = $name;
  91. $this->ParamValue = $value;
  92. }
  93. else
  94. {
  95. throw new EasyCurlPostParameterException("Unknown type of parameter specified.");
  96. }
  97. }
  98. }
  99. class EasyCurlHeader
  100. {
  101. public $HeaderName;
  102. public $HeaderValue;
  103. public function __construct($name, $value)
  104. {
  105. $this->HeaderName = $name;
  106. $this->HeaderValue = $value;
  107. }
  108. }
  109. class EasyCurlExecuterCallback
  110. {
  111. public $Receiver = null;
  112. public $Callback = null;
  113. public function __construct($callback, $receiver = null)
  114. {
  115. $isCallable = false;
  116. if ($receiver != null)
  117. {
  118. $isCallable = is_callable(array($receiver, $callback), true);
  119. if ($isCallable)
  120. {
  121. $this->Receiver = $receiver;
  122. $this->Callback = $callback;
  123. }
  124. else
  125. {
  126. throw new EasyCurlExecuterCallbackException("Callback expected");
  127. }
  128. }
  129. else
  130. {
  131. $isCallable = is_callable($callback, false, $callable_name);
  132. if ($isCallable)
  133. {
  134. $this->Callback = $callable_name;
  135. }
  136. else
  137. {
  138. throw new EasyCurlExecuterCallbackException("Callback expected");
  139. }
  140. }
  141. }
  142. public function Call($parameters = null)
  143. {
  144. if ($this->Receiver != null && $this->Callback != null)
  145. {
  146. $this->SendCallbackToReceiver($parameters);
  147. }
  148. else
  149. {
  150. $this->SendCallback($parameters);
  151. }
  152. }
  153. private function SendCallback($parameters = null)
  154. {
  155. if ($parameters != null)
  156. {
  157. call_user_func($this->Callback, $parameters);
  158. }
  159. else
  160. {
  161. call_user_func($this->Callback);
  162. }
  163. }
  164. private function SendCallbackToReceiver($parameters = null)
  165. {
  166. if ($parameters != null)
  167. {
  168. call_user_func(array($this->Receiver, $this->Callback), $parameters);
  169. }
  170. else
  171. {
  172. call_user_func(array($this->Receiver, $this->Callback));
  173. }
  174. }
  175. }
  176. class EasyCurlExecuterNode
  177. {
  178. public $Url;
  179. public $CurlResource;
  180. public $Callback;
  181. public $UserObject;
  182. }
  183. class EasyCurlExecuterResult
  184. {
  185. public $Url;
  186. public $Response;
  187. public $UserState;
  188. }
  189. class EasyCurlHTTPCodeInfo
  190. {
  191. public static $RESPONSE_MESSAGES = array(
  192. //[Informational 1xx]
  193. 100 => "Continue",
  194. 101 => "Switching Protocols",
  195. //[Successful 2xx]
  196. 200 => "OK",
  197. 201 => "Created",
  198. 202 => "Accepted",
  199. 203 => "Non-Authoritative Information",
  200. 204 => "No Content",
  201. 205 => "Reset Content",
  202. 206 => "Partial Content",
  203. //[Redirection 3xx]
  204. 300 => "Multiple Choices",
  205. 301 => "Moved Permanently",
  206. 302 => "Found",
  207. 303 => "See Other",
  208. 304 => "Not Modified",
  209. 305 => "Use Proxy",
  210. 306 => "(Unused)",
  211. 307 => "Temporary Redirect",
  212. //[Client Error 4xx]
  213. 400 => "Bad Request",
  214. 401 => "Unauthorized",
  215. 402 => "Payment Required",
  216. 403 => "Forbidden",
  217. 404 => "Not Found",
  218. 405 => "Method Not Allowed",
  219. 406 => "Not Acceptable",
  220. 407 => "Proxy Authentication Required",
  221. 408 => "Request Timeout",
  222. 409 => "Conflict",
  223. 410 => "Gone",
  224. 411 => "Length Required",
  225. 412 => "Precondition Failed",
  226. 413 => "Request Entity Too Large",
  227. 414 => "Request-URI Too Long",
  228. 415 => "Unsupported Media Type",
  229. 416 => "Requested Range Not Satisfiable",
  230. 417 => "Expectation Failed",
  231. //[Server Error 5xx]
  232. 500 => "Internal Server Error",
  233. 501 => "Not Implemented",
  234. 502 => "Bad Gateway",
  235. 503 => "Service Unavailable",
  236. 504 => "Gateway Timeout",
  237. 505 => "HTTP Version Not Supported"
  238. );
  239. }
  240. ?>