actinoids/api-suite-bundle
Symfony2 bundle providing a unified API client/service layer with a cURL-based implementation and built-in OAuth 1a authentication support, aimed at simplifying integration with external HTTP APIs in Symfony projects.
Installation:
composer require actinoids/api-suite-bundle
Add to config/bundles.php:
return [
// ...
Actinoids\ApiSuiteBundle\ActinoidsApiSuiteBundle::class => ['all' => true],
];
Configuration:
Define API endpoints in config/packages/actinoids_api_suite.yaml:
actinoids_api_suite:
services:
my_api:
url: 'https://api.example.com'
auth: 'oauth1a'
credentials:
consumer_key: 'your_key'
consumer_secret: 'your_secret'
First Use Case: Fetch data from an authenticated API in a controller:
use Actinoids\ApiSuiteBundle\Service\ApiService;
class MyController extends AbstractController
{
public function index(ApiService $apiService)
{
$response = $apiService->get('my_api', '/endpoint');
return $this->json($response->getData());
}
}
Service Integration:
ApiService into controllers/services to interact with APIs.oauth1a credentials).Request Handling:
$apiService->get('service_name', '/path', ['param' => 'value']);
$apiService->post('service_name', '/path', ['data' => 'payload']);
$apiService->setHeaders('service_name', ['X-Custom-Header' => 'value']);
OAuth1a Authentication:
oauth1a signature.Response Processing:
$response = $apiService->get('service_name', '/path');
$rawBody = $response->getBody();
$data = $response->getData(); // Auto-decodes JSON
Custom Curl Options:
actinoids_api_suite:
services:
my_api:
curl_options:
CURLOPT_TIMEOUT: 30
CURLOPT_SSL_VERIFYPEER: false
Event Listeners:
api.request and api.response events:
// src/EventListener/MyApiListener.php
public function onApiRequest(ApiRequestEvent $event) {
$event->setOption('CURLOPT_HTTPHEADER', ['X-My-Header: value']);
}
Service Factories:
$apiService->createService('dynamic_name', [
'url' => 'https://dynamic.example.com',
'auth' => 'oauth1a',
// ...
]);
Deprecated Symfony2:
guzzlehttp/guzzle.OAuth1a Quirks:
$apiService->setOption('service_name', CURLOPT_TIMEOUT, 10); // Ensure time sync
Response Handling:
getData() assumes JSON. For XML/other formats, use getBody() and parse manually.$response->isSuccess() before accessing data:
if (!$response->isSuccess()) {
throw new \RuntimeException($response->getError());
}
Configuration Overrides:
config/packages/. Use environment variables for sensitive data:
# config/packages/actinoids_api_suite.yaml
credentials:
consumer_secret: "%env(API_SECRET)%"
Enable Curl Debugging:
actinoids_api_suite:
debug: true
curl_options:
CURLOPT_VERBOSE: true
Logs appear in Symfony’s dev.log.
Validate OAuth Signatures:
curl --oauth_consumer_key KEY --oauth_consumer_secret SECRET ...
Common Errors:
CURLOPT_TIMEOUT or verify endpoint URLs.curl_options:
CURLOPT_SSL_VERIFYPEER: false
Custom Auth Methods:
Actinoids\ApiSuiteBundle\Service\Auth\AuthInterface for new auth schemes (e.g., OAuth2):
class MyAuth implements AuthInterface {
public function signRequest(Request $request) { ... }
}
services:
my_auth:
class: App\Service\MyAuth
tags: ['actinoids_api.auth']
Response Transformers:
Actinoids\ApiSuiteBundle\Response\Response to customize parsing:
$response->setTransformer(function ($body) {
return json_decode($body, true)['custom_key'];
});
Middleware:
// src/EventSubscriber/MyMiddleware.php
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(GetResponseEvent $event) {
if ($event->isMasterRequest()) {
$event->getRequest()->headers->set('X-My-Middleware', 'value');
}
}
How can I help you explore Laravel packages today?