I need to use basic token auth to API, and I want to create service to do that (I'll be using these features in other controllers), so I created service called smsapi
SMSApiService.php
<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class SMSApiService
{
private $httpClient;
private $apiHost;
private $apiToken;
public function __construct(HttpClientInterface $httpClient, $apiToken, $apiHost)
{
$this->apiHost = $apiHost;
$this->apiToken = $apiToken;
$this->httpClient = $httpClient::Create([
'auth_bearer' => $this->apiToken]);
}
public function sendSMS()
{
$response = $this->httpClient->request('GET', $this->apiHost);
$statusCode = $response->getContent();
return $statusCode;
}
}
Due to symfony docs: https://symfony.com/doc/current/components/http_client.html#authentication
I always get the error:
Attempted to call an undefined method named "Create" of class "Symfony\Component\HttpClient\CurlHttpClient".
What should I do, to use create method and enter API credentials? (API token). apiHost
and apiToken
are provided by services.yaml
.