Запросы к REST API на PHP
Введение | |
GET | |
POST | |
PUT | |
DELETE | |
Похожие статьи |
Введение
Нужно установить guzzle
composer require guzzlehttp/guzzle
GET
<?php require_once('vendor/autoload.php'); $client = new \GuzzleHttp\Client(); $response = $client->request('GET', 'https://example.com/vat/api/v1/status', [ 'headers' => [ 'accept' => 'application/json', ], ]); echo $response->getBody();
POST
<?php require_once('vendor/autoload.php'); $client = new \GuzzleHttp\Client(); $response = $client->request('POST', 'https://example.com/vault/api/v1/secrets', [ 'body' => '{"name":"name"}', 'headers' => [ 'accept' => 'application/json', 'content-type' => 'application/json', ], ]); echo $response->getBody();
PUT
<?php require_once('vendor/autoload.php'); $client = new \GuzzleHttp\Client(); $response = $client->request('PUT', 'https://example.com/vault/api/v1/secrets/name', [ 'headers' => [ 'Authorization' => 'Bearer a-proper-token-goes-here', 'accept' => 'application/json', 'content-type' => 'application/json', ], ]); echo $response->getBody();
DELETE
<?php require_once('vendor/autoload.php'); $client = new \GuzzleHttp\Client(); $response = $client->request('DELETE', 'https://example.com/role-store/api/v1/roles/r_id', [ 'headers' => [ 'Authorization' => 'Bearer a-proper-token-goes-here', 'accept' => 'application/json', ], ]); echo $response->getBody();
Пример обработки ответа API с помощью json_decode() можете изучить здесь