PHP's Curl wrapper class usage Example _php tutorial

Source: Internet
Author: User
Tags curl options

PHP's Curl wrapper class usage Example


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:

The code is as follows:

Include_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 parameters
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:

The code is as follows:

Class Curl
{
Public $cookieFile;
Public $timeout = 160;
Public function __construct ($dir) {
$this->cookiefile = $this->gettemporarycookiefilename ($dir);
}
/**
* Set curl parameter and send request to get response content
* @access Private
* @param $curlOptions Array curl to set parameter arrays
* @return String|false Access is successful, returns the obtained information in string form, otherwise 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 Options */
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 results */
return $responseText;
}
/**
* Convert Unicode strings (u0000) to utf-8 strings, tool functions
* @access Private
* @static
* @param $string string Unicode string
* @return String Utf-8 strings
*/
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);
$utf 8Code = ";
if ($unicodeCode < 128) {
$utf 8Code = Chr ($unicodeCode);
} else if ($unicodeCode < 2048) {
$utf 8Code. = Chr (192 + (($unicodeCode-($unicodeCode% 64))/64);
$utf 8Code. = Chr (+ ($unicodeCode% 64));
} else {
$utf 8Code. = Chr (224 + (($unicodeCode-($unicodeCode% 4096))/4096);
$utf 8Code. = Chr ((($unicodeCode% 4096)-($unicodeCode% 64))/64);
$utf 8Code. = Chr (+ ($unicodeCode% 64));
}
$stringResult. = $utf 8Code;
}
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 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_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:

Copy the code code as follows:

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

http://www.bkjia.com/PHPjc/907710.html www.bkjia.com true http://www.bkjia.com/PHPjc/907710.html techarticle PHP's Curl wrapper class usage instance before using the function we need to open the Php Curl module (libeay32.dll, Ssleay32.dll, Php5ts.dll, Php_curl.dll) to start the PHP Curl function library Step 1) . Go to ...

  • 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.