How to Enable cURL in PHP Extension?

What is cURL?
cURL (Client URL) is a software tool developed by Daniel Stenberg that enables applications to communicate with other servers by making HTTP requests. It's employed for activities such as logging into apps via Google or Facebook, talking to APIs, or fetching data. When you log in through Google or Facebook, cURL assists in fetching user authentication tokens from the respective services. With PHP, cURL makes it easy to send HTTP requests (like GET or POST) and process responses, including HTML, JSON, and file downloads. In this tutorial, we will see how to activate cURL in PHP extension step by step.
Also mentioned here is: how to enable curl in the PHP extension
Enable cURL in PHP (Apache)
To enable cURL in Apache, you need to set the php.ini file.
Step-by-Step Guide:
Step 1: Locate the php.ini file. This is usually in the server's root directory or the public_html directory. Open it with a text editor.
Step 2: Look for the line;extension=php_curl.dll using Ctrl+F. Delete the semi-colon (;) at the start of the line to enable cURL.
Step 3: Save the php.ini file by pressing Ctrl+S, and then restart Apache from the terminal or command line.
Enabling cURL in WAMP
WAMP is a Windows software stack that packages Apache, MySQL, and PHP together for simple installation.
Steps to Enable cURL in WAMP:
- Left-click on the WAMP server icon in the bottom right corner of your screen.
- Go to PHP -> PHP Extensions -> curl.
This will activate the cURL extension in your WAMP server.
Related topics you may enjoy: How To Use Triggers in MySQL?
Activating cURL in Ubuntu
To activate cURL in Ubuntu, use the following commands:
- Install PHP cURL by executing:
sudo apt-get install php5-curl
2. Restart Apache to implement the changes:
sudo service apache2 restart
To check if cURL is enabled on your server, you can use a small PHP script. If cURL is not enabled, the script will show an error when trying to run cURL functions.
Here's how to check if cURL is enabled and troubleshoot it:
3. Check if cURL is enabled with PHP
Create a simple PHP file (e.g., check_curl.php) and add the following code:
<?php
// Check if cURL is enabled
if (function_exists('curl_version')) {
echo "cURL is enabled. Version: " . curl_version()['version'];
} else {
echo "cURL is not enabled!";
}
?>
1. What to expect:
- If cURL is enabled, it will display the cURL version (e.g., cURL is enabled. Version: 7.x.x).
- If cURL is not enabled, it will display that cURL is not enabled!
2. Troubleshooting:
- If you get the message saying cURL is not enabled, you'll need to enable it in your server configuration (as discussed in earlier responses for Apache, WAMP, and Ubuntu).
Example PHP code using cURL
If cURL is enabled, you can run your previous code to fetch the content from a URL. Below is the complete example with a correction:
<?php
// Create a cURL resource
$ch = curl_init();// Set the URL (example: GeeksforGeeks website)
curl_setopt($ch, CURLOPT_URL, "https://www.geeksforgeeks.org");// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Execute the cURL request and store the output
$output = curl_exec($ch);// Display the output (HTML content from the URL)
echo $output;// Close the cURL resource
curl_close($ch);?>
Expected Output: The HTML content of the GeeksforGeeks page will be displayed on your local host running Apache, as the cURL request fetches the content from the URL.
By following these steps, you can enable cURL in PHP, WAMP, and Ubuntu to handle HTTP requests and integrate with various external services in your web applications.