PHP's Curl wrapper class usage example, Phpcurl encapsulation Instance _php tutorial

Source: Internet
Author: User
Tags curl options php programming

PHP's Curl wrapper class usage example, Phpcurl encapsulation instance


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:

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,//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:
Copy CodeThe code is as follows: <?php
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

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 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:
Copy the Code code as follows: $curl = new curl ();
$curl->get (' http://www.bkjia.com/');
Post usage:
Copy the Code code as follows: $curl = new curl ();
$curl->get (' http://www.bkjia.com/', ' p=1&time=0′ ');

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


PHP uses curl to crawl the content of a site is rejected

It's just written. Hope Useful
>

PHP Curl Encapsulation as a function problem

The reason is that the variable name is wrong, the variable name ② equals the variable name ③ not equal to the variable name ①, remember to adopt Oh!
$output ①= curl_exec ($ch); return the result;
Curl_close ($ch); Turn off communication;
if ($output = = ") {
$OUTPTU ②= 3;
}
$dataapi = $outptu ③;
return $DATAAPI;
}

http://www.bkjia.com/PHPjc/907838.html www.bkjia.com true http://www.bkjia.com/PHPjc/907838.html techarticle PHP's Curl wrapper class usage example, Phpcurl Encapsulation Example This article describes the two Php Curl wrapper class Usage examples, these two functions can make us very convenient to use PHP curl related ...

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