Phpcurl encapsulation class example _ PHP Tutorial

Source: Internet
Author: User
Tags curl options
Example of phpcurl encapsulation class. Before using a function in the phpcurl encapsulation class, we need to open the phpcurl module (libeay32.dll, ssleay32.dll, php5ts. dll, php_curl.dll) to enable the phpcurl function library step 1 ). example of removing php curl encapsulation class

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

Php curl

The code is as follows:

Include_once ('curl. class. php ');
$ Aa = new Curl ('');
$ CurlOptions = array (
CURLOPT_URL => "http://ww.ww.ww/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

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:

// 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:

$ Curl = new Curl ();
$ Curl-> get ('http: // www.111cn.net /');

POST usage

$ Curl = new Curl ();
$ Curl-> get ('http: // www.111cn.net/', 'p = 1 & time = 0 ′);

Http://www.bkjia.com/PHPjc/834973.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/834973.htmlTechArticlephp curl encapsulation class use example 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 step 1 ). remove...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.