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

你可能感兴趣的:(php)