This article mainly introduces php's curl encapsulation class usage, describes the curl encapsulation class and its usage in the form of instances in detail, and summarizes the GET and POST usage, for more information about the usage of php curl encapsulation classes, see the examples in this article. These two functions allow us to conveniently use php curl-related functions. Share it with you for your reference. The details are as follows:
Before using the function, we need to open the php curl module (libeay32.dll, ssleay32.dll, php5ts. dll, php_curl.dll)
To enable the php curl function library
1) remove the file in windows/php. ini; prefix extension = php_curl.dll;/* use echo phpinfo () to view the php. ini path */
2) copy php5/libeay32.dll and ssleay32.dll to the system directory windows/
3) restart apache
The code is as follows:
The code is as follows:
<? Php
Include_once ('curl. class. php ');
$ Aa = new Curl ('');
$ CurlOptions = array (
CURLOPT_URL => "http://www.xx.com/addTicket.jsp", // access URL
CURLOPT_RETURNTRANSFER => true, // returns the result as a string.
CURLOPT_REFERER => "ww. ww. ww/zw2 ",
CURLOPT_HTTPHEADER => array ('x-FORWARDED-FOR: 139.197.14.19 ', 'client-IP: 127.0.0.1', 'proxy-CLIENT-IP: 139.197.14.19 ', 'wl-Proxy-Client-IP: 139.197.14.19 '),
CURLOPT_HEADER => 1, // Get the returned header information
// CURLOPT_SSL_VERIFYPEER => false, // supports SSL encryption
CURLOPT_POST => true, // The POST parameter is included in the sending process.
CURLOPT_POSTFIELDS => 'IDS = 897 & Submit = % E6 % 8A % 95% E7 % A5 % A8 ', // the request's POST parameter string
CURLOPT_TIMEOUT => $ aa-> timeout // wait for the response time
);
Echo $ aa-> getResponseText ($ curlOptions );
Cul processing class:
The code is as follows:
<? Php
Class Curl
{
Public $ cookieFile;
Public $ timeout = 160;
Public function _ construct ($ dir ){
$ This-> cookieFile = $ this-> getTemporaryCookieFileName ($ dir );
}
/**
* Set the CURL parameter and send a request to obtain the response content.
* @ Access private
* @ Param $ curlOptions array curl sets the parameter array
* @ Return string | false: the access is successful. the obtained information is returned in the string format. otherwise, false is returned.
*/
Public function getResponseText ($ curlOptions ){
/* Set CURLOPT_RETURNTRANSFER to true */
If (! Isset ($ curlOptions [CURLOPT_RETURNTRANSFER]) | $ curlOptions [CURLOPT_RETURNTRANSFER] = false ){
$ CurlOptions [CURLOPT_RETURNTRANSFER] = true;
}
/* Initialize the curl module */
$ Curl = curl_init ();
/* Set curl options */
Curl_setopt_array ($ curl, $ curlOptions );
/* Send a request and obtain the response information */
$ ResponseText = '';
Try {
$ ResponseText = curl_exec ($ curl );
If ($ errno = curl_errno ($ curl ))! = CURLM_ OK ){
$ Errmsg = curl_error ($ curl );
Throw new Exception ($ errmsg, $ errno );
}
} Catch (Exception $ e ){
// Predictiondisposefunction ($ e );
// Print_r ($ e );
$ ResponseText = false;
}
/* Disable the curl module */
Curl_close ($ curl );
/* Returned result */
Return $ responseText;
}
/**
* Converts a Unicode string (u0000) to a UTF-8 string, a tool function
* @ Access private
* @ Static
* @ Param $ string Unicode string
* @ Return string UTF-8 string
*/
Public function unicodeToUtf8 ($ string ){
$ String = str_replace ('u', '', strtolower ($ string ));
$ Length = strlen ($ string)/4;
$ StringResult = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ CharUnicodeHex = substr ($ string, $ I * 4, 4 );
$ UnicodeCode = hexdec ($ charUnicodeHex );
$ Utf8Code = '';
If ($ unicodeCode <128 ){
$ Utf8Code = chr ($ unicodeCode );
} Else if ($ unicodeCode <2048 ){
$ Utf8Code. = chr (192 + ($ unicodeCode-($ unicodeCode % 64)/64 ));
$ Utf8Code. = chr (128 + ($ unicodeCode % 64 ));
} Else {
$ Utf8Code. = chr (224 + ($ unicodeCode-($ unicodeCode % 4096)/4096 ));
$ Utf8Code. = chr (128 + ($ unicodeCode % 4096)-($ unicodeCode % 64)/64 ));
$ Utf8Code. = chr (128 + ($ unicodeCode % 64 ));
}
$ StringResult. = $ utf8Code;
}
Return $ stringResult;
}
Private function getTemporaryCookieFileName ($ dir = '.'){
Return (str_replace ("", '/', tempnam ($ dir, 'tmp ')));
}
}
Example 2
The code is as follows:
<? Php
// Curl class
Class Curl
{
Function Curl (){
Return true;
}
Function execute ($ method, $ url, $ fields = '', $ userAgent ='', $ httpHeaders = '', $ username ='', $ password = ''){
$ Ch = Curl: create ();
If (false ===$ ch ){
Return false;
}
If (is_string ($ url) & strlen ($ url )){
$ Ret = curl_setopt ($ ch, CURLOPT_URL, $ url );
} Else {
Return false;
}
// Whether the header information is displayed
Curl_setopt ($ ch, CURLOPT_HEADER, false );
//
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
If ($ username! = ''){
Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password );
}
$ Method = strtolower ($ method );
If ('post' = $ method ){
Curl_setopt ($ ch, CURLOPT_POST, true );
If (is_array ($ fields )){
$ Sets = array ();
Foreach ($ fields AS $ key => $ val ){
$ Sets [] = $ key. '='. urlencode ($ val );
}
$ Fields = implode ('&', $ sets );
}
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
} Else if ('put' = $ method ){
Curl_setopt ($ ch, CURLOPT_PUT, true );
}
// Curl_setopt ($ ch, CURLOPT_PROGRESS, true );
// Curl_setopt ($ ch, CURLOPT_VERBOSE, true );
// Curl_setopt ($ ch, CURLOPT_MUTE, false );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // sets the curl timeout time in seconds.
If (strlen ($ userAgent )){
Curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent );
}
If (is_array ($ httpHeaders )){
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders );
}
$ Ret = curl_exec ($ ch );
If (curl_errno ($ ch )){
Curl_close ($ ch );
Return array (curl_error ($ ch), curl_errno ($ ch ));
} Else {
Curl_close ($ ch );
If (! Is_string ($ ret) |! Strlen ($ ret )){
Return false;
}
Return $ ret;
}
}
Function post ($ url, $ fields, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = ''){
$ Ret = Curl: execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password );
If (false ===$ ret ){
Return false;
}
If (is_array ($ ret )){
Return false;
}
Return $ ret;
}
Function get ($ url, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = ''){
$ Ret = Curl: execute ('GET', $ url, '', $ userAgent, $ httpHeaders, $ username, $ password );
If (false ===$ ret ){
Return false;
}
If (is_array ($ ret )){
Return false;
}
Return $ ret;
}
Function create (){
$ Ch = null;
If (! Function_exists ('curl _ init ')){
Return false;
}
$ Ch = curl_init ();
If (! Is_resource ($ ch )){
Return false;
}
Return $ ch;
}
}
?>
Usage
GET usage:
The code is as follows:
$ Curl = new Curl ();
$ Curl-> get ('http: // www.jb51.net /');
POST usage:
The code is as follows:
$ Curl = new Curl ();
$ Curl-> get ('http: // www.jb51.net/', 'p = 1 & time = 0 ′);
I hope this article will help you with PHP programming.