Using yiisoft/yii2-httpclient

Keywords: PHP JSON

yiisoft/yii2-httpclient is an extension of Yii2's HTTP client, dedicated to calling remote interfaces.

First: Install yiisoft/yii2-httpclient

composer require yiisoft/yii2-httpclient

II: Use

1:get request

get requests I summarized the following three ways to use them

(1) Using get method, the parameters are directly connected to the interface address.

//Interface address: https://api.wj0511.com/v1/literary/detail? Token=XXXXXX&id=351
$data = 'token=XXXXXX&id=351';//get parametric
$client = new Client([
    'baseUrl' => 'https://The entry address of api.wj0511.com'//interface
]);
$response = $client
    ->get('/v1/literary/detail?' . $data)
    ->send();
if (!$response->isOk) {
    echo 'Interface request error';
}
//Interface return information
var_dump($response->data);

(2): Using the get method, the passed parameters are placed on the second parameter of the get method.

//Interface address: https://api.wj0511.com/v1/literary/detail? Token=XXXXXX&id=351
//get parameter
$data = [
    'token' => 'XXXXXX',
    'id' => '351',
];
$client = new Client([
    'baseUrl' => 'https://The entry address of api.wj0511.com'//interface
]);
$response = $client
    ->get('/v1/literary/detail', $data)
    ->send();
if (!$response->isOk) {
    echo 'Interface request error';
}
//Interface return information
var_dump($response->data);

(3): get parameterization using setUrl

//Interface address: https://api.wj0511.com/v1/literary/detail? Token=XXXXXX&id=351
//Interface Address and get Reference
$url = 'https://api.wj0511.com/v1/literary/detail?token=XXXXXX&id=351';
$client = new Client();
$response = $client->createRequest()
    ->setMethod('GET')  //Set to get request
    ->setUrl($url)  //Interface Address
    ->send();
if (!$response->isOk) {
    echo 'Interface request error';
}
//Interface return information
var_dump($response->data);

2:post request

post requests I summarized two ways of requesting

(1) Use the post method

//post reference
$data = [
    'user_name' => 'test',
    'password' => 'test',
];
$data = json_encode($data);
$client = new Client([
    'baseUrl' => 'https://api.wj0511.com'
]);
$response = $client
    ->post(
        //Setting Interface Address
        '/v1/login/login',
        //Setting up post reference
        $data,
        //Setting header information
        [
            'Content-Type'=>'application/json'
        ]
    )
    ->send();
if (!$response->isOk) {
    echo 'Interface request error';
}
//Interface return information
var_dump($response->data);

(2) post parametric implementation using setUrl

//Interface Address
$url = 'https://api.wj0511.com/v1/login/login';
//post reference
$data = [
    'user_name' => 'test',
    'password' => 'test',
];
$client = new Client();
$response = $client->createRequest()
    ->setMethod('POST') // Request mode
    ->setUrl($url)      // Request address
    ->setData($data)    //Data transfer array
    ->setHeaders(['Content-Type'=>'application/json']) //header
    ->setFormat(Client::FORMAT_JSON) //Format of submitted data
    ->send();
if (!$response->isOk) {
    echo 'Interface request error';
}
//Interface return information
var_dump($response->data);

Posted by edwardoka on Tue, 30 Jul 2019 22:10:02 -0700