Created
December 27, 2016 03:28
-
-
Save Enelar/c003fee5aed0a8273c141adbb89f664d to your computer and use it in GitHub Desktop.
Curl cookie wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class curl | |
{ | |
public $url; | |
public $__curl; | |
public $useragent; | |
public $return_header; | |
public $result; | |
public function __construct($url = null) | |
{ | |
$this->url = $url; | |
$this->__curl = curl_init(); | |
$this->InitDefaultParams(); | |
} | |
private function InitDefaultParams() | |
{ | |
curl_setopt($this->__curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->__curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($this->__curl, CURLOPT_VERBOSE, false); | |
curl_setopt($this->__curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($this->__curl, CURLOPT_HEADER, true); | |
if ($this->useragent == null) | |
$this->useragent = "Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; | |
curl_setopt($this->__curl, CURLOPT_USERAGENT, $this->useragent); | |
} | |
public function SendHeaders($headers) | |
{ | |
curl_setopt($this->__curl, CURLOPT_HTTPHEADER, $headers); | |
} | |
public function UseCookieFile($filename) | |
{ | |
curl_setopt($this->__curl, CURLOPT_COOKIEFILE, $filename); | |
curl_setopt($this->__curl, CURLOPT_COOKIEJAR, $filename); | |
} | |
public function UseProxy($ip, $port = null, $login = null, $password = null) | |
{ | |
if (is_array($ip)) | |
return $this->UseProxy($ip['ip'], $ip['port'], $ip['login'], $ip['password']); | |
$string = ""; | |
if (!is_null($login)) | |
$string .= "$login:$password@"; | |
$string .= "$ip:$port"; | |
curl_setopt($this->__curl, CURLOPT_PROXY, $string); | |
} | |
public function SendPost($postdata = null) | |
{ | |
if (is_array($postdata)) | |
$postdata = http_build_query($postdata); | |
curl_setopt($this->__curl, CURLOPT_POST, !is_null($postdata)); | |
curl_setopt($this->__curl, CURLOPT_POSTFIELDS, $postdata); | |
} | |
public function Execute($url = null) | |
{ | |
if (!is_null($url)) | |
$this->url = $url; | |
curl_setopt($this->__curl, CURLOPT_URL, $this->url); | |
$response = curl_exec($this->__curl); | |
$this->result = []; | |
$header_length = curl_getinfo($this->__curl, CURLINFO_HEADER_SIZE); | |
$this->result['header'] = substr($response, 0, $header_length); | |
$this->result['body'] = substr($response, $header_length); | |
curl_close($this->__curl); | |
return $this->result['body']; | |
} | |
public function GetHeader() | |
{ | |
return $this->result['header']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('curl.php'); | |
class request | |
{ | |
private $proxy; | |
private $cookie; | |
public $ch; | |
private $refresh; | |
public function __construct() | |
{ | |
$this->refresh = true; | |
$this->IfNeedRefresh(); | |
} | |
private function IfNeedRefresh() | |
{ | |
if (!$this->refresh) | |
return; | |
$this->refresh = true; | |
$this->ch = new curl(); | |
} | |
public function SetProxy($ip, $port = null, $login = null, $password = null) | |
{ | |
if (is_null($port)) | |
{ | |
$parts = explode("@", $ip); | |
if (count($parts) > 1) | |
list($login, $password) = explode(":", array_shift($parts)); | |
list($ip, $port) = explode(":", array_shift($parts)); | |
} | |
$this->proxy = | |
[ | |
'ip' => $ip, | |
'port' => $port, | |
'login' => $login, | |
'password' => $password, | |
]; | |
} | |
public function SetCookies($cookies) | |
{ | |
$this->cookie = $cookies; | |
} | |
public function GetCookies() | |
{ | |
return $this->cookie; | |
} | |
private function MakeCookieFile() | |
{ | |
$name = tempnam(sys_get_temp_dir(), "E_CURL_COOKIER_"); | |
file_put_contents($name, $this->cookie); | |
return $name; | |
} | |
private function FreeCookieFile($name) | |
{ | |
$this->cookie = file_get_contents($name); | |
unlink($name); | |
return $this->cookie; | |
} | |
public function execute($endpoint, $post = null) | |
{ | |
$this->IfNeedRefresh(); | |
if ($post) | |
$this->ch->SendPost($post); | |
if ($this->proxy) | |
$this->ch->UseProxy($this->proxy); | |
$cookie_container = $this->MakeCookieFile(); | |
$this->ch->UseCookieFile($cookie_container); | |
//$this->ch->SendHeaders(config::HEADERS); | |
$response = $this->ch->Execute($endpoint); | |
$this->FreeCookieFile($cookie_container); | |
/* | |
echo "REQUEST: $endpoint\n"; | |
if (!is_null($post)) | |
var_dump($post); | |
echo "RESPONSE: $response\n\n"; | |
/**/ | |
return $response; | |
} | |
public function __invoke($endpoint, $post = null) | |
{ | |
return $this->execute($endpoint, $post); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment