[TOC]
* * * * *
# 1 網絡請求
>> 網絡請求
>對客戶端而言,指服務器發起的請求操作。
>對服務器端而言,指客戶端發起的請求信息。
>服務器端主要用來對客戶端發起的網絡請求進行處理。
# 2 請求信息
## 2-1 Url相關
### Request->url()
>>獲取當前完整的url(包含QUERY_STRING)
~~~
public function url($url = null)
{
if (!is_null($url) && true !== $url) {
$this->url = $url;
return $this;
} elseif (!$this->url) {
if (IS_CLI) {
$this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
} elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$this->url = $_SERVER['HTTP_X_REWRITE_URL'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$this->url = $_SERVER['REQUEST_URI'];
} elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
$this->url = $_SERVER['ORIG_PATH_INFO'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
} else {
$this->url = '';
}
}
return true === $url ? $this->domain() . $this->url : $this->url;
}
~~~
### Request->baseUrl()
>>獲取當前URL,(不包含QUERY_STRING)
~~~
public function baseUrl($url = null)
{
if (!is_null($url) && true !== $url) {
$this->baseUrl = $url;
return $this;
} elseif (!$this->baseUrl) {
$str = $this->url();
$this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
}
return true === $url ? $this->domain() . $this->baseUrl : $this->baseUrl;
}
~~~
### Request->scheme()
>>獲取URL中的scheme參數(http或者https)
~~~
public function scheme()
{
return $this->isSsl() ? 'https' : 'http';
}
~~~
### Request->domain()
>>獲取URL的域名信息(如http://baidu.com)
~~~
public function domain($domain = null)
{
if (!is_null($domain)) {
$this->domain = $domain;
return $this;
} elseif (!$this->domain) {
$this->domain = $this->scheme() . '://' . $this->host();
}
return $this->domain;
}
~~~
### Request->host()
>>獲取URL的host參數
~~~
public function host()
{
return $this->server('HTTP_HOST');
}
~~~
### Request->port()
>>獲取URL的port參數
~~~
public function port()
{
return $this->server('SERVER_PORT');
}
~~~
### Request->query()
>>獲取URL的query參數
~~~
public function query()
{
return $this->server('QUERY_STRING');
}
~~~
### Request->ext()
>>獲取URL的訪問后綴(.html或.php)
~~~
public function ext()
{
return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
}
~~~
### Request->root()
>>獲取URL訪問的根地址
~~~
public function root($url = null)
{
if (!is_null($url) && true !== $url) {
$this->root = $url;
return $this;
} elseif (!$this->root) {
$file = $this->baseFile();
if ($file && 0 !== strpos($this->url(), $file)) {
$file = str_replace('\\', '/', dirname($file));
}
$this->root = rtrim($file, '/');
}
return true === $url ? $this->domain() . $this->root : $this->root;
}
~~~
### Request->pathinfo
>>獲取當前URL的pathinfo信息(包含URL后綴)
~~~
public function pathinfo()
{
if (is_null($this->pathinfo)) {
if (isset($_GET[Config::get('var_pathinfo')])) {
// 判斷URL里面是否有兼容模式參數
$_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
unset($_GET[Config::get('var_pathinfo')]);
} elseif (IS_CLI) {
// CLI模式下 index.php module/controller/action/params/...
$_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
}
// 分析PATHINFO信息
if (!isset($_SERVER['PATH_INFO'])) {
foreach (Config::get('pathinfo_fetch') as $type) {
if (!empty($_SERVER[$type])) {
$_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
break;
}
}
}
$this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : ltrim($_SERVER['PATH_INFO'], '/');
}
return $this->pathinfo;
}
~~~
## 2-2 請求相關
### Request->baseFile()
>>獲取當前請求的執行文件
~~~
public function baseFile($file = null)
{
if (!is_null($file) && true !== $file) {
$this->baseFile = $file;
return $this;
} elseif (!$this->baseFile) {
$url = '';
if (!IS_CLI) {
$script_name = basename($_SERVER['SCRIPT_FILENAME']);
if (basename($_SERVER['SCRIPT_NAME']) === $script_name) {
$url = $_SERVER['SCRIPT_NAME'];
} elseif (basename($_SERVER['PHP_SELF']) === $script_name) {
$url = $_SERVER['PHP_SELF'];
} elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name) {
$url = $_SERVER['ORIG_SCRIPT_NAME'];
} elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $script_name)) !== false) {
$url = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $script_name;
} elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
$url = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']));
}
}
$this->baseFile = $url;
}
return true === $file ? $this->domain() . $this->baseFile : $this->baseFile;
}
~~~
### Request->time()
>>獲取當前請求時間
~~~
public function time($float = false)
{
return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
}
~~~
### Request->type()
>>獲取當前請求的資源類型
~~~
public function type()
{
$accept = isset($this->server['HTTP_ACCEPT']) ? $this->server['HTTP_ACCEPT'] : $_SERVER['HTTP_ACCEPT'];
if (empty($accept)) {
return false;
}
foreach ($this->mimeType as $key => $val) {
$array = explode(',', $val);
foreach ($array as $k => $v) {
if (stristr($accept, $v)) {
return $key;
}
}
}
return false;
}
~~~
### Request->method()
>>獲取當前的請求類型(GET/POST/CLI)
~~~
public function method($method = false)
{
if (true === $method) {
// 獲取原始請求類型
return IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
} elseif (!$this->method) {
if (isset($_POST[Config::get('var_method')])) {
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST);
} elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} else {
$this->method = IS_CLI ? 'GET' : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
}
}
return $this->method;
}
~~~
### Request->protocol()
>>獲取當前請求的通信協議
~~~
public function protocol()
{
return $this->server('SERVER_PROTOCOL');
}
~~~
### Request->remotePort()
>>獲取當前請求的遠程端口
~~~
public function remotePort()
{
return $this->server('REMOTE_PORT');
}
~~~
## 2-3 請求信息監測
### Request->isGet()
>>檢查當前是否Get請求
~~~
public function isGet()
{
return $this->method() == 'GET';
}
~~~
### Request->isPost()
>>檢查當前是否Post請求
~~~
public function isPost()
{
return $this->method() == 'POST';
}
~~~
### Request->isPut()
>>檢查是否為Put請求
~~~
public function isPut()
{
return $this->method() == 'PUT';
}
~~~
### Request->isDelete()
>>檢查是否為Delete請求
~~~
public function isDelete()
{
return $this->method() == 'DELETE';
}
~~~
### Request->isHead()
>>檢查是否為Head請求
~~~
public function isHead()
{
return $this->method() == 'HEAD';
}
~~~
### Request->isPatch()
>>檢查是否為Patch請求
~~~
public function isPatch()
{
return $this->method() == 'PATCH';
}
~~~
### Request->isOptions()
>>檢查是否為options請求
~~~
public function isOptions()
{
return $this->method() == 'OPTIONS';
}
~~~
### Request->isCli()
>>檢查是否為Cli請求
~~~
public function isCli()
{
return PHP_SAPI == 'cli';
}
~~~
### Request->isCgi()
>>檢查是否為Cgi請求
~~~
public function isCgi()
{
return strpos(PHP_SAPI, 'cgi') === 0;
}
~~~
### Request->issSsl()
>>檢查是否為ssl請求
~~~
public function isSsl()
{
$server = array_merge($_SERVER, $this->server);
if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
return true;
} elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) {
return true;
} elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) {
return true;
} elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) {
return true;
}
return false;
}
~~~
### Request->isAjax()
>>檢查是否為Ajax請求
~~~
public function isAjax()
{
$value = $this->server('HTTP_X_REQUESTED_WITH');
return (!is_null($value) && strtolower($value) == 'xmlhttprequest') ? true : false;
}
~~~
### Request->isPjax()
>>檢查是否為Pjax請求
~~~
public function isPjax()
{
return !is_null($this->server('HTTP_X_PJAX')) ? true : false;
}
~~~
### Request->isMobile()
>>檢查是否手機訪問
~~~
public function isMobile()
{
if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
return true;
} elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
return true;
} elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
return true;
} elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
return true;
} else {
return false;
}
}
~~~
## 2-4 請求數據過濾
### Request->filter()
>>獲取或設置過濾規則
~~~
public function filter($filter = null)
{
if (is_null($filter)) {
return $this->filter;
} else {
$this->filter = $filter;
}
}
~~~
### Request->filterValue()
>>遞歸過濾給的的值
~~~
private function filterValue(&$value, $key, $filters)
{
$default = array_pop($filters);
foreach ($filters as $filter) {
if (is_callable($filter)) {
// 調用函數或者方法過濾
$value = call_user_func($filter, $value);
} elseif (is_scalar($value)) {
if (strpos($filter, '/')) {
// 正則過濾
if (!preg_match($filter, $value)) {
// 匹配不成功返回默認值
$value = $default;
break;
}
} elseif (!empty($filter)) {
// filter函數不存在時, 則使用filter_var進行過濾
// filter為非整形值時, 調用filter_id取得過濾id
$value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
if (false === $value) {
$value = $default;
break;
}
}
}
}
return $this->filterExp($value);
}
~~~
### Request->filterExp()
>>過濾表單中的表達式
~~~
public function filterExp(&$value)
{
// 過濾查詢特殊字符
if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) {
$value .= ' ';
}
// TODO 其他安全過濾
}
~~~
### Request->typeCast()
>>強制請求數據的類型轉換
~~~
private function typeCast(&$data, $type)
{
switch (strtolower($type)) {
// 數組
case 'a':
$data = (array) $data;
break;
// 數字
case 'd':
$data = (int) $data;
break;
// 浮點
case 'f':
$data = (float) $data;
break;
// 布爾
case 'b':
$data = (boolean) $data;
break;
// 字符串
case 's':
default:
if (is_scalar($data)) {
$data = (string) $data;
} else {
throw new \InvalidArgumentException('variable type error:' . gettype($data));
}
}
}
~~~
## 2-5 設置與獲取同一函數
### Request->input()
>>input()是設置和獲取輸入信息的核心實現函數
>如果傳入的name參數為false,則獲取對應數據源
>如果傳入的name參數非false,則設置name為對應數據源
>input()在下面的多個函數中會被調用
~~~
public function input($data = [], $name = '', $default = null, $filter = null)
{
if (false === $name) {
// 獲取原始數據
return $data;
}
$name = (string) $name;
if ('' != $name) {
// 解析name
if (strpos($name, '/')) {
list($name, $type) = explode('/', $name);
} else {
$type = 's';
}
// 按.拆分成多維數組進行判斷
foreach (explode('.', $name) as $val) {
if (isset($data[$val])) {
$data = $data[$val];
} else {
// 無輸入數據,返回默認值
return $default;
}
}
if (is_object($data)) {
return $data;
}
}
// 解析過濾器
$filter = $filter ?: $this->filter;
if (is_string($filter)) {
$filter = explode(',', $filter);
} else {
$filter = (array) $filter;
}
$filter[] = $default;
if (is_array($data)) {
array_walk_recursive($data, [$this, 'filterValue'], $filter);
reset($data);
} else {
$this->filterValue($data, $name, $filter);
}
if (isset($type) && $data !== $default) {
// 強制類型轉換
$this->typeCast($data, $type);
}
return $data;
}
~~~
### Request->param()
~~~
public function param($name = '', $default = null, $filter = null)
{
if (empty($this->param)) {
$method = $this->method(true);
// 自動獲取請求變量
switch ($method) {
case 'POST':
$vars = $this->post(false);
break;
case 'PUT':
case 'DELETE':
case 'PATCH':
$vars = $this->put(false);
break;
default:
$vars = [];
}
// 當前請求參數和URL地址中的參數合并
$this->param = array_merge($this->get(false), $vars, $this->route(false));
}
if (true === $name) {
// 獲取包含文件上傳信息的數組
$file = $this->file();
$data = array_merge($this->param, $file);
return $this->input($data, '', $default, $filter);
}
return $this->input($this->param, $name, $default, $filter);
}
~~~
### Request->get()
~~~
public function get($name = '', $default = null, $filter = null)
{
if (empty($this->get)) {
$this->get = $_GET;
}
if (is_array($name)) {
$this->param = [];
return $this->get = array_merge($this->get, $name);
}
return $this->input($this->get, $name, $default, $filter);
}
~~~
### Request->post()
~~~
public function post($name = '', $default = null, $filter = null)
{
if (empty($this->post)) {
$this->post = $_POST;
}
if (is_array($name)) {
$this->param = [];
return $this->post = array_merge($this->post, $name);
}
return $this->input($this->post, $name, $default, $filter);
}
~~~
### Request->put()
~~~
public function put($name = '', $default = null, $filter = null)
{
if (is_null($this->put)) {
$content = file_get_contents('php://input');
if (strpos($content, '":')) {
$this->put = json_decode($content, true);
} else {
parse_str($content, $this->put);
}
}
if (is_array($name)) {
$this->param = [];
return $this->put = is_null($this->put) ? $name : array_merge($this->put, $name);
}
return $this->input($this->put, $name, $default, $filter);
}
~~~
### Request->delete()
~~~
public function delete($name = '', $default = null, $filter = null)
{
return $this->put($name, $default, $filter);
}
~~~
### Request->patch()
~~~
public function patch($name = '', $default = null, $filter = null)
{
return $this->put($name, $default, $filter);
}
~~~
### Reques->request()
~~~
public function request($name = '', $default = null, $filter = null)
{
if (empty($this->request)) {
$this->request = $_REQUEST;
}
if (is_array($name)) {
$this->param = [];
return $this->request = array_merge($this->request, $name);
}
return $this->input($this->request, $name, $default, $filter);
}
~~~
### Request->has()
>>檢查是否存在name參數
~~~
public function has($name, $type = 'param', $checkEmpty = false)
{
if (empty($this->$type)) {
$param = $this->$type();
} else {
$param = $this->$type;
}
// 按.拆分成多維數組進行判斷
foreach (explode('.', $name) as $val) {
if (isset($param[$val])) {
$param = $param[$val];
} else {
return false;
}
}
return ($checkEmpty && '' === $param) ? false : true;
}
~~~
### Request->only()
>>只獲取name的參數信息
~~~
public function only($name, $type = 'param')
{
$param = $this->$type();
if (is_string($name)) {
$name = explode(',', $name);
}
$item = [];
foreach ($name as $key) {
if (isset($param[$key])) {
$item[$key] = $param[$key];
}
}
return $item;
}
~~~
### Request->except()
>>排除name參數,獲取其他所有參數信息
~~~
public function except($name, $type = 'param')
{
$param = $this->$type();
if (is_string($name)) {
$name = explode(',', $name);
}
foreach ($name as $key) {
if (isset($param[$key])) {
unset($param[$key]);
}
}
return $param;
}
~~~
### Request->route()
>>獲取設置路由參數
~~~
public function route($name = '', $default = null, $filter = null)
{
if (is_array($name)) {
$this->param = [];
return $this->route = array_merge($this->route, $name);
}
return $this->input($this->route, $name, $default, $filter);
}
~~~
### Request->routeInfo()
>>獲取當前請求的路由信息
~~~
public function routeInfo($route = [])
{
if (!empty($route)) {
$this->routeInfo = $route;
} else {
return $this->routeInfo;
}
}
~~~
### Request->session()
>> 獲取和設置session信息
~~~
public function session($name = '', $default = null, $filter = null)
{
if (empty($this->session)) {
$this->session = Session::get();
}
if (is_array($name)) {
return $this->session = array_merge($this->session, $name);
}
return $this->input($this->session, $name, $default, $filter);
}
~~~
### Request->cookie()
>> 獲取和設置cookie信息
~~~
public function cookie($name = '', $default = null, $filter = null)
{
if (empty($this->cookie)) {
$this->cookie = $_COOKIE;
}
if (is_array($name)) {
return $this->cookie = array_merge($this->cookie, $name);
}
return $this->input($this->cookie, $name, $default, $filter);
}
~~~
### Request->server()
>>設置和獲取server參數信息
~~~
public function server($name = '', $default = null, $filter = null)
{
if (empty($this->server)) {
$this->server = $_SERVER;
}
if (is_array($name)) {
return $this->server = array_merge($this->server, $name);
}
return $this->input($this->server, false === $name ? false : strtoupper($name), $default, $filter);
}
~~~
### Request->getContent()
>>獲取或設置當前的請求內容content
~~~
public function getContent()
{
if (is_null($this->content)) {
$this->content = file_get_contents('php://input');
}
return $this->content;
}
~~~
### Request->file()
>>獲取上傳的文件信息
~~~
public function file($name = '')
{
if (empty($this->file)) {
$this->file = isset($_FILES) ? $_FILES : [];
}
if (is_array($name)) {
return $this->file = array_merge($this->file, $name);
}
$files = $this->file;
if (!empty($files)) {
// 處理上傳文件
$array = [];
foreach ($files as $key => $file) {
if (is_array($file['name'])) {
$item = [];
$keys = array_keys($file);
$count = count($file['name']);
for ($i = 0; $i < $count; $i++) {
if (empty($file['tmp_name'][$i])) {
continue;
}
$temp['key'] = $key;
foreach ($keys as $_key) {
$temp[$_key] = $file[$_key][$i];
}
$item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
}
$array[$key] = $item;
} else {
if ($file instanceof File) {
$array[$key] = $file;
} else {
if (empty($file['tmp_name'])) {
continue;
}
$array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
}
}
}
if (strpos($name, '.')) {
list($name, $sub) = explode('.', $name);
}
if ('' === $name) {
// 獲取全部文件
return $array;
} elseif (isset($sub) && isset($array[$name][$sub])) {
return $array[$name][$sub];
} elseif (isset($array[$name])) {
return $array[$name];
}
}
return null;
}
~~~
### Request->env()
>>獲取或設置當前請求的環境變量信息
~~~
public function env($name = '', $default = null, $filter = null)
{
if (empty($this->env)) {
$this->env = $_ENV;
}
if (is_array($name)) {
return $this->env = array_merge($this->env, $name);
}
return $this->input($this->env, false === $name ? false : strtoupper($name), $default, $filter);
}
~~~
### Request->header()
>>獲取或設置當前請求的header
~~~
public function header($name = '', $default = null)
{
if (empty($this->header)) {
$header = [];
$server = $this->server ?: $_SERVER;
foreach ($server as $key => $val) {
if (0 === strpos($key, 'HTTP_')) {
$key = str_replace('_', '-', strtolower(substr($key, 5)));
$header[$key] = $val;
}
}
if (isset($server['CONTENT_TYPE'])) {
$header['content-type'] = $server['CONTENT_TYPE'];
}
if (isset($server['CONTENT_LENGTH'])) {
$header['content-length'] = $server['CONTENT_LENGTH'];
}
$this->header = array_change_key_case($header);
}
if (is_array($name)) {
return $this->header = array_merge($this->header, $name);
}
if ('' === $name) {
return $this->header;
}
$name = str_replace('_', '-', strtolower($name));
return isset($this->header[$name]) ? $this->header[$name] : $default;
}
~~~
### Request->langset()
>>獲取或設置當前的語言設置
~~~
public function langset($lang = null)
{
if (!is_null($lang)) {
$this->langset = $lang;
return $this;
} else {
return $this->langset ?: '';
}
}
~~~
### Request->dispatch()
>>獲取或設置當前請求調度信息
~~~
public function dispatch($dispatch = null)
{
if (!is_null($dispatch)) {
$this->dispatch = $dispatch;
}
return $this->dispatch;
}
~~~
### Request->module()
>>獲取或設置請求當前模塊名
~~~
public function module($module = null)
{
if (!is_null($module)) {
$this->module = $module;
return $this;
} else {
return $this->module ?: '';
}
}
~~~
### Request->controller()
>>獲取或設置當前請求控制器
~~~
public function controller($controller = null)
{
if (!is_null($controller)) {
$this->controller = $controller;
return $this;
} else {
return $this->controller ?: '';
}
}
~~~
### Request->action()
>>獲取或設置當前請求操作
~~~
public function action($action = null)
{
if (!is_null($action)) {
$this->action = $action;
return $this;
} else {
return $this->action ?: '';
}
}
~~~
# 3 請求創建與設置
## 3-1 網絡請求創建
### Request::instance()
>>單例模式創建全局唯一請求對象
~~~
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
~~~
### Request->__construct()
>>網絡請求構造函數
~~~
public function __construct($options = [])
{
foreach ($options as $name => $item) {
if (property_exists($this, $name)) {
$this->$name = $item;
}
}
if (is_null($this->filter)) {
$this->filter = Config::get('default_filter');
}
}
~~~
### Request::create()
>>網絡請求 自定義創建
~~~
public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
{
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);
$info = parse_url($uri);
if (isset($info['host'])) {
$server['SERVER_NAME'] = $info['host'];
$server['HTTP_HOST'] = $info['host'];
}
if (isset($info['scheme'])) {
if ('https' === $info['scheme']) {
$server['HTTPS'] = 'on';
$server['SERVER_PORT'] = 443;
} else {
unset($server['HTTPS']);
$server['SERVER_PORT'] = 80;
}
}
if (isset($info['port'])) {
$server['SERVER_PORT'] = $info['port'];
$server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
}
if (isset($info['user'])) {
$server['PHP_AUTH_USER'] = $info['user'];
}
if (isset($info['pass'])) {
$server['PHP_AUTH_PW'] = $info['pass'];
}
if (!isset($info['path'])) {
$info['path'] = '/';
}
$options = [];
$queryString = '';
if (isset($info['query'])) {
parse_str(html_entity_decode($info['query']), $query);
if (!empty($params)) {
$params = array_replace($query, $params);
$queryString = http_build_query($query, '', '&');
} else {
$params = $query;
$queryString = $info['query'];
}
} elseif (!empty($params)) {
$queryString = http_build_query($params, '', '&');
}
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
$server['QUERY_STRING'] = $queryString;
$options['cookie'] = $cookie;
$options['param'] = $params;
$options['file'] = $files;
$options['server'] = $server;
$options['url'] = $server['REQUEST_URI'];
$options['baseUrl'] = $info['path'];
$options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
$options['method'] = $server['REQUEST_METHOD'];
$options['domain'] = $info['scheme'] . '://' . $server['HTTP_HOST'];
$options['content'] = $content;
self::$instance = new self($options);
return self::$instance;
}
~~~
## 3-2 網絡請求設置
>>網絡請求設置相關見上面的獲取與設置同一函數部分
- 框架簡介
- 簡介
- 框架目錄
- 根目錄
- 應用目錄
- 核心目錄
- 擴展目錄
- 其他目錄
- 框架流程
- 啟動流程
- 請求流程
- 響應流程
- 框架結構
- 應用組織
- 網絡請求
- 路由組織
- 數據驗證
- 數據模型(M)
- 數據庫連接(Connection)
- 數據庫(Db)
- 查詢構造(Builder)
- 數據庫查詢(Query)
- 模型(Model)
- 模板視圖(V)
- 視圖(View)
- 模板引擎(Think)
- 模板標簽庫(TagLib)
- 控制器(C)
- 網絡響應
- 配置與緩存
- 配置操作
- 緩存操作
- cookie與session
- Cookie操作
- Session操作
- 自動加載
- 鉤子注冊
- 文件上傳
- 分頁控制
- 控制臺
- 自動構建
- 日志異常調試
- 異常處理
- 代碼調試
- 日志記錄
- 框架使用
- 1 環境搭建(Server)
- 2 網絡請求(Request)
- 3 請求路由(Route)
- 4 響應輸出(Response)
- 5 業務處理(Controller)
- 6 數據存取(Model)
- 7 Web界面(View)