当前位置:   article > 正文

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

fast\http post thinkphp header

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/322755
推荐阅读
相关标签
  

闽ICP备14008679号