1. Go to this page and download the library: Download mrizwan/laravel-fcgi-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/ */
// Add HTTP headers
FCGI::withHeaders([
'Authorization' => 'Bearer your-token',
'Accept-Language' => 'en-US'
]);
// Add a single header
FCGI::withHeader('X-API-Key', 'secret-key');
// Add query parameters (for GET requests or as method parameter)
FCGI::withQuery([
'page' => 1,
'limit' => 20,
'sort' => 'created_at'
]);
// Add request payload (for POST, PUT, PATCH requests)
FCGI::withPayload([
'name' => 'Product Name',
'description' => 'Product description'
]);
// Set raw body content with content type
FCGI::withBody(
'{"custom":"json structure"}',
'application/json'
);
// Set URL parameters for template substitution
FCGI::withUrlParameters([
'userId' => 123,
'postId' => 456,
'user' => ['id' => 789] // Supports nested parameters
]);
// Add custom server parameters that will be available in $_SERVER
FCGI::withServerParams([
'HTTP_X_CUSTOM_HEADER' => 'Value',
'SERVER_NAME' => 'custom-server'
]);
// Add custom FastCGI variables
FCGI::withCustomVars([
'CUSTOM_VAR' => 'custom_value'
]);
// Set connection timeouts (in seconds)
FCGI::timeout(30); // Read timeout
FCGI::connectTimeout(5); // Connect timeout
// Add retry logic
FCGI::retry(
3, // Number of retries
500, // Delay between retries in milliseconds
function ($exception) { // Optional callback to determine whether to retry
return !($exception instanceof AuthenticationException);
}
);
// Configure request to send JSON payload
FCGI::asJson()
->post('service.com/doSomething', ['key' => 'value']);
// Configure request to send form-encoded data
FCGI::asForm()
->post('service.com/doSomethingElse', ['key' => 'value']);
// Set Accept header to JSON
FCGI::acceptJson();
// Set Accept header to specific content type
FCGI::accept('application/xml');
// Set custom content type
FCGI::contentType('application/vnd.api+json');
// POST requests default to form-encoded data
FCGI::post('service.com', ['key' => 'value']);
// Content-Type: application/x-www-form-urlencoded
// PUT/PATCH requests default to JSON
FCGI::put('service.com', ['key' => 'value']);
// Content-Type: application/json
// Override defaults with explicit configuration
FCGI::asJson()->post('service.com', ['key' => 'value']);
// Content-Type: application/json
FCGI::asForm()->put('service.com', ['key' => 'value']);
// Content-Type: application/x-www-form-urlencoded
// Using try-catch blocks
try {
$response = FCGI::get('service.internal');
return $response->json();
} catch (\Rizwan\LaravelFcgiClient\Exceptions\ConnectionException $e) {
// Handle connection errors (e.g., service unreachable)
Log::error('FastCGI connection failed: ' . $e->getMessage());
return response()->json(['error' => 'Service unavailable'], 503);
} catch (\Rizwan\LaravelFcgiClient\Exceptions\TimeoutException $e) {
// Handle timeout errors
Log::error('FastCGI request timed out: ' . $e->getMessage());
return response()->json(['error' => 'Request timed out'], 504);
} catch (\Rizwan\LaravelFcgiClient\Exceptions\FastCGIException $e) {
// Handle all other FastCGI-related errors
Log::error('FastCGI error: ' . $e->getMessage());
return response()->json(['error' => 'Internal server error'], 500);
}
// Using the throw method and Laravel's error handling
$posts = FCGI::get('blog-service.internal', '/var/www/blog/public/index.php')
->throw() // This will throw an exception if the request fails
->json();
// Using conditional throws
$response = FCGI::get('service.internal');
$response->throwIf(
$response->getStatusCode() === 429,
fn() => new RateLimitException('Too many requests')
);
// Using throwUnless
$response->throwUnless(
$response->getStatusCode() === 200,
fn() => new ServiceException('Expected 200 OK response')
);
// Basic retry (3 attempts with 500ms delay)
$response = FCGI::retry(3, 500)
->get('flaky-service.internal');
// By default, retries happen on:
// - Connection failures and timeouts (always)
// - Server errors (5xx status codes)
// - NOT on client errors (4xx status codes)
// Custom retry logic based on the exception or response
$response = FCGI::retry(3, 1000, function ($exceptionOrResponse, $request) {
// Only retry for connection and timeout issues
if ($exceptionOrResponse instanceof \Throwable) {
return $exceptionOrResponse instanceof ConnectionException ||
$exceptionOrResponse instanceof TimeoutException;
}
// For responses, retry on server errors but not client errors
if ($exceptionOrResponse instanceof Response) {
return $exceptionOrResponse->serverError();
}
return false;
})
->withToken($token)
->get('api-service.internal');
// Check retry attempts
$response = FCGI::retry(3)->get('service.com');
echo "Request took {$response->getAttempts()} attempts";
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.