Advertisement
D3vBl4ck

CurlX

Jul 27th, 2020 (edited)
16,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.76 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CurlX Lib - v0.0.2
  4.  * How to use? -> https://github.com/devblack/curlx
  5.  * COD3D BY D3VBL4CK
  6.  */
  7. class CurlX
  8. {
  9.     /**
  10.      * private access to response
  11.      * @var response
  12.      */
  13.     private static $response;
  14.  
  15.     /**
  16.      * public access to info args of requests
  17.      * @var info
  18.      */
  19.     private static $info;
  20.  
  21.     /**
  22.      * private and global var
  23.      * @var ch
  24.      */
  25.     private static $ch;
  26.  
  27.     /**
  28.      * private cookie-file var
  29.      * @var cookiefile
  30.      */
  31.     private static $cookiefile;
  32.  
  33.     /**
  34.      * requests error number, example: 5
  35.      * @var error_code
  36.      */
  37.     private static $error_code;
  38.  
  39.     /**
  40.      * requests error string, example: CURLE_COULDNT_RESOLVE_PROXY
  41.      * @var error_string
  42.      */
  43.     private static $error_string;
  44.  
  45.     /**
  46.      * Default Options for request structure
  47.      */
  48.     private static $default = [
  49.         CURLOPT_RETURNTRANSFER => true,
  50.         CURLOPT_HEADER         => false,
  51.         CURLOPT_FOLLOWLOCATION => true,
  52.         CURLOPT_AUTOREFERER    => true,
  53.         CURLOPT_CONNECTTIMEOUT => 70,
  54.         CURLOPT_TIMEOUT        => 70,
  55.         CURLOPT_SSL_VERIFYPEER => false,
  56.         CURLOPT_SSL_VERIFYHOST => 0,
  57.         CURLOPT_VERBOSE => 1
  58.     ];
  59.  
  60.     /**
  61.      * function prepare, basic curl_init for requests
  62.      * @param url
  63.      * @return void
  64.      */
  65.     private static function prepare(string $url) {
  66.         self::$ch = curl_init($url);
  67.     }
  68.  
  69.     /**
  70.      * function header, set a header for request structure
  71.      * @param header
  72.      * @return void
  73.      */
  74.     private static function header(array $header) {
  75.         curl_setopt(self::$ch, CURLOPT_HTTPHEADER, $header);
  76.     }
  77.  
  78.     /**
  79.      *  function tunnel, support HTTP/S, SOCKS4, SOCKS5 for request structure
  80.      * @param args
  81.      * @return void
  82.      */
  83.     private static function tunnel(array $args) {
  84.         curl_setopt_array(self::$ch, [
  85.             CURLOPT_PROXY => "{$args['TYPE']}://{$args['SERVER']}",
  86.             CURLOPT_HTTPPROXYTUNNEL => true
  87.         ]);
  88.     }
  89.  
  90.     /**
  91.      * function luminati, set a luminati auto-router proxy configuration
  92.      * @param args
  93.      * @return void
  94.      */
  95.     private static function luminati(array $args) {
  96.         curl_setopt_array(self::$ch, [
  97.             CURLOPT_PROXY => 'http://zproxy.lum-superproxy.io:22225',
  98.             CURLOPT_PROXYUSERPWD => "{$args['USERNAME']}-route_err-pass_dyn-country-{$args['COUNTRY']}-session-{$args['SESSION']}:{$args['PASSWORD']}"
  99.         ]);
  100.     }
  101.  
  102.     /**
  103.      * function apify, set a apify auto-router proxy configuration
  104.      * @param args
  105.      * @return void
  106.      */
  107.     private static function apify(array $args) {
  108.         curl_setopt_array(self::$ch, [
  109.             CURLOPT_PROXY => 'http://proxy.apify.com:8000',
  110.             CURLOPT_PROXYUSERPWD => "auto:{$args['PASSWORD']}"
  111.         ]);
  112.     }
  113.  
  114.     /**
  115.      * function RandProxy, get a rand proxy from proxies.txt
  116.      * @return proxy
  117.      */
  118.     public static function RandProxy() {
  119.         $proxy = file("proxies.txt");
  120.         return $proxy[rand(0, (count($proxy) - 1))];
  121.     }
  122.  
  123.     /**
  124.      * function AutoRouter, detect the tunnel configuration
  125.      * @param args
  126.      * @return void
  127.      */
  128.     private static function AutoRouter($args) {
  129.         switch (strtoupper($args['METHOD'])) {
  130.             case 'TUNNEL':
  131.                 self::tunnel($args);
  132.             break;
  133.             case 'LUMINATI':
  134.                 self::luminati($args);
  135.             break;
  136.             case 'APIFY':
  137.                 self::apify($args);
  138.             break;
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * function cookies, created a file in the temp dir and saved it
  144.      * @param file
  145.      * @return void
  146.      */
  147.     private static function cookies(string $file) {
  148.         self::$cookiefile = sys_get_temp_dir."/$file.txt";
  149.         curl_setopt_array(self::$ch, [
  150.             CURLOPT_COOKIEJAR => self::$cookiefile,
  151.             CURLOPT_COOKIEFILE => self::$cookiefile
  152.         ]);
  153.     }
  154.  
  155.     /**
  156.      * function deleteCookie, this function delete the current cookie file of the current request
  157.      * @return bool
  158.      */
  159.     public static function deleteCookie() {
  160.         unlink(self::$cookiefile);
  161.     }
  162.  
  163.     /**
  164.      * Check parameters
  165.      */
  166.     private static function CheckParam(array $headers=NULL, string $cookie=NULL, array $server=NULL) {
  167.         if (!empty($headers) && is_array($headers))
  168.             self::header($headers);
  169.  
  170.         if (!empty($cookie))
  171.             self::cookies($cookie);
  172.  
  173.         if (!empty($server) && is_array($server))
  174.             self::AutoRouter($server);
  175.     }
  176.  
  177.     /**
  178.      * function get, this function send a get request with custom headers, cookies and server tunnel
  179.      *
  180.      * @param url
  181.      * @param headers
  182.      * @param cookie
  183.      * @param server
  184.      *
  185.      * @return response|error_string
  186.      */
  187.     public static function get(string $url, array $headers=NULL, string $cookie=NULL, array $server=NULL) {
  188.         $options = array_replace(self::$default, [
  189.             CURLOPT_USERAGENT => self::userAgent()
  190.         ]);
  191.  
  192.         self::prepare($url);
  193.  
  194.         curl_setopt_array(self::$ch, $options);
  195.  
  196.         self::CheckParam($headers, $cookie, $server);
  197.  
  198.         return self::run();
  199.     }
  200.  
  201.     /**
  202.      * function post, this function send a post request with custom post data, headers, cookies and server tunnel
  203.      *
  204.      * @param url
  205.      * @param data
  206.      * @param headers
  207.      * @param cookie
  208.      * @param server
  209.      *
  210.      * @return response|error_string
  211.      */
  212.     public static function post(string $url, mixed $data=NULL, array $headers=NULL, string $cookie=NULL, array $server=NULL) {
  213.         $options = array_replace(self::$default,[
  214.             CURLOPT_USERAGENT      => self::userAgent(),
  215.             CURLOPT_POST           => true,
  216.             CURLOPT_POSTFIELDS     => is_array($data) ? json_encode($data) : $data
  217.         ]);
  218.  
  219.         self::prepare($url);
  220.  
  221.         curl_setopt_array(self::$ch, $options);
  222.  
  223.         self::CheckParam($headers, $cookie, $server);
  224.  
  225.         return self::run();
  226.     }
  227.  
  228.     /**
  229.      * function put, this function send a post request with custom post data, headers, cookies and server tunnel
  230.      *
  231.      * @param url
  232.      * @param data
  233.      * @param headers
  234.      * @param cookie
  235.      * @param server
  236.      *
  237.      * @return response|error_string
  238.      */
  239.     public static function put(string $url, mixed $data=NULL, array $headers=NULL, string $cookie=NULL, array $server=NULL) {
  240.         $options = array_replace(self::$default, [
  241.             CURLOPT_USERAGENT      => self::userAgent(),
  242.             CURLOPT_CUSTOMREQUEST  => "PUT",
  243.             CURLOPT_POSTFIELDS     => is_array($data) ? json_encode($data) : $data,
  244.         ]);
  245.  
  246.         self::prepare($url);
  247.  
  248.         curl_setopt_array(self::$ch, $options);
  249.  
  250.         self::CheckParam($headers, $cookie, $server);
  251.  
  252.         return self::run();
  253.     }
  254.  
  255.     /**
  256.      * function delete, this function send a post request with custom post data, headers, cookies and server tunnel
  257.      *
  258.      * @param url
  259.      * @param data
  260.      * @param headers
  261.      * @param cookie
  262.      * @param server
  263.      *
  264.      * @return response|error_string
  265.      */
  266.     public static function delete(string $url, mixed $data=NULL, array $headers=NULL, string $cookie=NULL, array $server=NULL) {
  267.         $options = array_replace(self::$default, [
  268.             CURLOPT_USERAGENT      => self::userAgent(),
  269.             CURLOPT_CUSTOMREQUEST  => 'DELETE',
  270.             CURLOPT_POSTFIELDS     => is_array($data) ? json_encode($data) : $data,
  271.         ]);
  272.  
  273.         self::prepare($url);
  274.  
  275.         curl_setopt_array(self::$ch, $options);
  276.  
  277.         self::CheckParam($headers, $cookie, $server);
  278.  
  279.         return self::run();
  280.     }
  281.  
  282.     /**
  283.      * function run, send the request structure
  284.      * @return response|error_string
  285.      */
  286.     private static function run() {
  287.         self::$response = curl_exec(self::$ch);
  288.         self::$info = curl_getinfo(self::$ch);
  289.  
  290.         // Request failed
  291.         if (self::$response === FALSE) {
  292.             self::$error_code = curl_errno(self::$ch);
  293.             self::$error_string = curl_error(self::$ch);
  294.  
  295.             curl_close(self::$ch);
  296.  
  297.             return (object) [
  298.                 'status_code' => self::$info['http_code'],
  299.                 'headers'  => self::$info,
  300.                 'body'     => self::$error_string,
  301.                 'errno'    => self::$error_code,
  302.                 'error'    => self::$error_string
  303.             ];
  304.         } else {
  305.             curl_close(self::$ch);
  306.  
  307.             return (object) [
  308.                 'status_code' => self::$info['http_code'],
  309.                 'headers'  => self::$info,
  310.                 'body'     => self::$response
  311.             ];
  312.         }
  313.     }
  314.  
  315.     /**
  316.      * function debug, this function show all data process|errors of the request
  317.      * @return information|errors
  318.      */
  319.     public static function debug() {
  320.         echo "=============================================<br/>\n";
  321.         echo "<h2>REQUESTS DEBUG</h2>\n";
  322.         echo "=============================================<br/>\n";
  323.         echo "<h3>Response</h3>\n";
  324.         echo "<code>" . nl2br(htmlentities(self::$response)) . "</code><br/>\n\n";
  325.  
  326.         if (self::$error_string) {
  327.             echo "=============================================<br/>\n";
  328.             echo "<h3>Errors</h3>";
  329.             echo "<strong>Code:</strong> " . self::$error_code . "<br/>\n";
  330.             echo "<strong>Message:</strong> " . self::$error_string . "<br/>\n";
  331.         }
  332.  
  333.         echo "=============================================<br/>\n";
  334.         echo "<h3>Info</h3>";
  335.         echo "<pre>";
  336.         print_r(self::$info);
  337.         echo "</pre>";
  338.     }
  339.  
  340.     /**
  341.      * function ParseString, this function can split a string by two strings
  342.      * @return string
  343.      */
  344.     public static function ParseString(string $string, string $start, string $end) {
  345.         return explode($end, explode($start, $string)[1])[0];
  346.     }
  347.  
  348.     /**
  349.      * function userAgent, this function return a random user agent
  350.      * @return string
  351.      */
  352.     private static function userAgent() {
  353.         $uas = [
  354.             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
  355.             "Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36",
  356.             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9",
  357.             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
  358.             "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1",
  359.             "Mozilla/5.0 (Linux; Android 7.0; SM-G892A Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/60.0.3112.107 Mobile Safari/537.36",
  360.             "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0",
  361.             "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0",
  362.             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
  363.             "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/44.0.2403.155 Safari/537.36",
  364.             "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
  365.             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36",
  366.             "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0",
  367.             "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:62.0) Gecko/20100101 Firefox/62.0",
  368.             "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; Avant Browser; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)",
  369.             "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Avant Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022; InfoPath.2)",
  370.             "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Avant Browser; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30618; InfoPath.1)",
  371.             "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)",
  372.             "Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.27; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
  373.             "Mozilla/4.0 (compatible; MSIE 8.0; AOL 9.7; AOLBuild 4343.21; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)",
  374.             "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.7 (KHTML, like Gecko) Comodo_Dragon/16.1.1.0 Chrome/16.0.912.63 Safari/535.7",
  375.             "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14931",
  376.             "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
  377.             "Opera/9.80 (Macintosh; Intel Mac OS X 10.14.1) Presto/2.12.388 Version/12.16",
  378.             "Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14",
  379.             "Mozilla/5.0 (Windows; U; Windows NT 6.1; x64; fr; rv:1.9.1.1) Gecko/20090722 Firefox/3.5.1 Orca/1.2 build 2",
  380.             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A",
  381.             "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25",
  382.             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  383.             "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
  384.             "Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
  385.             "Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9",
  386.             "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+",
  387.             "Mozilla/5.0 (BlackBerry; U; BlackBerry 9860; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.254 Mobile Safari/534.11+"
  388.         ];
  389.         return $uas[array_rand($uas)];
  390.     }
  391. }
  392.  
  393. new CurlX;
  394. /***
  395.  * COD3D BY D3VBL4CK
  396.  */
  397.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement