PHP: Enabling cURL in Windows

Recently I was working with some PHP code that made use of cURL, and found it wasn’t enabled on my Windows server. The error was similar to this:

Fatal error: Call to undefined function curl_init()

The good news is that PHP libcurl is bundled in the Windows PHP installation by default. It just has to be enabled.

What is PHP cURL?

The official PHP documentation describes the libcurl libarary as

"...a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols."

You’ll commonly see it used in PHP to make API calls to web servers, which was what my code was trying to do.

Enabling cURL in Windows

First you’ll need to know the location of the server’s php.ini file, and have access to edit it.

If you don’t know the location of php.ini you can follow step 1-3 below to locate it. If you already know where the php.ini lives skip to step 4.

  1. Create a new file info.php on your web server. Paste the following into the file and save:

    <?php
        phpinfo();
    ?>
    
  2. Use a web browser to open info.php

    If PHP is configured correctly (hopefully it is!) you should see a heap of configuration information displayed.

  3. Find the Loaded Configuration File setting and note it’s value. This is the location of the php.ini file.

    For example, it may be like: C:\php5.6.30\php.ini

    Now we need to edit the php.ini file and enable the libcurl extension.

  4. Open the php.ini file in a text editor

  5. Search for php_curl

    You should find a line similar to this:

    ;extension=php_curl.dll
    

    It is commented out by the ; at the start of the line.

  6. Uncomment the line (remove the preceeding ;), i.e.:

    extension=php_curl.dll
    

    Note that you may need to set the absolute path to php_curl.dll located within the ext directory of the PHP installation, for example:

    extension=C:\php5.6.30\ext\php_curl.dll
    
  7. Save the changes to php.ini

  8. Restart the Apache httpd service

  9. In your web browser reload the info.php and search for curl

    You should find something like:

    cURL support: enabled