Home Backend Development PHP Tutorial PHP封装CURL扩展类实例_PHP

PHP封装CURL扩展类实例_PHP

May 30, 2016 am 08:46 AM
curl php encapsulation Extended class

本文实例讲述了PHP封装CURL扩展类。分享给大家供大家参考。具体如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

<&#63;php

/**

* @description: 封装CURL扩展

* @date: 2014-07-28 16:04

*/

/**

* @编码规范

* @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage

* @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result

* @function 函数名与类名规则相同 eg: function SendRequest

* @params 函数形参规则与变量名相同

* @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_

*/

/**

* @要求

*

*/

class Curl{

/**

* @请求的host

*/

private $host_;

/**

* @curl 句柄

*/

private $ch_;

/**

* @超时限制时间

*/

const time_=5;

/**

* @请求的设置

*/

private $options_;

/**

* @保存请求头信息

*/

private $request_header_;

/**

* @保存响应头信息

*/

private $response_header_;

/**

* @body_ 用于保存curl请求返回的结果

*/

private $body_;

/**

* @读取cookie

*/

private $cookie_file_;

/**

* @写入cookie

*/

private $cookie_jar_;

/**

* @todo proxy

* @构造函数,初始化CURL回话

*/

public function Start($url){

$this->ch_ = curl_init($url);

curl_setopt($this->ch_, CURLOPT_HEADER, 1);

curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );

}

/**

* @返回响应头

*/

public function ResponseHeader($url){

if (!function_exists('http_parse_headers')) {

function http_parse_headers ($raw_headers){

$headers = array();

foreach (explode("\n", $raw_headers) as $i => $h) {

$h = explode(':', $h, 2);

if (isset($h[1])) {

if(!isset($headers[$h[0]])) {

$headers[$h[0]] = trim($h[1]);

} else if(is_array($headers[$h[0]])) {

$tmp = array_merge($headers[$h[0]],array(trim($h[1])));

$headers[$h[0]] = $tmp;

} else {

$tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));

$headers[$h[0]] = $tmp;

}

}

}

return $headers;

}

}

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);

$this->body_=$this->Execx();

$header_size = curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE);

$this->response_header_ = substr($this->body_, $start = 0, $offset = $header_size);

$this->response_header_ = http_parse_headers($this->response_header_);

print_r($this->response_header_);

return $this->Close($this->body_);

}

/**

* @读取cookie

*/

public function LoadCookie($url,$cookie_file){

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_COOKIE, 1);

curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file);

$this->body_=$this->Execx();

return $this->Close($this->body_);

}

/**

* @写入cookie

*/

public function SaveCookie($url){

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_COOKIE, 1);

curl_setopt($this->ch_, CURLOPT_COOKIEFILE ,'cookie.txt');

curl_setopt($this->ch_, CURLOPT_COOKIEJAR , 'cookie.txt');

$this->body_=$this->Execx();

return $this->Close($this->body_);

}

/**

* @设置HEADER

*/

public function SetHeader($headers = null){

if (is_array($headers) && count($headers) > 0) {

curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);

}

}

/**

* @GET请求

*/

public function Get($url, array $params = array()) {

if ($params) {

if (strpos($url, '&#63;')) {

$url .= "&".http_build_query($params);

}

else {

$url .= "&#63;".http_build_query($params);

}

}

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);

if (strpos($url, 'https') === 0) {

curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);

}

$this->body_=$this->Execx();

return $this->Close($this->body_);

}

/**

* @POST请求

*/

public function Post($url, array $params = array()) {

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));

curl_setopt($this->ch_, CURLOPT_POST, true);

curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);

if ($params) {

curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params));

}

$this->body_=$this->Execx();

return $this->Close($this->body_);

}

/**

* @tips: google http head 方法

*/

public function Head($url, array $params = array()) {

$this->Start($url);

curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);

curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0);

curl_setOpt($this->ch_,CURLOPT_NOBODY, true);

$this->body_=$this->Execx();

return $this->Close($this->body_);

}

/**

* @执行CURL会话

*/

public function Execx(){

return curl_exec($this->ch_);

}

/**

* @关闭CURL句柄

*/

public function Close($body_){

if ($body_ === false) {

echo "CURL Error: " . curl_error($body_);

return false;

}

curl_close($this->ch_);

return $body_;

}

}

Copy after login

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles