Introducing Async-Curl: A Fire-and-Forget PHP Library for Asynchronous HTTP Requests

Avinash Pratap Singh
3 min readAug 6, 2024

--

Introduction

In modern web development, efficiency and responsiveness are critical. Traditional PHP applications often handle HTTP requests synchronously, meaning each request must complete before the next one starts. This can lead to performance bottlenecks, especially when dealing with multiple or slow external APIs.

To tackle this challenge, we introduce Async-Curl, a PHP library designed to execute HTTP requests asynchronously. With Async-Curl, you can “fire and forget” your HTTP requests, allowing your application to continue running while the requests are processed in the background.

The Problem with Synchronous Requests

Handling HTTP requests synchronously can be a significant limitation. Every time your application makes an API call, it must wait for the response before proceeding. This can result in slower response times and an inefficient use of resources, particularly when multiple requests need to be made.

What is Async-Curl?

Async-Curl leverages the power of PHP’s exec function and cURL to perform asynchronous HTTP requests. By executing requests in the background, it frees up your application to continue processing other tasks, thus enhancing performance and responsiveness.

Core Features

  • Fire-and-Forget Requests: Send HTTP requests and immediately move on to the next task, without waiting for a response.
  • Support for Multiple HTTP Methods: Handle various methods like GET, POST, PUT, DELETE, and PATCH.
  • Custom Headers and Data Formats: Easily set custom headers and send data in JSON or form-urlencoded formats.
  • Advanced Request Options: Configure options like following redirects, timeout settings, and SSL verification.
  • Error Logging: Comprehensive error logging for troubleshooting failed requests.

Installation and Setup

Prerequisites

  • PHP 7.1 or higher
  • cURL extension enabled
  • The exec function must not be disabled in your php.ini configuration

Installation

  1. Clone the Repository:
    git clone https://github.com/hackersdad/Async-Curl
  2. Setup Logging:
    Create a logs.txt file in the async-curl directory for logging errors if it doesn't exist. Ensure the PHP user has read and write permissions for this file.
  3. Platform Compatibility:
    Note that Async-Curl is designed for Linux servers and is not compatible with Windows environments.

Using Async-Curl in Your PHP Project

Here’s how to use the async_curl function in your project:

require 'async_curl.php';

$url = 'https://api.example.com/endpoint';
$data = ['key' => 'value'];
$headers = ['Authorization: Bearer your_token'];
$options = [
'follow_redirects' => true,
'timeout' => 30,
'ssl_verify' => true
];

$result = async_curl($url, 'POST', $data, $headers, $options);

if ($result === true) {
echo "Curl request initiated asynchronously.";
} else {
echo "Failed to initiate curl request. Check logs for details.";
}

Supported Features

Asynchronous Curl Requests

Async-Curl executes HTTP requests in the background, using PHP’s exec function to run async-curl-core.php. This allows your application to continue processing other tasks while the request is handled asynchronously.

HTTP Methods

The library supports various HTTP methods, including GET, POST, PUT, DELETE, and PATCH, giving you the flexibility to interact with APIs and other web services.

Request Data Formats and Custom Headers

Async-Curl can send data in both JSON and form-urlencoded formats, automatically setting the appropriate headers. You can also include custom HTTP headers as needed for authentication or other purposes.

Request Options

You can configure options like following redirects, setting timeout durations, and enabling SSL verification to suit your specific requirements.

Error Handling

Errors are logged in the logs.txt file, providing detailed information for troubleshooting. Common issues, such as network errors or invalid URLs, are clearly documented to help you diagnose and resolve problems quickly.

Troubleshooting

Common Issues and Solutions

  • Empty or Missing logs.txt File: Ensure the PHP user has write permissions for the async-curl directory. Create the file manually if necessary and grant appropriate permissions.
  • Invalid JSON String: If you encounter errors related to invalid JSON strings, double-check that the JSON data passed to async-curl-core.php is correctly formatted.
  • Curl Request Failed: Check the logs.txt file for detailed error messages. Issues may stem from network problems, incorrect URLs, or server-side errors.

Conclusion

Async-Curl is a powerful tool for PHP developers looking to improve their application’s performance through asynchronous HTTP requests. Its “fire-and-forget” mechanism allows you to initiate requests without waiting for them to complete, making it ideal for tasks like web scraping, API integration, and more.

Try out Async-Curl today and experience the benefits of asynchronous processing in your PHP applications. We welcome your feedback and contributions to the project!

Additional Resources

--

--

Avinash Pratap Singh
Avinash Pratap Singh

Written by Avinash Pratap Singh

Backend developer and Cybersecurity Enthusiast with expertise in technologies like Python, PHP, Django, DRF, Codeignitor, MySQL.

No responses yet