0x01 Background Introduction
CURL is a tool that uses URL syntax to transfer files and data, and supports many protocols, such as HTTP, FTP, Telnet, and so on. PHP also supports the CURL library.
0x001 using Curl extension in PHP
Basic steps for creating a curl request in PHP:
(1) initialization
Curl_init ()
(2) Setting variables
curl_setopt (). The most important thing is here. There is a long list of curl parameters that can be set to specify the details of the URL request. It may be difficult to read them all at once and to understand them, so today we'll just try some of the more commonly used and more useful options.
(3) Execute and get results
Curl_exec ()
(4) Release curl handle
Curl_close ()
0x002 PHP for Get and post in Curl
Get Mode implementation:
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); Executes and obtains the contents of the HTML document $output = curl_exec ($ch); Release Curl handle Curl_close ($ch); Print the obtained data print_r ($output);
Post Mode 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); Post variables curl_setopt ($ch, Curlopt_postfields, $post _data); $output = curl_exec ($ch); Curl_close ($ch); Print the obtained data print_r ($output);
0x02 tlinux Environment deployment php Curl Extension0X001 Environment
System: Tlinux 1.2 final (CentOS 6.2)
PHP Version: PHP 5.4.36 x64
0X002 Installation
# #安装curl, yum and rpm installation or source code compilation installation, three select one can
Yum Install Curl
RPM-IVH http://mirror.city-fan.org/ftp/contrib/sysutils/Mirroring/curl-7.42.1-1.0.cf.rhel6.x86_64.rpm
# #源码编译安装curl:
Installing Curlwget http://curl.haxx.se/download/curl-7.42.1.tar.gz tar-zxf curl-7.42.1.tar.gz./configure--prefix=/usr/ Local/curl (optional, installed by default in the/usr/local/bin directory) make & make Install
# #两种方式安装php扩展: 1. Compile and install PHP, add--with-curl=/usr/local/curl (Curl directory), 2. Compile and install PHP extensions curl.so
# #源码编译安装curl扩展
: php-5.4.36.tar.gz
unzip the source , and go to the Ext/curl directory
install :
/usr/local/php/bin/phpize
. /configure--with-php-config=/usr/local/php/bin/php-config--with-curl=dir (default path is not specified)
Make&&make Install
generates curl.so files in/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
config : in php.ini or place 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 for the extension to take effect:
/usr/local/apache/bin/apachectl Configtest
/usr/local/apache/bin/apachectl-k restart
/usr/local/php/bin/php-m |grep Curl
A successful version of the Curl extension has been compiled
Simplified deployment script:
php_version_ok=$ (/usr/local/php/bin/php - v|grep "5.4." | wc -l) if [ 1 -gt ${php_version_ok} ];thenecho "PHP version is not 5.4.x "Exit 1ficd /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525wget -q -o curl.so http://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 -k restart/usr/local/php/ Bin/php -m |grep curl
This article from "Ops said: from rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1653323
Installing and opening the Curl PHP extension under Tlinux (CentOS)