Home >Backend Development >PHP Problem >How to install curl module in PHP
With the continuous development of the Internet, there are more and more demands for network applications, and PHP, as a very popular server-side language, has become the main language of many websites and applications. To use PHP, in addition to the necessary server software, you also need to install some extension modules, including the curl module. This article will introduce how to install the curl module in PHP.
The curl module is a tool for network communication in PHP. It can send HTTP requests, GET and POST data, and use FTP and SMTP. and other protocols for file transfer, etc. Using the curl module in PHP can easily obtain remote data, such as data API, web page content, etc. Therefore, the curl module is very important in PHP applications.
Before installing the curl module, you first need to check the PHP version, because different versions of PHP use different versions of the curl module. You can check the PHP version through the following command:
php -v
In the output results, you can find the PHP version number, for example:
PHP 7.2.24-0ubuntu0.18.04.7 (cli) (built: Oct 7 2021 15:24:25) ( NTS )
This version number indicates that PHP version 7.2.24 is used. If you are using another version of PHP, the version number will be different.
Before installing the curl module, you need to ensure that the curl library has been installed on your server. You can use the following command to check whether the curl library is installed:
which curl
If the curl library has been installed, the path information of curl will be output, for example:
/usr/bin/curl
If it is not installed, you need to pass the following command To install:
sudo apt-get update sudo apt-get install curl
After the installation is complete, you can use the which curl command again to check the path information of curl to ensure that curl has been successfully installed.
Before installing the curl module, you also need to install the php-curl module, which is the bridge between PHP and the curl library. You can use the following command to install the php-curl module:
sudo apt-get install php-curl
During the installation process, you will be prompted for confirmation. You can enter Y to confirm the installation.
After the installation is complete, you can use the following command to check whether the php-curl module has been installed:
php -m | grep curl
If the output curl, it means that the php-curl module has been successfully installed. If there is no output, the installation failed.
After the installation is complete, you also need to enable the php-curl module in the PHP configuration file. You can use the following command to open the PHP configuration file:
sudo nano /etc/php/7.2/apache2/php.ini
This command will open the PHP configuration file php.ini. Search curl in the file and find the following line:
;extension=curl
Remove the preceding semicolon and it becomes:
extension=curl
After saving the changes, restart the Apache server to make the configuration take effect:
sudo service apache2 restart
In order to verify whether the curl module is completely installed successfully, we can use a simple PHP script to test. Create a new php file, such as test_curl.php, and enter the following content:
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://www.baidu.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); echo $output; ?>
This PHP script uses the curl module to obtain the content of Baidu web pages and output it to the browser. Execute the script on the local server. If Baidu's web page content is output, it means that the curl module has been successfully installed.
Summary
The curl module is very important in PHP applications. Remote data can be easily obtained through the curl module. Therefore, when installing PHP, you also need to install the curl and php-curl modules. In this article, we introduced how to install the curl and php-curl modules in Ubuntu system, and enable the php-curl module. Finally, we also demonstrated a simple PHP script to test the curl module. Hope this article is helpful to everyone.
The above is the detailed content of How to install curl module in PHP. For more information, please follow other related articles on the PHP Chinese website!