# 请求类

KaliPHP 即是框架，也是类库，所以在执行框架注册`kali::registry()`后，`req.php`就可以被调用了，以下是几个常用操作

## 请求参数

以请求[`https://www.kaliphp.com/?ct=test&ac=demo&id=10`](http://www.kaliphp.com/?ct=test\&ac=demo\&id=10)为例

```
// 获取id参数，GET、POST、PUT、DELETE、PATCH 都可以用这个获取
req::item('id');

// 如果id参数不存在则设置为0
req::item('id', 0);

// 强制id参数转化为整型
req::item('id', 0, 'int');
```

### GET请求参数

```
req::get('id', 0, 'int');

// 所有 GET 参数
print_r(req::$gets);
```

### POST请求参数

```
req::post('id', 0, 'int');

// 所有 POST 参数
print_r(req::$posts);
```

### PUT请求参数

```
req::put('id', 0, 'int');

// 所有 PUT 参数
print_r(req::$puts);
```

### DELETE请求参数

```
req::delete('id', 0, 'int');

// 所有 DELETE 参数
print_r(req::$deletes);
```

### PATCH请求参数

```
req::patch('id', 0, 'int');

// 所有 PATCH 参数
print_r(req::$patchs);
```

### COOKIE请求参数

```
req::cookie('id', 0, 'int');

// 所有 COOKIE 参数
print_r(req::$cookies);
```

### SESSION请求参数

```
req::session('id', 0, 'int');

// 所有 SESSION 参数
print_r(req::$sessions);
```

### SERVER请求参数

```
// 获得token，通常token不应放在 Query String，而是在 header
req::server('HTTP_TOKEN');
req::server('HTTP_ACCEPT', 'application/json');
```

### 获取HTTP Method名

```
// 返回 GET、POST、PUT、DELETE、CLI
req::method();
```

### 返回JSON编码内容

```
// 客户端设置 HTTP_ACCEPT，要求服务端返回JSON编码内容
req::is_json();
```

### 获得请求路径

```
// 返回 /test/demo
req::path();
```

### 获得请求参数

```
// 返回 ct=test&ac=demo&id=10
req::query_string();
```

### 获得网址，不含 QueryString

```
// 返回 https://www.kaliphp.com
req::url();
```

### 获得完整网址，包含 QueryString

```
// 返回 https://www.kaliphp.com/?ct=test&ac=demo&id=10
req::full_url();
```

### 获取来源网址 （上一个页面地址）

```
req::referrer();
```

### 获取浏览器UA

```
req::user_agent();
```

### 获取浏览器语言

```
req::language();
```

### 获取用户IP

```
req::ip();
```

### 获取用户国家代码

```
// 返回 HK
req::country();
```

### 设置跳转页

```
req::set_redirect('?ct=category&ac=list&cate_id=10')
```

### 获取最后一次设置的跳转页

{% hint style="warning" %}
通常用于列表页通过搜索，然后进入详情页，再编辑，这个时候上一页已经不是搜索后的列表页了，要想编辑后直接返回，就只能通过设置跳转页来实现
{% endhint %}

```
// 不存在设置的跳转页则直接返回上一页
req::redirect(req::referer());
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.kaliphp.com/request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
