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

本文介绍了如何在ThinkPHP6框架中使用Http类进行GET、POST请求,包括设置Header参数、JSON格式数据发送以及文件上传的示例,展示了在实际开发中处理API接口的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入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类进行的,确保你已经在项目中引入了该类。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一分半心动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值