CurlEmulator.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. if (!extension_loaded('curl')) {
  3. // The curl option constants
  4. define ('CURLOPT_RETURNTRANSFER2', 19913);
  5. define ('CURLOPT_SSL_VERIFYPEER2', 64);
  6. define ('CURLOPT_SSL_VERIFYHOST2', 81);
  7. define ('CURLOPT_USERAGENT2', 10018);
  8. define ('CURLOPT_HEADER2', 42);
  9. define ('CURLOPT_CUSTOMREQUEST2', 10036);
  10. define ('CURLOPT_POST2', 47);
  11. define ('CURLOPT_POSTFIELDS2', 10015);
  12. define ('CURLOPT_HTTPHEADER2', 10023);
  13. define ('CURLOPT_URL2', 10002);
  14. define ('CURLOPT_HTTPGET2', 80); // this could be a good idea to handle params as array
  15. define ('CURLOPT_CONNECTTIMEOUT2', 78);
  16. define ('CURLOPT_TIMEOUT2', 13);
  17. define ('CURLOPT_CAINFO2', 10065);
  18. define ('CURLOPT_SSLVERSION2', 32);
  19. define ('CURLOPT_FOLLOWLOCATION2', 52);
  20. define ('CURLOPT_FORBID_REUSE2', 75);
  21. define ('CURLOPT_HTTP_VERSION2', 84);
  22. define ('CURLOPT_MAXREDIRS2', 68);
  23. define ('CURLOPT_ENCODING2', 10102);
  24. // curl info constants
  25. define ('CURLINFO_HEADER_SIZE2', 2097163);
  26. define ('CURLINFO_HTTP_CODE2', 2097154);
  27. define ('CURLINFO_HEADER_OUT2', 2); // This seems to be an option?
  28. define ('CURLINFO_TOTAL_TIME2', 3145731);
  29. define ('CURLE_SSL_CACERT2', 60);
  30. define ('CURLE_SSL_PEER_CERTIFICATE2', 51);
  31. define ('CURLE_SSL_CACERT_BADFILE2', 77);
  32. define ('CURLE_COULDNT_CONNECT2', 7);
  33. define ('CURLE_OPERATION_TIMEOUTED2', 28);
  34. define ('CURLE_COULDNT_RESOLVE_HOST2', 6);
  35. class CurlEmulator
  36. {
  37. // Storing the result in here
  38. private $result;
  39. // The headers of the result will be stored here
  40. private $responseHeader;
  41. // url for request
  42. private $url;
  43. // options
  44. private $options = [];
  45. public function __construct($url)
  46. {
  47. $this->url = $url;
  48. }
  49. public function setOpt($option, $value)
  50. {
  51. $this->options[$option] = $value;
  52. }
  53. public function getInfo($opt = 0)
  54. {
  55. if (!$this->result) {
  56. $this->fetchResult();
  57. }
  58. $responseHeaderSize = 0;
  59. foreach ($this->responseHeader as $header)
  60. $responseHeaderSize += (strlen($header) + 2); // The one is for each newline
  61. $httpCode = 200;
  62. if (preg_match('#HTTP/\d+\.\d+ (\d+)#', $this->responseHeader[0], $matches))
  63. $httpCode = intval($matches[1]);
  64. // opt
  65. if ($opt == CURLINFO_HEADER_SIZE2)
  66. return $responseHeaderSize;
  67. if ($opt == CURLINFO_HTTP_CODE2)
  68. return $httpCode;
  69. return [
  70. "url" => $this->url,
  71. "content_type" => "",
  72. "http_code" => $httpCode,
  73. "header_size" => $responseHeaderSize,
  74. "request_size" => 0,
  75. "filetime" => 0,
  76. "ssl_verify_result" => null,
  77. "redirect_count" => 0,
  78. "total_time" => 0,
  79. "namelookup_time" => 0,
  80. "connect_time" => 0,
  81. "pretransfer_time" => 0,
  82. "size_upload" => 0,
  83. "size_download" => 0,
  84. "speed_download" => 0,
  85. "speed_upload" => 0,
  86. "download_content_length" => 0,
  87. "upload_content_length" => 0,
  88. "starttransfer_time" => 0,
  89. "redirect_time" => 0,
  90. "certinfo" => 0,
  91. "request_header" => 0
  92. ];
  93. }
  94. public function exec()
  95. {
  96. $this->fetchResult();
  97. $fullResult = $this->result;
  98. if ($this->getValue(CURLOPT_HEADER2, false)) {
  99. $headers = implode("\r\n", $this->responseHeader);
  100. $fullResult = $headers . "\r\n" . $this->result;
  101. }
  102. if ($this->getValue(CURLOPT_RETURNTRANSFER2, false) == false) {
  103. print $fullResult;
  104. } else {
  105. return $fullResult;
  106. }
  107. }
  108. private function fetchResult()
  109. {
  110. // Create the context for this request based on the curl parameters
  111. // Determine the method
  112. if (!$this->getValue(CURLOPT_CUSTOMREQUEST2, false) && $this->getValue(CURLOPT_POST2, false)) {
  113. $method = 'POST';
  114. } else {
  115. $method = $this->getValue(CURLOPT_CUSTOMREQUEST2, 'GET');
  116. }
  117. // Add the post header if type is post and it has not been added
  118. if ($method == 'POST') {
  119. if (is_array($this->getValue(CURLOPT_HTTPHEADER2))) {
  120. $found = false;
  121. foreach ($this->getValue(CURLOPT_HTTPHEADER2, array()) as $header) {
  122. if (strtolower($header) == strtolower('Content-type: application/x-www-form-urlencoded')) {
  123. $found = true;
  124. }
  125. }
  126. // add post header if not found
  127. if (!$found) {
  128. $headers = $this->getValue(CURLOPT_HTTPHEADER2, array());
  129. $headers[] = 'Content-type: application/x-www-form-urlencoded';
  130. $this->setOpt(CURLOPT_HTTPHEADER2, $headers);
  131. }
  132. }
  133. }
  134. // Determine the content which can be an array or a string
  135. if (is_array($this->getValue(CURLOPT_POSTFIELDS2))) {
  136. $content = http_build_query($this->getValue(CURLOPT_POSTFIELDS2, array()));
  137. } else {
  138. $content = $this->getValue(CURLOPT_POSTFIELDS2, "");
  139. }
  140. // get timeout
  141. $timeout = $this->getValue(CURLOPT_TIMEOUT2, 60);
  142. $connectTimeout = $this->getValue(CURLOPT_CONNECTTIMEOUT2, 30);
  143. // take bigger timeout
  144. if ($connectTimeout > $timeout)
  145. $timeout = $connectTimeout;
  146. $headers = $this->getValue(CURLOPT_HTTPHEADER2, "");
  147. if (is_array($headers)) {
  148. $headers = join("\r\n", $headers);
  149. }
  150. // 'http' instead of $parsedUrl['scheme']; https doest work atm
  151. $options = array(
  152. 'http' => array(
  153. "timeout" => $timeout,
  154. "ignore_errors" => true,
  155. 'method' => $method,
  156. 'header' => $headers,
  157. 'content' => $content
  158. )
  159. );
  160. $options["http"]["follow_location"] = $this->getValue(CURLOPT_FOLLOWLOCATION2, 1);
  161. // get url from options
  162. if ($this->getValue(CURLOPT_URL2, false))
  163. $this->url = $this->getValue(CURLOPT_URL2);
  164. $context = stream_context_create($options);
  165. $this->result = file_get_contents($this->url, false, $context);
  166. $this->responseHeader = $http_response_header;
  167. }
  168. private function getValue($value, $default = null)
  169. {
  170. if (isset($this->options[$value]) && $this->options[$value]) {
  171. return $this->options[$value];
  172. }
  173. return $default;
  174. }
  175. public function errNo()
  176. {
  177. return 0;
  178. }
  179. public function error()
  180. {
  181. return "";
  182. }
  183. public function close()
  184. {
  185. }
  186. }
  187. function curl_init2($url = null)
  188. {
  189. return new CurlEmulator($url);
  190. }
  191. function curl_setopt2($ch, $option, $value)
  192. {
  193. $ch->setOpt($option, $value);
  194. }
  195. function curl_exec2($ch)
  196. {
  197. return $ch->exec();
  198. }
  199. function curl_getinfo2($ch, $option = 0)
  200. {
  201. return $ch->getInfo($option);
  202. }
  203. function curl_errno2($ch) {
  204. return $ch->errNo();
  205. }
  206. function curl_error2($ch) {
  207. return $ch->error();
  208. }
  209. function curl_close2($ch) {
  210. return $ch->close();
  211. }
  212. function curl_setopt_array2($ch, $options) {
  213. foreach ($options as $option => $value) {
  214. curl_setopt2($ch, $option, $value);
  215. }
  216. }
  217. }