Php curl encapsulation class usage instance, phpcurl encapsulation instance

Source: Internet
Author: User
Tags curl options

Php curl encapsulation class usage instance, phpcurl encapsulation instance

The examples in this article describe the usage examples of two php curl encapsulation classes. 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:

Copy codeThe 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:
Copy codeThe 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

Copy codeThe 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:
Copy codeThe Code is as follows: $ curl = new Curl ();
$ Curl-> get ('HTTP: // www.bkjia.com /');
POST usage:
Copy codeThe Code is as follows: $ curl = new Curl ();
$ Curl-> get ('HTTP: // www.bkjia.com/', 'P = 1 & time = 0 ′);

I hope this article will help you with PHP programming.


Php uses curl to capture the content of a website and is rejected.

Just written. Hope to be useful
<? Php $ binfo = array ('mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net clr 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar) ', 'mozilla/5.0 (Windows NT 5.1; rv: 22.0) Gecko/20100101 Firefox/123456 ', 'mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. NET4.0C; Alexa Toolbar) ', 'mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', $ _ SERVER ['HTTP _ USER_AGENT ']); // 218.242.124.16 * // 125.90.88. * $ cip = '2017. 242. 124. '. mt_rand (0,254); $ xip = '2017. 242. 124. '. mt_rand (0,254); $ header = array ('client-IP :'. $ cip, 'x-FORWARDED-:'. $ xip,); function getimgs ($ url, $ data, $ userinfo, $ header) {$ ch = curl_init (); $ timeout = 5; curl_setopt ($ ch, CURLOPT_URL, "$ url"); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ ch, CURLOPT_REFERER, "www.sgs.gov.cn/lz/etpsInfo.do? Method = index "); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data ); curl_setopt ($ ch, CURLOPT_USERAGENT, "$ userinfo"); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout); $ contents = curl_exec ($ ch); curl_close ($ ch ); return $ contents;} $ url = '...... remaining full text>

Php curl encapsulation as a function

The reason is that the variable name is wrong. variable name ② is equal to variable name ③ is not equal to variable name ①. Remember to use it!
$ Output ① = curl_exec ($ ch); // return the result;
Curl_close ($ ch); // disable communication;
If ($ output = ''){
$ Outptu ② = 3;
}
$ Dataapi = $ outptu ③;
Return $ dataapi;
}

Related Article

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.