<?php
declare(strict_types=1);
/**
* Class Http
*/
class Http
{
private $sp = "\r\n"; //这里必须要写成双引号 private $protocol = 'HTTP/1.1';
private $requestLine = "";
private $requestHeader = "";
private $requestInfo = "";
private $fp = null;
private $urlInfo = null;
private $header = [];
private $body = "";
private static $http = null; //Http对象单例
/**
* Http constructor.
*/
private function __construct()
{
}
/**
* @return Http|null
*/
public static function getInstance()
{
if (self::$http === null) {
self::$http = new Http();
}
return self::$http;
}
/**
* @param $url
* @return $this
*/
public function init($url)
{
$this->parseUrl($url);
$this->header['Host'] = $this->urlInfo['host'];
return $this;
}
/**
* @param array $header
* @return Generator
*/
public function get($header = []): Generator
{
$this->header = array_merge($this->header, $header);
return $this->request('GET');
}
/**
* @param array $header
* @param array $body
* @return bool
*/
public function post($header = [], $body = [])
{
$this->header = array_merge($this->header, $header);
if (!empty($body)) {
$this->body = http_build_query($body);
$this->header['Content-Type'] = 'application/x-www-form-urlencoded';
$this->header['Content-Length'] = strlen($this->body);
}
return $this->request('POST');
}
/**
* @param $method
* @return bool
*/
private function request($method)
{
$header = "";
$this->requestLine = $method . ' ' . $this->urlInfo['path'] . '?' . $this->urlInfo['query'];
foreach ($this->header as $key => $value) {
$header .= $header == "" ? $key . ':' . $value : $this->sp . $key . ':' . $value;
}
$this->requestHeader = $header . $this->sp . $this->sp;
$this->requestInfo = $this->requestLine . $this->sp . $this->requestHeader;
if ($this->body != "") {
$this->requestInfo .= $this->body;
}
$port = isset($this->urlInfo['port
declare(strict_types=1);
/**
* Class Http
*/
class Http
{
private $sp = "\r\n"; //这里必须要写成双引号 private $protocol = 'HTTP/1.1';
private $requestLine = "";
private $requestHeader = "";
private $requestInfo = "";
private $fp = null;
private $urlInfo = null;
private $header = [];
private $body = "";
private static $http = null; //Http对象单例
/**
* Http constructor.
*/
private function __construct()
{
}
/**
* @return Http|null
*/
public static function getInstance()
{
if (self::$http === null) {
self::$http = new Http();
}
return self::$http;
}
/**
* @param $url
* @return $this
*/
public function init($url)
{
$this->parseUrl($url);
$this->header['Host'] = $this->urlInfo['host'];
return $this;
}
/**
* @param array $header
* @return Generator
*/
public function get($header = []): Generator
{
$this->header = array_merge($this->header, $header);
return $this->request('GET');
}
/**
* @param array $header
* @param array $body
* @return bool
*/
public function post($header = [], $body = [])
{
$this->header = array_merge($this->header, $header);
if (!empty($body)) {
$this->body = http_build_query($body);
$this->header['Content-Type'] = 'application/x-www-form-urlencoded';
$this->header['Content-Length'] = strlen($this->body);
}
return $this->request('POST');
}
/**
* @param $method
* @return bool
*/
private function request($method)
{
$header = "";
$this->requestLine = $method . ' ' . $this->urlInfo['path'] . '?' . $this->urlInfo['query'];
foreach ($this->header as $key => $value) {
$header .= $header == "" ? $key . ':' . $value : $this->sp . $key . ':' . $value;
}
$this->requestHeader = $header . $this->sp . $this->sp;
$this->requestInfo = $this->requestLine . $this->sp . $this->requestHeader;
if ($this->body != "") {
$this->requestInfo .= $this->body;
}
$port = isset($this->urlInfo['port