PHP code example of tony-sol / symfony-api-client
1. Go to this page and download the library: Download tony-sol/symfony-api-client library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tony-sol / symfony-api-client example snippets
private \ApiClient\Service\ApiClientInterface $apiClient;
public function __construct(\ApiClient\Service\ApiClientInterface $apiClient)
{
$this->apiClient = $apiClient;
}
public function sendGet()
{
// curl --location --request GET 'http://example.org/endpoint?some=data'
$response = $this->apiClient
->makeGetClient('http://example.org/endpoint')
->withResponseAs(Example\EndpoindDTO::class)
->withRequest(['some' => 'data',])
->send();
/** @var int $responseCode */
$responseCode = $response->getCode();
/** @var array<string, string> $responseHeaders */
$responseHeaders = $response->getHeaders();
/** @var Example\EndpoindDTO $responseDTO */
$responseDTO = $response->getResponse();
return;
}
public function sendPost()
{
// curl --location --request POST 'http://example.org/endpoint' --header 'Content-Type: application/json' --data-raw '{"some": "data"}'
$response = $this->apiClient
->makePostClient('http://example.org/endpoint')
->withResponseAs(Example\EndpoindDTO::class)
->withRequest(['some' => 'data',])
->send();
/** @var int $responseCode */
$responseCode = $response->getCode();
/** @var array<string, string> $responseHeaders */
$responseHeaders = $response->getHeaders();
/** @var Example\EndpoindDTO $responseDTO */
$responseDTO = $response->getResponse();
return;
}