PHP cURL curl_version() Function



The PHP cURL curl_version() function is used to get the information about the cURL version installed. It returns an associative array with various details about cURL.

Syntax

Below is the syntax of the PHP cURL curl_version() function −

array curl_version ()

Parameters

This function does not accepts any parameter.

Return Value

The curl_version() function returns an associative array which contains various information like −

  • 1. version_number − The cURL version number as an integer.

  • 2. version − The cURL version number as a string.

  • 3. ssl_version_number − The OpenSSL version number as an integer.

  • 4. ssl_version − The OpenSSL version number as a string.

  • 5. libz_version − The libz version as a string.

  • 6. host − Information about the host where cURL was built.

  • 7. age − The age of the cURL library.

  • 8. features − It is a bitmask of features.

  • 9. protocols − An array of supported protocols.

PHP Version

First introduced in core PHP 4.0.2, the curl_version() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP cURL curl_version() function to get the information about version.

<?php
   // Get cURL version information
   $curl_info = curl_version();
   
   // Print the cURL version information
   echo 'cURL version: ' . $curl_info['version'] . "\n";
   echo 'SSL version: ' . $curl_info['ssl_version'] . "\n";
   echo 'libz version: ' . $curl_info['libz_version'] . "\n";
?>

Output

Here is the outcome of the following code −

cURL version: 7.53.1
SSL version: OpenSSL/1.1.1t
libs version: 1.2.11

Example 2

Now the below code retrieves cURL version information using the curl_version() function.

<?php
   // Get cURL version information
   $curl_info = curl_version();
   
   // Print supported protocols
   echo 'Supported protocols: ' . implode(', ', $curl_info['protocols']) . "\n";
?> 

Output

This will create the below output −

Supported protocols: dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, smb, smbs, smtp, smtps, telnet, tftp

Example 3

In the below PHP code we will use the curl_version() function to check the version and features of the installed cURL.

<?php
   // Get the curl version array
   $ver = curl_version();
   
   // To check for features in the curl build
   $bitfields = Array(
               'CURL_VERSION_IPV6', 
               'CURL_VERSION_KERBEROS4', 
               'CURL_VERSION_SSL', 
               'CURL_VERSION_LIBZ'
               );

   foreach($bitfields as $feature)
   {
       echo $feature . ($ver['features'] & constant($feature) ? ' It matches' : ' It does not match');
       echo '<br>';
       echo PHP_EOL;
   }
?> 

Output

This will generate the below output −

CURL_VERSION_IPV6 It matches
CURL_VERSION_KERBEROS4 It does not match
CURL_VERSION_SSL It matches
CURL_VERSION_LIBZ It matches

Example 4

In the following example, we are using the curl_version() function to get the fetch the primary IP address with the number of redirects.

<?php
   // Get cURL version information
   $curl_info = curl_version();
   
   // Print all cURL version information
   foreach ($curl_info as $key => $value) {
      if (is_array($value)) {
         echo $key . ': ' . implode(', ', $value) . "<br>";
      } else {
         echo $key . ': ' . $value . "<br>";
      }
   }
?> 

Output

Following is the output of the above code −

version_number: 472321
age: 3
features: 2671133
ssl_version_number: 0
version: 7.53.1
host: x86_64-apple-darwin14.5.0
ssl_version: OpenSSL/1.1.1t
libz_version: 1.2.11
protocols: dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, smb, smbs, smtp, smtps, telnet, tftp
ares:
ares_num: 0
libidn:
iconv_ver_num: 0
libssh_version:

Summary

The curl_version() method is a built-in method in PHP which allows you to check the information about the cURL version installed. And we have seen four examples to see the usage of the function in different cases.

php_function_reference.htm
Advertisements