Advertisement
ObjectClass

Untitled

Jan 18th, 2019
2,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Curl
  4. {
  5.     protected static $userAgent = 'Mozilla/5.0 (Linux; Android 6.0.1; SM-G532F Build/MMB29T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.103 YaBrowser/18.7.1.575.00 Mobile Safari/537.36';
  6.     protected static $arrParams = [
  7.         'error' => false,
  8.         'headers' => false,
  9.         'user_agent' => null,
  10.     ];
  11.    
  12.     public function __construct ($arrParams = [])
  13.     {
  14.         $this->params = (object) array_merge(self::$arrParams, $arrParams);
  15.        
  16.         if (empty ($this->params->user_agent)) $this->params->user_agent = self::$userAgent;
  17.     }
  18.    
  19.     public static function get($url = null, $headers = false, $info = false)
  20.     {
  21.         $response = false;
  22.        
  23.         if (!empty ($url))
  24.         {
  25.            
  26.             if (!$this->params->cookie_file)
  27.             {
  28.             $cookieFile = parse_url($url)['host'].'.txt';
  29.             }
  30.             else
  31.             {
  32.                 $cookieFile = $this->params->cookie_file;
  33.             }
  34.            
  35.             $ch = curl_init($url);
  36.            
  37.             curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
  38.             curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
  39.            
  40.             curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  41.             curl_setopt($ch, CURLOPT_USERAGENT, $this->params->user_agent);
  42.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  43.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  44.             curl_setopt($ch, CURLOPT_ENCODING, "");
  45.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  46.             curl_setopt($ch, CURLOPT_HEADER, $this->params->headers);
  47.            
  48.             $request = curl_exec($ch);
  49.            
  50.             if (!$info && !$this->params->error && $request !== false)
  51.             {
  52.                 $response = $request;
  53.             }
  54.             elseif ($request === false && $this->params->error)
  55.             {
  56.                 $response = [
  57.                     'error' => curl_getinfo($ch),
  58.                     'html' => '<h1 style="color: red;">?????? cURL</h1>'
  59.                 ];
  60.             }
  61.             elseif ($info)
  62.             {
  63.                 $response = [
  64.                     'html' => $request,
  65.                     'info' => curl_getinfo($ch)
  66.                 ];
  67.             }
  68.         }
  69.         return $response;
  70.     }
  71.    
  72.     public static function post($url = null, $arrData = [], $headers = false, $info = false)
  73.     {
  74.         $response = false;
  75.        
  76.         if (!empty ($url) && !empty ($arrData))
  77.         {
  78.             if (empty($this->params['cookie_file']))
  79.             {
  80.                 $cookieFile = parse_url($url)['host'].'.txt';
  81.             }
  82.             else
  83.             {
  84.                 $cookieFile = $this->params->cookie_file;
  85.             }
  86.            
  87.             $ch = curl_init($url);
  88.            
  89.             curl_setopt($ch, CURLOPT_POST, 1);
  90.             curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($arrData) ? http_build_query($arrData, '', '&') : $arrData);
  91.            
  92.             curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
  93.             curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
  94.            
  95.             curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  96.             curl_setopt($ch, CURLOPT_USERAGENT, $this->params->user_agent);
  97.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  98.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  99.             curl_setopt($ch, CURLOPT_ENCODING, "");
  100.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  101.             curl_setopt($ch, CURLOPT_HEADER, $this->params->headers);
  102.            
  103.             $request = curl_exec($ch);
  104.            
  105.             if (!$info && !$this->params->error && $request !== false)
  106.             {
  107.                 $response = $request;
  108.             }
  109.             elseif ($request === false && $this->params->error)
  110.             {
  111.                 $response = [
  112.                     'error' => curl_getinfo($ch),
  113.                     'html' => '<h1 style="color: red;">?????? cURL</h1>'
  114.                 ];
  115.             }
  116.             elseif ($info)
  117.             {
  118.                 $response = [
  119.                     'html' => $request,
  120.                     'info' => curl_getinfo($ch)
  121.                 ];
  122.             }
  123.         }
  124.         return $response;
  125.     }
  126.    
  127.     public function design($url = null, $html = null)
  128.     {
  129.         $response = false;
  130.        
  131.         if (!empty($html) && !empty($url))
  132.         {
  133.             $response = preg_replace('/src="(.+)"/', 'src="http://'.parse_url($url)['host'].'$1"', $html);
  134.             $response = preg_replace('/rel="(.+)" href="(.+)"/', 'rel="$1" href="http://'.parse_url($url)['host'].'$2"', $response);
  135.         }
  136.         return $response;
  137.     }
  138.    
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement