PHP Curl Encapsulation Class Usage example

Source: Internet
Author: User
Tags chr curl ini strlen

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 function library

1). remove Windows/php.ini file; Extension=php_curl.dll Front; /* Use echo phpinfo () to view the path of the php.ini * *

2). Copy the Php5/libeay32.dll,ssleay32.dll to the system directory windows/

3). Restart Apache

PHP Curl

The code is as follows Copy Code

<?php

Include_once (' curl.class.php ');
$aa =new Curl (");
$curlOptions = Array (
Curlopt_url => "http://ww.ww.ww/addTicket.jsp",//Access URL
Curlopt_returntransfer => True,//Get result returned as 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 Return header information

Curlopt_ssl_verifypeer => false,//support for 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//wait for response time
);
echo $aa->getresponsetext ($curlOptions);

Cul processing class

<?php
Class Curl
{
Public $cookieFile;
Public $timeout = 160;

Public Function __construct ($dir) {
$this->cookiefile = $this->gettemporarycookiefilename ($dir);
}

/**
* Set the curl parameter and send the request to get the response content
* @access Private
* @param $curlOptions Array Curl set parameter arrays
* @return String|false Access succeeds, returns the obtained information as a string;
*/
Public Function GetResponseText ($curlOptions) {
/* Set Curlopt_returntransfer to True * *
if (!isset ($curlOptions [curlopt_returntransfer]) | | $curlOptions [Curlopt_returntransfer] = = False) {
$curlOptions [Curlopt_returntransfer] = true;
}
/* Initialize 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;
}
/* Close Curl Module * *
Curl_close ($curl);
/* Return result * *
return $responseText;
}

/**
* Converts a Unicode string (u0000) to a utf-8 string, a tool function
* @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 (($unicodeCode-($unicodeCode% 64))/64);
$utf 8Code. = chr (128 + ($unicodeCode% 64));
} else {
$utf 8Code. = Chr (224 + ($unicodeCode-($unicodeCode% 4096))/4096);
$utf 8Code. = chr (128 + (($unicodeCode% 4096)-($unicodeCode% 64))/64);
$utf 8Code. = chr (128 + ($unicodeCode% 64));
}
$stringResult. = $utf 8Code;
}
return $stringResult;
}

Private Function gettemporarycookiefilename ($dir = '. ') {
Return (Str_replace ("", '/", Tempnam ($dir, ' tmp '));
}
}


Example 2

The code is as follows Copy Code

<?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 number of 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′ ');

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.