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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章