composer require anchovy/curl-bundle
config/bundles.php:
return [
// ...
Iman\AnchovyCURLBundle\ImanAnchovyCURLBundle::class => ['all' => true],
];
config/packages/anchovy_curl.yaml:
anchovy_curl:
timeout: 30
connect_timeout: 10
default_headers: { 'User-Agent': 'MyApp/1.0' }
CurlService into a controller or service:
use Iman\AnchovyCURLBundle\Service\CurlService;
class MyController extends AbstractController
{
public function __construct(private CurlService $curlService) {}
public function fetchData()
{
$response = $this->curlService->get('https://api.example.com/data');
return $this->json($response);
}
}
Basic HTTP Requests:
$response = $curlService->get('https://api.example.com/endpoint');
$response = $curlService->post('https://api.example.com/endpoint', ['key' => 'value']);
$response = $curlService->put('https://api.example.com/endpoint', ['data' => 'payload']);
Custom Headers/Options:
$curlService->setHeaders(['Authorization' => 'Bearer token']);
$curlService->setOptions(['CURLOPT_FOLLOWLOCATION' => true]);
Handling Responses:
$response = $curlService->execute();
$body = $response->getBody(); // Raw response
$json = $response->getJson(); // Parsed JSON
$status = $response->getStatusCode();
Async Requests (if supported):
$curlService->async()->get('https://api.example.com/endpoint');
CurlService over instantiating it directly.Validator to validate API responses post-cURL.CurlService for specific APIs.post() and validate payloads.get() with custom headers/user-agents for scraping.Symfony 2.x Legacy:
guzzlehttp/guzzle) for newer projects.Response Handling:
getBody() returns raw response; always check getStatusCode() before parsing.if ($response->getStatusCode() === 200 && $response->isJson()) {
$data = $response->getJson();
}
Timeouts:
anchovy_curl:
timeout: 60
SSL Issues:
$curlService->setOptions(['CURLOPT_SSL_VERIFYPEER' => false]);
CURLOPT_CAINFO.Memory Leaks:
curl_close() implicitly via execute() or get(). Avoid manual curl_* calls.$curlService->setOptions(['CURLOPT_VERBOSE' => true]);
$this->logger->debug('API Response', ['body' => $response->getBody()]);
CURLOPT_HEADER to inspect raw headers:
$curlService->setOptions(['CURLOPT_HEADER' => true]);
Custom Response Classes:
Extend Iman\AnchovyCURLBundle\Response\Response to add domain-specific methods:
class ApiResponse extends Response
{
public function getData(): array
{
return $this->getJson()['data'] ?? [];
}
}
Middleware:
Decorate CurlService to add pre/post-processing:
class AuthMiddlewareCurlService extends CurlService
{
public function __construct(CurlService $decoratedService)
{
$this->decoratedService = $decoratedService;
}
public function get($url)
{
$this->decoratedService->setHeaders(['Authorization' => $this->getToken()]);
return $this->decoratedService->get($url);
}
}
Event Listeners: Dispatch events before/after requests (e.g., logging, metrics):
$dispatcher->addListener('curl.request', function (CurlEvent $event) {
$this->logger->info('Request to ' . $event->getUrl());
});
anchovy_curl:
default_headers:
'X-Custom-Header': 'value'
.env:
ANCHOVY_CURL_TIMEOUT=45
How can I help you explore Laravel packages today?