- Published on : 18 November 2013 at 21:18 UTC
- Lionel Eppe
If you've ever tried to use the "curl" of PHP on Windows, you may have noticed that PHP you return an error stating that the "curl_init()" function (or another function curl) was not defined.
The reason is simple : this extension is not enabled by default in PHP.
- Enable the "curl" extension in php.ini
- Add the PHP folder path in the "PATH" environment variable of Windows.
- Restart the Apache service take into account changes
- Test the curl extension through our example
First, to enable the "curl" extension to PHP, you have 2 options:
- Either you have WAMP :
In this case, you can activate it by clicking on the WAMP (left-click) and then going to "PHP -> PHP extensions". Then click "php_curl" to a "v" appears next.
- Either you installed PHP from the official website of the author :
In this case, you need to edit the "php.ini" file located in the root folder of PHP.
In this file, uncomment the following line by removing the ";" if present (or add it if it is not there) :
As stated on the official website of PHP curl module needs DLLs "libeay32.dll" and "ssleay32.dll" to work.
So PHP can find and use so you have 2 solutions :
- Either copy the folder C:\Windows\System32
- Either add the folder path in the PHP environment "PATH" variable of Windows. (Recommended)
To add, modify the variable by adding the path to the PHP file at the end of this variable, separating by ;
Note : If you don't know how to change an environment variable, take a look at our tutorial : Change and / or display environment variables in Windows
To restart the Apache service is the web server running PHP as a module :
- Launch the "services.msc program
- Select "Apache" (named "wampapache" with WAMP) service
- Then click Restart (in the right column)
For more information on managing services in Windows, take a look at our tutorial : Managing Windows Services
Here is a PHP script that will show you the Wikipedia page on curl.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Example of using curl</title> </head> <body> <?php // URL of the page you want to retrieve the content (more precisely : the source code) $url = 'http://fr.wikipedia.org/wiki/CURL'; // Initialize a cURL session $ch = curl_init(); // Set some options // - Specifies the URL which curl will access curl_setopt($ch, CURLOPT_URL, $url); // - Return the content by the curl_exec function instead of displaying content directly recovered curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // - Set a user-agent to send to the server. For example, you can send a user-agent of an Android phone, iPhone, ... to try to recover the contents of a mobile version of a website curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0'); // - Execute the cURL session and stores the retrieved content in the variable $ result (through CURLOPT_RETURNTRANSFER optional) $resultat = curl_exec ($ch); // - Close a cURL session and thus the connection to the remote server curl_close($ch); // Displays the contents recovered by curl echo $resultat; ?> </body> </html>