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:
"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, // Obtain the returned header information // CURLOPT_SSL_VERIFYPEER => false, // supports SSL encryption. CURLOPT_POST => true, // The POST parameter CURLOPT_POSTFIELDS => 'IDS = 897 & Submit = % E6 % 8A % 95% E7 % A5 % A8 'is included during transmission ', // request's POST parameter string CURLOPT_TIMEOUT => $ aa-> timeout // wait for response time); echo $ aa-> getResponseText ($ curlOptions );
Cul processing class:
CookieFile = $ this-> getTemporaryCookieFileName ($ dir);}/*** sets the CURL parameter and sends a request, get the response content * @ access private * @ param $ curlOptions array curl: set the parameter array * @ return string | false: the access is successful and the obtained information is returned in the string format; otherwise, false */public function getResponseText ($ curlOptions) is returned. {/* 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 the curl option */curl_setopt_array ($ curl, $ curlOptions ); /* send the 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) {// exceptionDisposeFunction ($ e); // print_r ($ e); $ responseText = false;}/* close the curl module */curl_close ($ curl ); /* returned result */return $ responseText;}/*** converts a Unicode string (u0000) to a UTF-8 string, 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
$ 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 ); // Set curl timeout seconds if (strlen ($ userAgent) {cur Rochelle 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 = '', $ pas Sword = '') {$ 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:
$curl = new Curl();$curl->get('#');
POST usage:
$curl = new Curl();$curl->get('#', 'p=1&time=0′);
I hope this article will help you with PHP programming.
For more articles on curl encapsulation examples of php, please refer to the Chinese PHP website!