The biggest benefit of PHP cURL is its ca­pa­bil­i­ty to handle various data transfer needs and seam­less­ly integrate with APIs. In this article, you’ll gain insights into PHP cURL’s syntax and func­tion­al­i­ty.

What is PHP cURL?

PHP cURL, short for “Curl URL Request Library”, is a PHP extension. It offers a way to send HTTP requests and com­mu­ni­cate using different network protocols, including HTTP, HTTPS, and FTP. With cURL, you can adjust and manage headers, pa­ra­me­ters, and data to fulfill API needs.

How to install PHP cURL?

To utilize cURL in PHP, you typically don’t have to install cURL sep­a­rate­ly, as it comes as a built-in extension. However, ensure that the cURL extension is enabled in your PHP setup. You can confirm cURL func­tion­al­i­ty by creating a PHP file and running the following code within it:

phpinfo();
php

On the generated page, look for cURL support or similar in­for­ma­tion to see if PHP cURL is enabled. If cURL is not active, you’ll need to edit the php.ini file. Therein, locate the ex­pres­sion ;extension=php_curl.dll for cURL in Windows or ;extension=curl for cURL in Linux. To activate the cURL extension, remove the semicolon “;” preceding the line. Save the file, and for the changes to take effect, restart the web server.

Learn the most important basics of PHP pro­gram­ming in our PHP tutorial – essential for using cURL. To learn more about the ad­van­tages and dis­ad­van­tages of the PHP pro­gram­ming language, you can check out the com­par­isons between PHP vs. Python and PHP vs. JavaScript in the IONOS Digital Guide.

This is the syntax of PHP cURL

The syntax of PHP cURL consists of various functions and options to configure a cURL session, perform queries and work with the results.

Step 1: ini­tial­ize a cURL session

$curl = curl_init();
php

Step 2: set options

curl_setopt($curl, CURLOPT_URL, 'https://example.com/api'); // defines the URL
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // sets the result of the query as return value
php

Step 3: execute the cURL request

$response = curl_exec($curl);
php

Step 4: closing the cURL session

curl_close($curl);
php
Tip

Deploy Now from IONOS ac­cel­er­ates your de­vel­op­ment cycles and minimizes downtime. Discover flexible de­ploy­ment strate­gies for your web projects with Deploy Now.

What cURL functions exist in PHP?

A variety of PHP functions are available to create HTTP requests or upload files. It’s also possible to use PHP to retrieve in­for­ma­tion from a MySQL database and send it as JSON via cURL.

Here are some of the most important cURL functions in PHP:

  • curl_init: Ini­tial­izes a new cURL session and returns a cURL handle.
  • curl_setopt: Sets options for the cURL session. Pa­ra­me­ters such as the URL, headers, or au­then­ti­ca­tion methods are set here. You can also use PHP cURL to specify POST data to send to the server.
  • curl_exec: Executes the cURL session and returns the response as a string.
  • curl_close: Closes the cURL session and releases the resources.
  • curl_setopt_array: Defines an array of cURL options in a single call.
  • curl_getinfo: Returns in­for­ma­tion about the last cURL session, such as the URL or HTTP status code.
  • curl_error: Returns the error message from the last cURL request.
  • curl_errno: Returns the error code of the last cURL request.
  • curl_multi_init: Ini­tial­izes a multi-cURL handle that allows you to make multiple cURL requests at the same time.
  • curl_multi_add_handle: Adds a cURL session to a multi-cURL handle.
  • curl_multi_exec: Executes the multi-cURL requests.
  • curl_multi_get­con­tent: Returns the content of the response for a given cURL session in the multi-cURL handle.
IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­men­ta­tion

An example of the use of cURL in PHP

By creating your own PHP classes, you can make the code more modular, define reusable methods, and simplify the im­ple­men­ta­tion of PHP cURL in your ap­pli­ca­tions.

Here’s an example of what a PHP class might look like in con­junc­tion with PHP cURL GET:

class MyCurlClient {
    private $curl;
    public function __construct() {
        $this->curl = curl_init();
        // Further configurations can be made here
    }
    public function sendRequest($url) {
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($this->curl);
        return $response;
    }
    public function close() {
        curl_close($this->curl);
    }
}
// Using your own class
$myCurl = new MyCurlClient();
$response = $myCurl->sendRequest('https://example.com/api');
echo $response;
$data = json_decode($response, true);
// Output of the data with PHP operators
echo "Post ID: " . $data['id'] . "<br>";
echo "Title: " . $data['title'] . "<br>";
echo "Body: " . $data['body'] . "<br>";
$myCurl->close();
php

In this example, we created the My­Curl­Client class that handles a PHP cURL session in its con­struc­tor. The sendRequest() method takes a URL, con­fig­ures the cURL options, and executes the HTTP GET request. The output strings are con­cate­nat­ed using PHP operators. Finally, we use the close() function to end the cURL session.

IONOS Cloud Object Storage
Secure, af­ford­able storage

Cost-effective, scalable storage that in­te­grates into your ap­pli­ca­tion scenarios. Protect your data with highly secure servers and in­di­vid­ual access control.

Go to Main Menu