Windows : Fixing PHP cURL extension not working when enabled

Windows - Missing PHP cURL extension

The other day I wanted to install a WordPress plugin on a website hosted on my Windows local development machine, but the plugin complained that it needs the cURL extension enabled on the php.ini configuration file. This seemed like an easy problem to solve, but after modifying the php.ini file, I was still getting the "curl not found" error. This article will show two different ways to make the cURL extension work on the Windows system.

Whichever solution we use, we first need to enable the cURL extension in the php.ini file. If you have already done that and the cURL is still not available, skip to the next section.

Note: When Enabling cURL in php.ini or use either of the two solutions, we need to restart the webserver for changes to take into effect. When running Apache as a service, we can easily do that by using ApacheMonitor.exe utility.

Enabling cURL extension in php.ini file

The php.ini is a PHP configuration file and among other settings, it has a list of extensions that are commented out and disabled by default.

In the steps below, we are going to enable the cURL extension.

  1. First, we need to locate the PHP folder. It's usually at C:\php, but it really depends on how it was installed in the first place. You could try to run the following code to find the location of the PHP directory:
    <?php
    echo PHP_BINDIR;
    ?>
    
  2. Inside the PHP directory, there should be a php.ini file. Open it with the text editor and search for the following line:
    ;extension=curl 
    

    Note: On older PHP versions, you might have ;extension=php_curl.dll instead of ;extension=curl in the php.ini file.
  3. Uncomment that line by removing the semicolon ; character and save the changes.
  4. Restart the web server.

Removing the comment of the extension in php.ini, saving the changes, and restarting the server should be enough to enable most extensions, but in my case, with the cURL, it was still not working.

Why cURL extension was still not enabled

It turns out, the reason why cURL cannot be found is that Windows needs to know the location of the following .dll file:

libssh2.dll

This dynamic link library file is located in the root of the PHP folder, but the problem is Windows doesn't look for .dll files there.

In the end, I was able to make it work using two different solutions. One solution is quick, but a little "hacky", while the other takes a bit more time, but is a more complete solution.

Note: You will need Administrator access to Windows for both solutions.

Solution 1 - Copy the libssh2.dll into Windows System folder

In this solution, we simply copy the required libssh2.dll into the Windows System folder, so that Windows finds the libssh2.dll file.

  1. In the File Explorer, go to the folder where PHP is located.
  2. Inside it, you should see the libssh2.dll file. Copy it.
  3. Go to the C:\Windows\System32 and Paste the file there.
  4. Restart the web server.

Now the cURL PHP extension should work.

This solution is pretty simple, but the problem with it is that if you update the PHP in the future, you might forget to again copy the newer libssh2.dll to the Windows System folder.

A much better solution is to let Windows know about the location of the PHP folder using the Path environment variable.

Solution 2 - Add PHP folder into the System Path variable

Instead of copying libssh2.dll into a Windows System folder, a much cleaner solution is to add the location of the PHP folder into the System Environment Path variable. This way, if you update a PHP in the future on the same folder, nothing else needs to be done.

The steps are as follow:

  1. In Windows Search field, look for "env". You should see two results, one to edit "environment variables" and the other to edit "system environment variables".

    Select the "Edit the system environment variables" from the search result as shown below.

    Windows 10 - Search to edit system environment variables

  2. The "System Properties" windows should open up. Click on the "Environment Variables..." button at the bottom of the window.
    Windoes - System Properties windows - selecting Environment Variables button
  3. The "Environment Variables" window should now open. The first list at the top of the window contains user variables, while the bottom list contains system variables. We need to edit the Path in system variables.

    In the bottom list for "System Variables", select the "Path" variable and click the "Edit" button (located near the OK button).

    Windows - System Environment Variables

  4. You should now see the list of locations that are in the Path variable. Click on the New button to add a new location.
    Windows - Edit Path System Environment Variable - adding a new path
  5. Paste the location of the PHP folder. For example, C:\php as shown below.
    Windows - Edit path system environment variables - Adding path to PHP folder
  6. Click on OK to close the window of Path locations and again click OK to close both the "Environment Variables" and "System Properties" windows.
  7. Restart the web server. If restarting the server doesn't work, restarting the computer should do the trick.

The PHP cURL extension should now be active, so let's test if this is indeed the case.

Testing if PHP cURL extension really is enabled

We could confirm that the cURL is working by using the phpinfo() function that outputs all the PHP configuration information, but to just check for the cURL extension, we can use the following code:

<?php
var_dump(extension_loaded("curl"));
?>

If it is enabled, the script will display bool(true), if on other hand it is still not enabled, it will display bool(false). In that case, double-check that you didn't add the PHP location in Path for user variables. Having a location for PHP folder there will not work.

Conclusion

PHP has many extensions that are disabled by default in the php.ini file. To enable an extension, we just need to uncomment it, but with the cURL extension, just uncommenting the extension=curl line might not be enough.

We learned two different ways to make cURL work on Windows. One way is to manually copy the libssh2.dll file to the Windows system folder, but a better solution is to tell Windows where the .dll file is by adding the location of the PHP folder in the PathSystem environment variable.

Tags:

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.