YKBlackHat

Curl Class

Dec 21st, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. <?php
  2. # Change the author name don't make you become a coder
  3. # @2017 FathurFreakz
  4. class curl {
  5. public $curl;
  6. public $debug = false;
  7. public $result;
  8. public $error = array();
  9. public $requestheader;
  10. public $responseheader;
  11. public $cookiepath;
  12. public $responsecookie;
  13. public $requestcookie;
  14. public $headers = array();
  15. public $referer;
  16. public $option = array();
  17. public $httpcode;
  18. public $lasturl;
  19. public $debugvar = array();
  20. public $timeout = 30;
  21.  
  22.  
  23. function __construct(){
  24. date_default_timezone_set("Asia/Jakarta");
  25. $this->curl = curl_init();
  26. $this->setCookiePath(md5(time()));
  27. $this->setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4");
  28. $this->setOption(CURLOPT_HEADER, true);
  29. $this->setOption(CURLINFO_HEADER_OUT, true);
  30. $this->setOption(CURLOPT_RETURNTRANSFER, true);
  31. $this->setOption(CURLOPT_FOLLOWLOCATION, true);
  32. $this->setOption(CURLOPT_TIMEOUT, $this->timeout);
  33. $this->setOption(CURLOPT_SSL_VERIFYPEER, false);
  34. $this->setOption(CURLOPT_SSL_VERIFYHOST, 2);
  35. }
  36.  
  37. function setOption($option, $value) {
  38. $this->options[$option] = $value;
  39. return curl_setopt($this->curl, $option, $value);
  40. }
  41.  
  42. function debug(){
  43. $this->debugvar['DEBUG_ERROR'] = $this->error;
  44. $this->debugvar['DEBUG_REQUEST_HEADERS'] = $this->requestheader;
  45. $this->debugvar['DEBUG_RESPONSE_HEADERS'] = $this->responseheader;
  46. $this->debugvar['DEBUG_LAST_URL'] = $this->lasturl;
  47. $this->debugvar['DEBUG_RESULT'] = $this->result;
  48. return $this->debugvar;
  49. }
  50.  
  51. function setHeader($key,$value){
  52. $this->headers[$key] = $value;
  53. }
  54.  
  55. function request($method,$url,$var = false){
  56. if(!empty($var)){
  57. $data = (is_array($var) ? http_build_query($var, '', '&') : $var);
  58. $this->setOption(CURLOPT_POSTFIELDS,$data);
  59. }
  60. if(!empty($this->headers) && is_array($this->headers)){
  61. $this->setRequestHeader();
  62. }
  63. $this->setMethod($method);
  64. $this->setOption(CURLOPT_URL,$url);
  65. $this->result = curl_exec($this->curl);
  66. $this->error['code'] = curl_errno($this->curl);
  67. $this->error['msg'] = curl_error($this->curl);
  68. $this->httpcode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  69. $this->lasturl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
  70. $this->requestheader = $this->parseHeader(curl_getinfo($this->curl, CURLINFO_HEADER_OUT));
  71. $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
  72. $this->responsecookie = $this->parseCookie(substr($this->result, 0, $header_size));
  73. $this->responseheader = $this->parseHeader(substr($this->result, 0, $header_size));
  74. $this->result = substr($this->result, $header_size);
  75. if($this->debug == true){
  76. var_dump($this->debug());
  77. } else {
  78. return $this->result;
  79. }
  80. $this->unsetMethod($method);
  81. $this->unsetCurl();
  82. }
  83.  
  84. function setRequestHeader(){
  85. $headers = array();
  86. foreach ($this->headers as $key => $value) {
  87. $headers[] = $key.': '.$value;
  88. }
  89. $this->setOption(CURLOPT_HTTPHEADER, $headers);
  90. }
  91.  
  92.  
  93.  
  94. function parseHeader($response){
  95. if (!preg_match_all('/([A-Za-z\-]{1,})\:(.*)\\r/', $response, $matches) || !isset($matches[1], $matches[2])){
  96. return false;
  97. }
  98. $headers = [];
  99. foreach ($matches[1] as $index => $key){
  100. $headers[$key] = $matches[2][$index];
  101. }
  102. return $headers;
  103. }
  104.  
  105. function setMethod($method){
  106. switch (strtoupper($method)){
  107. case 'HEAD':
  108. $this->setOption(CURLOPT_CUSTOMREQUEST, $method);
  109. $this->setOption(CURLOPT_NOBODY, true);
  110. break;
  111. case 'GET':
  112. $this->setOption(CURLOPT_CUSTOMREQUEST, $method);
  113. $this->setOption(CURLOPT_HTTPGET, true);
  114. break;
  115. case 'POST':
  116. $this->setOption(CURLOPT_CUSTOMREQUEST, $method);
  117. $this->setOption(CURLOPT_POST, true);
  118. break;
  119. default:
  120. $this->setOption(CURLOPT_CUSTOMREQUEST, $method);
  121. }
  122. }
  123.  
  124. function unsetHeader(){
  125. $this->headers = array();
  126. }
  127.  
  128. function unsetCurl(){
  129. curl_close($this->curl);
  130. $this->unsetCookie();
  131. }
  132.  
  133. function unsetCookie(){
  134. if(file_exists($this->cookiepath)){
  135. unlink($this->cookiepath);
  136. }
  137. }
  138.  
  139. function unsetMethod($method){
  140. $this->unsetHeader();
  141. $this->setOption(CURLOPT_URL, false);
  142. $this->setOption(CURLOPT_CUSTOMREQUEST, null);
  143. switch (strtoupper($method)) {
  144. case 'HEAD':
  145. $this->setOption(CURLOPT_NOBODY, false);
  146. break;
  147. case 'POST':
  148. $this->setOption(CURLOPT_POST, false);
  149. $this->setOption(CURLOPT_POSTFIELDS, false);
  150. break;
  151. }
  152. }
  153.  
  154. function setCookiePath($name){
  155. $path = getcwd(). DIRECTORY_SEPARATOR . "cookie" . DIRECTORY_SEPARATOR . $name;
  156. $this->setOption(CURLOPT_COOKIEJAR, $path);
  157. $this->setOption(CURLOPT_COOKIEFILE, $path);
  158. $this->cookiepath = $path;
  159. }
  160.  
  161. function setCookie($key, $value = false){
  162. if(is_array($key)){
  163. foreach($key as $set => $cookie){
  164. $this->requestcookie[$set] = $cookie;
  165. }
  166. } else {
  167. $this->requestcookie[$key] = $value;
  168. $this->setOption(CURLOPT_COOKIE, http_build_query($this->requestcookie, '', '; '));
  169. }
  170. }
  171.  
  172.  
  173. function parseCookie($header){
  174.  
  175. preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
  176. $cookies = array();
  177. foreach($matches[1] as $item) {
  178. parse_str($item, $cookie);
  179. $cookies = array_merge($cookies, $cookie);
  180. }
  181. return $cookies;
  182. }
  183.  
  184.  
  185. function setTimeout($int) {
  186. $this->setOption(CURLOPT_TIMEOUT, intval($int));
  187. }
  188.  
  189. function post($url,$var = false){
  190. return $this->request("POST",$url,$var);
  191. }
  192.  
  193. function get($url,$var = false){
  194. return $this->request("GET",$url,$var);
  195. }
  196.  
  197. function put($url,$var = false){
  198. return $this->request("PUT",$url,$var);
  199. }
  200.  
  201. function head($url,$var = false){
  202. return $this->request("HEAD",$url,$var);
  203. }
  204.  
  205. function delete($url,$var = false){
  206. return $this->request("DELETE",$url,$var);
  207. }
  208.  
  209. public function setUserAgent($ua){
  210. $this->setOption(CURLOPT_USERAGENT, $ua);
  211. }
  212. public function setReferer($referer){
  213. $this->setOption(CURLOPT_REFERER, $referer);
  214. }
  215. public function setSocks($socks){
  216. $this->setOption(CURLOPT_PROXY, $socks);
  217. $this->setOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  218. }
  219.  
  220. function getString($start,$end,$string){
  221. preg_match_all("/" . $start . "(.*?)" . $end . "/sm",$string,$result);
  222. return (isset($result[1][0]) ? $result[1][0] : false);
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment