guzzle http 请求demo

<?php

use \GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

$client = new Client(['verify' => false]);
//post  Content-Type: multipart/form-data请求
try {
    $url = "";
    $data = [
        'key' => '',
        'secret'=>''
    ];
    $request_data = [
        'form_params' => $data
    ];
   $res =  $client->request('POST', $url, $request_data);
} catch (ClientException $e) {
    return $e->getCode();
}
$res = $res->getBody();
$res = json_encode($res,true);

//get,Content-Type: application/json请求
 $get_data = [
     'headers' => [
         'access-token' => ""
     ]
 ];
$res = $client->request('GET',$url,$get_data);
$body = $res->getBody();
$res = $body->getContents();
$res = json_decode($res, true);

//post上传视频
$video_path = "";
$post_video_data = [
    'multipart' => [
        [
            'name' => 'video',
            'contents' => fopen($video_path, 'r'),
            'headers' => ['Content-Type' => 'video/mp4']
        ],
    ]
];
$res = $client->request('POST',$url,$post_video_data);
$body = $res->getBody();
$res = $body->getContents();
$res = json_decode($res, true);

//post Content-Type: application/json
$create_data = [];
$jsonData = [
    'json' => $create_data,
    'headers' => ['content-type' => 'application/json']
];
$res = $client->request('POST',$url,$jsonData);
$body = $res->getBody();
$res = $body->getContents();
$res = json_decode($res, true);

 

posted @ 2022-08-26 11:44  X__cicada  阅读(157)  评论(0编辑  收藏  举报