composer require anchovy/curl-bundle
config/bundles.php:
return [
// ...
Iman\AnchovyCURLBundle\ImanAnchovyCURLBundle::class => ['all' => true],
];
use Iman\AnchovyCURLBundle\Service\CurlService;
public function __construct(private CurlService $curlService) {}
// In a controller or service
$response = $this->curlService->get('https://api.example.com/data');
$body = $response->getBody();
$status = $response->getStatusCode();
Request/Response Pattern:
// GET
$response = $this->curlService->get($url, $options);
// POST
$response = $this->curlService->post($url, $data, $headers);
// PUT/DELETE/PATCH
$response = $this->curlService->put($url, $data);
Custom Headers & Authentication:
$headers = [
'Authorization' => 'Bearer token123',
'X-Custom-Header' => 'value'
];
$response = $this->curlService->get($url, ['headers' => $headers]);
Handling Responses:
$response = $this->curlService->get($url);
$json = $response->getJson(); // Auto-decodes JSON
$status = $response->getStatusCode();
$headers = $response->getHeaders();
CurlService over instantiating it directly.config/packages/anchovy_curl.yaml:
anchovy_curl:
default_options:
timeout: 30
ssl_verifypeer: false
CurlResponse class to add custom logic:
// src/Service/CustomCurlResponse.php
class CustomCurlResponse extends \Iman\AnchovyCURLBundle\Response\CurlResponse {
public function getCustomData() { ... }
}
Then bind it in services.yaml:
services:
Iman\AnchovyCURLBundle\Response\CurlResponse: '@App\Service\CustomCurlResponse'
guzzlehttp/guzzle or symfony/http-client.try {
$response = $this->curlService->get($url);
} catch (\Iman\AnchovyCURLBundle\Exception\CurlException $e) {
// Handle error (e.g., log, retry, or return fallback)
}
ssl_verifypeer: false) is insecure. Use it only for testing or trusted internal APIs.CURLOPT_VERBOSE via options:
$response = $this->curlService->get($url, [
'options' => [
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => fopen('php://temp', 'w+')
]
]);
$response = $this->curlService->get($url);
if ($response->getError()) {
$error = $response->getError();
// Log or handle $error
}
Custom Request Factories: Override the default CurlRequestFactory to modify request behavior:
// src/Service/CustomRequestFactory.php
class CustomRequestFactory extends \Iman\AnchovyCURLBundle\Factory\CurlRequestFactory {
public function createRequest($method, $url, array $options = []) {
// Add custom logic (e.g., default headers)
return parent::createRequest($method, $url, $options);
}
}
Bind it in services.yaml:
services:
Iman\AnchovyCURLBundle\Factory\CurlRequestFactory: '@App\Service\CustomRequestFactory'
Response Transformers: Extend CurlResponse to add custom parsing logic (e.g., XML, custom APIs):
class CustomResponse extends \Iman\AnchovyCURLBundle\Response\CurlResponse {
public function getParsedData() {
return json_decode($this->getBody(), true);
}
}
CURLOPT_FORBID_REUSE and CURLOPT_FRESH_CONNECT in default options.CURLOPT_SHARE to share connections across requests (advanced use case).How can I help you explore Laravel packages today?