The biggest benefit of PHP cURL is its cap­ab­il­ity to handle various data transfer needs and seam­lessly integrate with APIs. In this article, you’ll gain insights into PHP cURL’s syntax and func­tion­al­ity.

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­nic­ate using different network protocols, including HTTP, HTTPS, and FTP. With cURL, you can adjust and manage headers, para­met­ers, and data to fulfill API needs.

How to install PHP cURL?

To utilise cURL in PHP, you typically don’t have to install cURL sep­ar­ately, 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­ity 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­form­a­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­vant­ages and dis­ad­vant­ages of the PHP pro­gram­ming language, you can check out the com­par­is­ons between PHP vs Python and PHP vs JavaS­cript 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­ise 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 minimises downtime. Discover flexible de­ploy­ment strategies 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­form­a­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­ises a new cURL session and returns a cURL handle.
  • curl_setopt: Sets options for the cURL session. Para­met­ers such as the URL, headers, or au­then­tic­a­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­form­a­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­ises 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­ment­a­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­ment­a­tion of PHP cURL in your ap­plic­a­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­Cli­ent class that handles a PHP cURL session in its con­struct­or. The sendRe­quest() method takes a URL, con­fig­ures the cURL options, and executes the HTTP GET request. The output strings are con­cat­en­ated using PHP operators. Finally, we use the close() function to end the cURL session.

IONOS Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

Go to Main Menu