In this quick tip tutorial we’re looking at how to enable CURL in PHP. I’ve included a number of different options as it may be different depending on what your running.
For those that want a bit of background on what CURL is and can be used for continue reading. If you just want the answer click here!
What is CURL?
CURL is a library created by Daniel Stenberg that allows you to connect and communicate via a variety of different protocols such as HTTP, HTTPS, FTP, Telnet etc
I personally use cURL as an alternative to file_get_contents(URL) to get web content as it provides better performance and additional functionality and flexibility.
Why doesn’t CURL work on my computer?
By default CURL isn’t enabled in Apache and therefore trying to make a CURL call won’t work until you’ve enabled it.
CURL Error Message
If curl isnt running and your trying to run it In php you’ll more than likely be Seeing an error message like : “Fatal error: Call to undefined function curl_init()”
Right, now we’ve covered that lets get cURL working!
How to enable CURL in Apache
There are a few ways I found to go about enabling CURL in apache. The one you choose will depend on what OS your running and which flavour of Apache you’ve got.
Hopefully one of these should sort you out:
Option 1 : enable CURL via the php.inI
This is the main method on any windows install like WAMP, XAMPP etc
- Locate your PHP.ini file
(normally located at in the bin folder of your apache install e.g. - Open the PHP.ini in notepad
- Search or find the following : ‘;extension=php_curl.dll’
- Uncomment this by removing the semi-colon ‘;’ before it
- Save and Close PHP.ini
- Restart Apache
Option 2: Enabling CURL in WAMP
- Left-click on the WAMP server icon in the bottom right of the screen
- PHP -> PHP Extensions -> php_curl
Option 3: enable CURL in Ubuntu
Run the following command:
sudo service apache2 restart
How to Make sure CURL is enabled and running
Option 1: Use the phpinfo() command to output PHP’s configuration and look for curl support under the listed environment variable/modules
Option 2: Create and run a simple PHP script like the following:
$ch = curl_init();// set url
curl_setopt($ch, CURLOPT_URL, “domain.com”);
//return the as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// echo output string
$output = curl_exec($ch);
echo $output;
// close curl resource to free up system resources
curl_close($ch);
Option 3: On a unix machine type the following command:
That’s it. Your done!
If you have any questions leave them below. Don’t forget to share this if it was useful!
Thank you!
Very useful article!
hey buddy! we dont know each other but i have to call u buddy because with your help i cured a one day headache! my page works fine now! MILLION THANKS!!!
No problem buddy, glad to help you out. Thanks for leaving the message 🙂
Thanks.
thanks a lot. a really helpful post
I have been trying to fix this for the past 8 hours. Here is what I have done to try and fix the problem relating to curl.
uninstalled and installed xampp. in the php.ini file, deleted the ;for the comment on the extension. restarted apache.
ran a phpinfo() and curl is not listed at all anywhere. So, not sure what the next step is. Tried to find out if I have to downloaded curl separately but I would assume it is part of xampp. Any suggestions are help.
Thanks,
Grant
Currently running a test site on localhost. I have deleted the ; in the php.ini file and restarted xampp and ran phpinfo(). Curl is not showing up anywhere in the list, whether disable or enabled. Am I missing a file or something. I uninstalled and then reinstalled xampp and it didn’t help either.
Need help on this one.
Thanks,
Grant
Hi Grant, are you definitely editing the right php.ini? Apparently in some versions of Xampp there are multiple versions of the php.ini file which could be your problem. On my installation, the main file is located in c:\xampp\php\php.ini but i’ve heard of other installs that have it in /apache/bin/php.ini
FYI – you shouldn’t have to install the curl dll separately as it comes with Xampp. You might want to double check your extension folder though just to make sure its in there. c:\xampp\php\ext\php_curl.dll
Let me know how you get on.
All the best,
Tom