Commit 495cb234 authored by Alex Ne's avatar Alex Ne

new http client

parent 0c0eb96f
<?php
/**
*
*/
class X_HTTP_Client
{
*
*/
class X_HTTP_Client {
/**
* @var string
*/
public $UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:41.0) Gecko/20100101 Firefox/41.0";
public $HEADERS = array();
public $Cookies = array();
/**
* @var array
*/
public $HEADERS = [];
/**
* @var array
*/
public $Cookies = [];
/**
* @var mixed
*/
public $Proxy = false;
/**
* @var int
*/
public $Timeout = 60;
function __construct($cfg=[])
{
foreach ($cfg as $key => $value)
{
switch (strtolower($key))
{
case 'proxy': $this->Proxy = $value; break;
case 'useragent': $this->UserAgent = $value; break;
case 'cookies': $this->Cookies = $value; break;
case 'headers': $this->HEADERS = $value; break;
case 'timeout': $this->Timeout = $value; break;
/**
* @param array $cfg
*/
public function __construct($cfg = []) {
foreach ($cfg as $key => $value) {
switch (strtolower($key)) {
case 'proxy':
$this->Proxy = $value;
break;
case 'useragent':
$this->UserAgent = $value;
break;
case 'cookies':
$this->Cookies = $value;
break;
case 'headers':
$this->HEADERS = $value;
break;
case 'timeout':
$this->Timeout = $value;
break;
}
}
}
public function GET($URL)
{
$this->HEADERS[] = "User-Agent: ".$this->UserAgent;
$cookies_arr = array();
foreach( $this->Cookies as $k => $v ) { $cookies_arr[] = $k."=".$v; }
/**
* @param $URL
* @return mixed
*/
public function GET($URL) {
$this->HEADERS[] = "User-Agent: " . $this->UserAgent;
$cookies_arr = [];
foreach ($this->Cookies as $k => $v) {$cookies_arr[] = $k . "=" . $v;}
$result = false;
$curl = curl_init();
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $URL);
//curl_setopt($curl, CURLOPT_HEADER, true);
if( count($cookies_arr) > 0 ) curl_setopt($curl, CURLOPT_COOKIE, implode("; ",$cookies_arr));
if (count($cookies_arr) > 0) {
curl_setopt($curl, CURLOPT_COOKIE, implode("; ", $cookies_arr));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->HEADERS);
if( $this->Proxy ) curl_setopt($curl, CURLOPT_PROXY, $this->Proxy);
if ($this->Proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->Proxy);
}
curl_setopt($curl, CURLOPT_TIMEOUT, $this->Timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function POST($URL)
{
# NOT YET
/**
* @param $URL
*/
public function POST($URL) {
# NOT YET
}
}
?>
\ No newline at end of file
<?php
namespace X\Network\Http;
class Client extends ClientSettings {
/**
* @return mixed
*/
public function exec($url = false) {
if ($url) {
$this->parse_url($url);
}
if (isset($this->data["useragent"]) && is_string($this->data["useragent"])) {
$headers[] = "User-Agent: {$this->data["useragent"]}";
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->build_url());
if (isset($this->data["cookies"]) && is_array($this->data["cookies"])) {
curl_setopt($curl, CURLOPT_COOKIE, $this->implode_cookies());
}
if (isset($headers)) {
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
}
#if ($this->Proxy) {
# curl_setopt($curl, CURLOPT_PROXY, $this->Proxy);
#}
if (isset($this->data["post"]) && is_array($this->data["post"])) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->build_post());
}
curl_setopt($curl, CURLOPT_TIMEOUT, $this->data["timeout"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, true);
//curl_setopt($curl, CURLOPT_VERBOSE, 1);
$result = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
return new ClientResponse($header_size, $result);
}
}
?>
\ No newline at end of file
<?php
namespace X\Network\Http;
class ClientResponse {
/**
* @var mixed
*/
protected $curl_result;
/**
* @var mixed
*/
protected $header_size;
/**
* @var mixed
*/
protected $header;
/**
* @var mixed
*/
protected $body;
/**
* @param $curl_result
*/
public function __construct($header_size, $curl_result) {
$this->curl_result = $curl_result;
$this->header_size = $header_size;
$this->header = substr($curl_result, 0, $header_size);
$this->body = substr($curl_result, $header_size);
}
/**
* @return mixed
*/
public function get_header_size() {
return $this->header_size;
}
/**
* @return mixed
*/
public function get_header() {
return $this->header;
}
/**
* @return mixed
*/
public function get_body() {
return $this->body;
}
public function get_json() {
return json_decode($this->body, true);
}
}
?>
\ No newline at end of file
<?php
namespace X\Network\Http;
class ClientSettings {
/**
* @var array
*/
protected $url_data =
[
"scheme" => false,
"host" => false,
"path" => false,
"query" => false,
];
/**
* @var array
*/
protected $data =
[
"get" => false,
"post" => false,
"put" => false,
"timeout" => 10,
"cookies" => false,
"useragent" => false,
];
/**
* @param $data
*/
public function __construct($data = false) {
if (is_string($data)) {
$this->parse_url($data);
} else if (is_array($data)) {
$this->set_model_data($data);
}
}
/**
* @param $url
* @return mixed
*/
public function parse_url($url) {
if (is_array($data = parse_url($url))) {
$this->url_data = $data;
if (isset($this->url_data["query"])) {
$out = [];
parse_str($this->url_data["query"], $out);
if (is_array($this->data["get"])) {
$this->data["get"] = array_merge($this->data["get"], $out);
} else {
$this->data["get"] = $out;
}
}
return $this;
}
}
/**
* @param $data
*/
public function set_model_data($data) {
if (isset($data["url"])) {
$this->parse_url($data["url"]);
}
if (isset($data["scheme"])) {
$this->url_data["scheme"] = $data["scheme"];
}
if (isset($data["host"])) {
$this->url_data["host"] = $data["host"];
}
if (isset($data["path"])) {
$this->url_data["path"] = $data["path"];
}
if (isset($data["query"])) {
$this->url_data["query"] = $data["query"];
}
if (isset($data["get"])) {
$this->data["get"] = $data["get"];
}
if (isset($data["post"])) {
$this->data["post"] = $data["post"];
}
if (isset($data["put"])) {
$this->data["put"] = $data["put"];
}
if (isset($data["cookies"])) {
$this->data["cookies"] = $data["cookies"];
}
if (isset($data["useragent"])) {
$this->data["useragent"] = $data["useragent"];
}
if (isset($data["timeout"])) {
$this->data["timeout"] = intval($data["timeout"]) ?: 10;
}
}
/**
* @param $name
* @param $value
*/
public function add_getvar($name, $value) {
$this->data["get"][$name] = $value;
}
/**
* @param $name
* @param $value
*/
public function add_postvar($name, $value) {
$this->data["post"][$name] = $value;
}
/**
* @param $value
*/
public function set_putvar($value) {
$this->data["put"] = $value;
}
protected function build_url() {
if (isset($this->url_data["scheme"]) && isset($this->url_data["host"]) && isset($this->url_data["path"])) {
return "{$this->url_data["scheme"]}://{$this->url_data["host"]}{$this->url_data["path"]}" .
(is_array($this->data["get"]) ? "?" . http_build_query($this->data["get"]) : "");
} else {
throw new \X\ETrace\System("bad url params");
}
}
protected function build_post() {
return (isset($this->data["post"]) && is_array($this->data["post"])) ? http_build_query($this->data["post"]) : "";
}
/**
* @param $data
*/
protected function implode_cookies() {
$data = $this->data["cookies"];
array_walk($data, function (&$i, $k) {$i = implode("=", [$k, $i]);});
return implode("; ", $data);
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment