26 private $_host =
'localhost';
38 private $_sock =
null;
44 private $_headers = [];
50 private $_postFields = [];
56 private $_cookies = [];
62 private $_responseHeaders = [];
68 private $_responseBody =
'';
74 private $_responseStatus = 0;
80 private $_timeout = 300;
86 private $_redirectCount = 0;
96 $this->_timeout = (int)
$value;
108 $this->
connect($host, (
int)$port);
121 $this->_host = $host;
122 $this->_port = (int)$port;
132 @fclose($this->_sock);
142 $this->_headers = $headers;
165 unset($this->_headers[
$name]);
178 $val = base64_encode(
"{$login}:{$pass}");
179 $this->
addHeader(
"Authorization",
"Basic {$val}");
202 unset($this->_cookies[
$name]);
213 $this->_cookies = $cookies;
232 public function get($uri)
247 $parts = parse_url($uri);
248 if (!empty($parts[
'user']) && !empty($parts[
'pass'])) {
251 if (!empty($parts[
'port'])) {
252 $this->_port = (int)$parts[
'port'];
255 if (!empty($parts[
'host'])) {
256 $this->_host = $parts[
'host'];
258 throw new \InvalidArgumentException(
"Uri doesn't contain host part");
261 if (!empty($parts[
'path'])) {
262 $requestUri = $parts[
'path'];
264 throw new \InvalidArgumentException(
"Uri doesn't contain path part");
266 if (!empty($parts[
'query'])) {
267 $requestUri .=
"?" . $parts[
'query'];
291 return $this->_responseHeaders;
301 return $this->_responseBody;
311 if (empty($this->_responseHeaders[
'Set-Cookie'])) {
315 foreach ($this->_responseHeaders[
'Set-Cookie'] as
$row) {
321 list($key, $val) = explode(
"=",
$values[0]);
325 $out[trim($key)] = trim($val);
337 if (empty($this->_responseHeaders[
'Set-Cookie'])) {
341 foreach ($this->_responseHeaders[
'Set-Cookie'] as
$row) {
347 list($key, $val) = explode(
"=",
$values[0]);
351 $out[trim($key)] = [
'value' => trim($val)];
357 for (
$i = 0;
$i < $c;
$i++) {
358 list($subkey, $val) = explode(
"=",
$values[
$i]);
359 $out[trim($key)][trim($subkey)] = trim($val);
373 $this->_responseHeaders = [];
374 while (!
feof($this->_sock)) {
375 $line = fgets($this->_sock, 1024);
376 if ($line === $crlf) {
380 $out = explode(
": ", trim($line), 2);
381 if (count($out) == 2) {
386 if (
$name ==
"Set-Cookie") {
387 if (!isset($this->_responseHeaders[
$name])) {
388 $this->_responseHeaders[
$name] = [];
405 $this->_responseBody =
'';
407 while (!
feof($this->_sock)) {
408 $this->_responseBody .= @fread($this->_sock, 1024);
421 $responseLine = trim(fgets($this->_sock, 1024));
423 $line = explode(
" ", $responseLine, 3);
424 if (count($line) != 3) {
425 return $this->
doError(
"Invalid response line returned from server: " . $responseLine);
427 $this->_responseStatus = intval($line[1]);
453 return $this->_responseStatus;
467 $errno = $errstr =
'';
468 $this->_sock = @
fsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout);
470 return $this->
doError(sprintf(
"[errno: %d] %s", $errno, $errstr));
480 $appendHeaders[
'Content-type'] =
'application/x-www-form-urlencoded';
481 $appendHeaders[
'Content-length'] = strlen($paramsStr);
484 $out =
"{$method} {$uri} HTTP/1.1{$crlf}";
488 $out .= $paramsStr . $crlf;
491 fwrite($this->_sock, $out);
503 throw new \Exception($string);
514 $headers[
"Host"] = $this->_host;
515 $headers[
'Connection'] =
"close";
516 $headers = array_merge($headers, $this->_headers, $append);
518 foreach ($headers as $k => $v) {
519 $str[] =
"{$k}: {$v}\r\n";
521 return implode($str);
fsockopen(&$errorNumber, &$errorMessage)
makeRequest($method, $uri, $params=[])
setCredentials($login, $pass)
headersToString($append=[])
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
__construct($host=null, $port=80)
if(!isset($_GET['name'])) $name