Current Location: Home> Latest Articles> Initialize HTTP requests with curl using init function

Initialize HTTP requests with curl using init function

gitbox 2025-05-19

PHP provides a curl library that can easily send HTTP requests over cURL . curl is a powerful tool that is widely used in HTTP requests, sending form data, crawling web pages and other operations. To simplify these operations, PHP provides some functions, the most important of which is curl_init() . It is used to initialize a cURL session, allowing us to start sending requests.

In this article, we will demonstrate how to use curl_init() to initialize a cURL session and combine curl_setopt() , curl_exec() and other functions to send HTTP requests.

1. Initialize the cURL session

Before sending a request using cURL, we first need to initialize a cURL session. PHP provides the curl_init() function to initialize a new cURL session, which returns a cURL handle that we can use to configure the request options and ultimately execute the request.

 <?php
// Initialize a cURL Session
$ch = curl_init();
?>

At this time, the $ch variable returned by curl_init() is a cURL handle that represents the current cURL session. Next, we can configure some of the requested options through this handle.

2. Set cURL request options

Using the curl_setopt() function, we can set various options for cURL sessions. The most commonly used options are to set the request URL, request method, request header information, etc.

Set URL

To set the requested target URL, you can use the CURLOPT_URL option.

 // Set requested URL
curl_setopt($ch, CURLOPT_URL, "http://gitbox.net/example");

Enable returned content

By default, the curl_exec() function will output the requested result directly to the browser. If we want to get the request result instead of outputting it directly, we can control it through the CURLOPT_RETURNTRANSFER option.

 // Set the return content without direct output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Setting up HTTP request method

cURL supports a variety of HTTP methods, such as GET , POST , PUT , etc. If you want to use the POST method, you can set it through CURLOPT_POST :

 // Set the request method to POST
curl_setopt($ch, CURLOPT_POST, true);

// set up POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'username' => 'testuser',
    'password' => 'testpassword'
]));

Set request header

If you need to set custom HTTP headers, you can use the CURLOPT_HTTPHEADER option:

 // set up自定义的 HTTP head
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Bearer token_value'
]);

3. Execute a cURL request and get a response

After all the options are configured, you can use the curl_exec() function to execute the request and get the response. This function returns the requested result. If no error occurs, the returned result is the response content of the target URL.

 // Execute the request and get the response result
$response = curl_exec($ch);

// Check if the request is successful
if ($response === false) {
    echo "cURL mistake:" . curl_error($ch);
} else {
    echo "Request succeeded,Response content:" . $response;
}

4. Close the cURL session

After the request is completed, the cURL session should be closed to free the resource. Closed cURL sessions is a good habit, especially when handling large numbers of requests, which helps improve system performance.

 // closure cURL Session
curl_close($ch);

5. Complete example

Here is a complete example of how to initialize, set options, send requests, and get response results through PHP.

 <?php
// initialization cURL Session
$ch = curl_init();

// Set requested URL
curl_setopt($ch, CURLOPT_URL, "http://gitbox.net/example");

// Set the return content without direct output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the request method to POST
curl_setopt($ch, CURLOPT_POST, true);

// set up POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
    'username' => 'testuser',
    'password' => 'testpassword'
]));

// set up自定义的 HTTP head
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Bearer token_value'
]);

// Execute the request and get the response result
$response = curl_exec($ch);

// Check if the request is successful
if ($response === false) {
    echo "cURL mistake:" . curl_error($ch);
} else {
    echo "Request succeeded,Response content:" . $response;
}

// closure cURL Session
curl_close($ch);
?>