Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Curl
- {
- 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';
- protected static $arrParams = [
- 'error' => false,
- 'headers' => false,
- 'user_agent' => null,
- ];
- public function __construct ($arrParams = [])
- {
- $this->params = (object) array_merge(self::$arrParams, $arrParams);
- if (empty ($this->params->user_agent)) $this->params->user_agent = self::$userAgent;
- }
- public static function get($url = null, $headers = false, $info = false)
- {
- $response = false;
- if (!empty ($url))
- {
- if (!$this->params->cookie_file)
- {
- $cookieFile = parse_url($url)['host'].'.txt';
- }
- else
- {
- $cookieFile = $this->params->cookie_file;
- }
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
- curl_setopt($ch, CURLOPT_AUTOREFERER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, $this->params->user_agent);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_ENCODING, "");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_HEADER, $this->params->headers);
- $request = curl_exec($ch);
- if (!$info && !$this->params->error && $request !== false)
- {
- $response = $request;
- }
- elseif ($request === false && $this->params->error)
- {
- $response = [
- 'error' => curl_getinfo($ch),
- 'html' => '<h1 style="color: red;">?????? cURL</h1>'
- ];
- }
- elseif ($info)
- {
- $response = [
- 'html' => $request,
- 'info' => curl_getinfo($ch)
- ];
- }
- }
- return $response;
- }
- public static function post($url = null, $arrData = [], $headers = false, $info = false)
- {
- $response = false;
- if (!empty ($url) && !empty ($arrData))
- {
- if (empty($this->params['cookie_file']))
- {
- $cookieFile = parse_url($url)['host'].'.txt';
- }
- else
- {
- $cookieFile = $this->params->cookie_file;
- }
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($arrData) ? http_build_query($arrData, '', '&') : $arrData);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
- curl_setopt($ch, CURLOPT_AUTOREFERER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, $this->params->user_agent);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_ENCODING, "");
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_HEADER, $this->params->headers);
- $request = curl_exec($ch);
- if (!$info && !$this->params->error && $request !== false)
- {
- $response = $request;
- }
- elseif ($request === false && $this->params->error)
- {
- $response = [
- 'error' => curl_getinfo($ch),
- 'html' => '<h1 style="color: red;">?????? cURL</h1>'
- ];
- }
- elseif ($info)
- {
- $response = [
- 'html' => $request,
- 'info' => curl_getinfo($ch)
- ];
- }
- }
- return $response;
- }
- public function design($url = null, $html = null)
- {
- $response = false;
- if (!empty($html) && !empty($url))
- {
- $response = preg_replace('/src="(.+)"/', 'src="http://'.parse_url($url)['host'].'$1"', $html);
- $response = preg_replace('/rel="(.+)" href="(.+)"/', 'rel="$1" href="http://'.parse_url($url)['host'].'$2"', $response);
- }
- return $response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement