tp6框架中Http类 请求的header、body参数传参 及post、file格式

引入Http类:

在需要使用的地方引入Http类:

use think\facade\Http;

GET请求示例:$response = Http::get('https://example.com/api/resource'); 

设置Header参数:

$headers = [ 'Authorization' => 'Bearer YourAccessToken', 'Content-Type' => 'application/json', ];

$response = Http::header($headers)->get('https://example.com/api/resource');

POST请求示例(带Body参数):

$data = [
    'key'   => 'value',
    'param' => 'another value',
];

$response = Http::post('https://example.com/api/post-endpoint', $data);

POST请求示例(JSON格式):

$jsonData = [ 'key' => 'value', 'param' => 'another value', ];

$response = Http::contentType('application/json')->post('https://example.com/api/post-endpoint', $jsonData);

dump($response->getBody());

上传文件示例:

$file = request()->file('image'); // Assuming 'image' is the name of the file input

$response = Http::attach('file', $file)->post('https://example.com/api/upload');

以上示例是基于ThinkPHP 6框架中的Http类进行的,确保你已经在项目中引入了该类。 

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-19 12:28:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-19 12:28:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-19 12:28:04       82 阅读
  4. Python语言-面向对象

    2024-01-19 12:28:04       91 阅读

热门阅读

  1. spring中的事务及底层原理

    2024-01-19 12:28:04       68 阅读
  2. 高质量的前端代码

    2024-01-19 12:28:04       46 阅读
  3. flutter qr_flutter二维码库填充不满问题解决方案

    2024-01-19 12:28:04       61 阅读
  4. python 小说下载

    2024-01-19 12:28:04       51 阅读
  5. 绿色科技投资:可持续发展的财经新趋势

    2024-01-19 12:28:04       56 阅读
  6. Qt实现在5种情况下快速求最值

    2024-01-19 12:28:04       55 阅读