PHP's Curl wrapper class usage Example

Source: Internet
Author: User
Tags php programming
This example describes the usage examples of two PHP Curl wrapper classes, which make it very convenient for us to use the PHP curl correlation function. Share to everyone for your reference. Specific as follows:

We need to open the PHP Curl module before using the function (Libeay32.dll, Ssleay32.dll, Php5ts.dll, Php_curl.dll)

To open the PHP Curl Library

1). Remove the Windows/php.ini file; Extension=php_curl.dll Front; /* with Echo phpinfo (); View PHP.ini's Path */
2). Copy the Php5/libeay32.dll,ssleay32.dll to the system directory windows/
3). Restart Apache

The code is as follows:

<?phpinclude_once (' curl.class.php '); $aa =new Curl ('); $curlOptions = Array (curlopt_url = "http://www.xx.com/addTicket.jsp",//Access URL Curlopt_returntransfer = true,// Gets the result as a string return 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 Return header information//curlopt_ssl_verifypeer + false,//support SSL encryption Curlopt_post + true,//send with POST parameter Curlopt_postfields = > ' ids=897&submit=%e6%8a%95%e7%a5%a8 ',//requested post parameter string curlopt_timeout = = $aa->timeout//Time to wait for response); echo $aa->getresponsetext ($curlOptions);


Cul processing class:

<?phpclass curl{public $cookieFile;p ublic $timeout = 160; Public function __construct ($dir) {$this->cookiefile = $this->gettemporarycookiefilename ($dir);} /*** set the curl parameter and send the request, get the response content * @access private* @param $curlOptions Array Curl Set the parameter arrays * @return String|false access succeeded, Returns the obtained information as a string, otherwise returns False*/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 Option */curl_setopt_array ($curl, $curlOptions);/* Send request and get 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;} /* Turn off the Curl Module */curl_close ($curl);/* Return the result */return $responseText;} /*** converts a Unicode string (u0000) to a utf-8 string, the tool function * @access Private* @static * @param $string string Unicode String * @return string Utf-8 string */public function UnicodeToUtf8 ($string) {$stri ng = Str_replace (' u ', ' ', Strtolower ($string)); $length = strlen ($string)/4; $stringResult = "; for ($i = 0; $i < $lengt H  $i + +) {$charUnicodeHex = substr ($string, $i * 4, 4), $unicodeCode = Hexdec ($charUnicodeHex); $utf 8Code = "; if ($unicodeCode <) {$utf 8Code = Chr ($unicodeCode),} else if ($unicodeCode < 2048) {$utf 8Code. = Chr (192 + ($unicodeCode-($uni Codecode)), $utf 8Code. = Chr (+ ($unicodeCode% 64));} else {$utf 8Code. = Chr (224 + ($unicodeCode-($unicodeCode% 4096)/4096)), $utf 8Code. = Chr (+ (($unicodeCode% 409 6)-($unicodeCode)), $utf 8Code. = Chr (+ ($unicodeCode% 64));} $stringResult. = $utf 8Code;} return $stringResult;} Private Function gettemporarycookiefilename ($dir = '. ') {return (Str_replace ("", '/', Tempnam ($dir, ' tmp '))}}


Example 2

<?php//curl class curl{function Curl () {return true;        The 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 to display header information 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);//Set Curl timeout seconds if (strlen ($userAgent)) {curl_setopt ($ch, Curlopt_user        AGENT, $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::exec        Ute (' 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.php.cn/');

Post usage:

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

I hope this article is helpful to everyone's PHP programming.

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.