Install and enable curl php extension in CentOS
0x01 background
CURL is a tool that uses URL syntax to transmit files and data. It supports many protocols, such as HTTP, FTP, and TELNET. PHP also supports the cURL library.
0x001 using curl extension in PHP
Follow these steps to create a cURL request in PHP:
(1) initialization
Curl_init ()
(2) set variables
Curl_setopt (). This is the most important thing. There is a long string of cURL parameters that can be set. They can specify the details of URL requests. It may be difficult to read and understand all at once, so today we will only try out the more common and useful options.
(3) execute and obtain results
Curl_exec ()
(4) release the cURL handle
Curl_close ()
0x002 in PHP, cURL implements Get and Post
Get:
// Initialize $ ch = curl_init (); // set options, including URL curl_setopt ($ ch, CURLOPT_URL, "http://liuqunying. OK"); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_HEADER, 0); // execute and obtain the HTML document content $ output = curl_exec ($ ch); // release the curl handle curl_close ($ ch ); // print the obtained data print_r ($ output );
Post Implementation
$ Url = "http://myserver.lqy/web_services.php"; $ post_data = array ("username" => "qunyingliu", "key" => "! 234s67B "); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // post Data curl_setopt ($ ch, CURLOPT_POST, 1); // The post variable curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); $ output = curl_exec ($ ch ); curl_close ($ ch); // print the obtained data print_r ($ output );
0x02 deployment of PHP cURL extension in tlinux environment 0x001 Environment
System: tlinux 1.2 final (centos 6.2)
Php version: PHP 5.4.36 x64
0x002 Installation
# Install curl, install yum and RPM, or compile and install the source code. Select either of them.
Yum install curl
Rpm-ivhhttp: // configure
# Source code compilation and installation curl:
Install cURLwgethttp: // curl. haxx. se/download/curl-7.42.1.tar.gztar-zxfcurl-7.42.1.tar.gz. /configure -- prefix =/usr/local/curl (optional, installed in the/usr/local/bin directory by default) make & makeinstall
# Install php extensions in two ways: 1. install php through source code compilation, and add -- with-curl =/usr/local/curl (curl directory); 2. compile and install php extension curl. so
# Curl extension for source code compilation and Installation
Download source code: php-5.4.36.tar.gz
Decompress the source code and go to the ext/curl directory.
Installation:
/Usr/local/php/bin/phpize
./Configure -- with-php-config =/usr/local/php/bin/php-config -- with-curl = DIR (you do not need to specify the default path)
Make & make install
The curl. so file is generated in/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
Configuration: in php. ini or put all extensions in a separate directory.
Cd/usr/local/php/etc/subconfig; echo 'extension = "curl. so" '> curl. ini
Modify php. ini: allow_url_fopen = On
Enable to make the extension take effect:
/Usr/local/apache/bin/apachectl configtest
/Usr/local/apache/bin/apachectl-k restart
/Usr/local/php/bin/php-m | grep curl
Curl extension of a successfully compiled version
Simplified deployment script:
PHP_VERSION_OK=$(/usr/local/php/bin/php-v|grep"5.4."|wc-l)if[1-gt${PHP_VERSION_OK}];thenecho"PHPversionisnot5.4.x"exit1ficd/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525wget-q-Ocurl.sohttp://myserver.lqy/download/20150520/2015052046_curl.socd/usr/local/php/etc/subconfigecho'extension="curl.so"'>curl.inised-i'/allow_url_fopen/callow_url_fopen=On'/usr/local/php/etc/php.iniif[-d/usr/local/apache]&&[!-d/usr/local/apache2];thenln-s/usr/local/apache/usr/local/apache2fi/usr/local/apache2/bin/apachectl-krestart/usr/local/php/bin/php-m|grepcurl