搜索
首页后端开发php教程php基于curl扩展制作跨平台的restfule 接口_PHP教程

php基于curl扩展制作跨平台的restfule 接口

   这篇文章主要介绍了php基于curl扩展制作跨平台的restfule 接口的相关资料以及详细的代码,有需要的小伙伴可以参考下。

  restfule 接口

  适用的平台:跨平台

  所依赖:curl扩展

  git:https://git.oschina.net/anziguoer/restAPI

  ApiServer.php

  ?

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

/**

* @Author: yangyulong

* @Email : anziguoer@sina.com

* @Date: 2015-04-30 05:38:34

* @Last Modified by: yangyulong

* @Last Modified time: 2015-04-30 17:14:11

*/

 

class apiServer

{

/**

* 客户端请求的方式

* @var string

*/

private $method = '';

 

/**

* 客户端发送的数据

* @var [type]

*/

protected $param;

 

/**

* 要操作的资源

* @var [type]

*/

protected $resourse;

 

/**

* 要操作的资源id

* @var [type]

*/

protected $resourseId;

 

 

/**

* 构造函数, 获取client 请求的方式,以及传输的数据

* @param object 可以自定义传入的对象

*/

public function __construct()

{

//首先对客户端的请求进行验证

$this->authorization();

 

$this->method = strtolower($_SERVER['REQUEST_METHOD']);

 

//所有的请求都是pathinfo模式

$pathinfo = $_SERVER['PATH_INFO'];

 

//将pathinfo数据信息映射为实际请求方法

$this->getResourse($pathinfo);

 

//获取传输的具体参数

$this->getData();

 

//执行响应

$this->doResponse();

}

 

/**

* 根据不同的请求方式,获取数据

* @return [type]

*/

private function doResponse(){

switch ($this->method) {

case 'get':

$this->_get();

break;

case 'post':

$this->_post();

break;

case 'delete':

$this->_delete();

break;

case 'put':

$this->_put();

break;

default:

$this->_get();

break;

}

}

 

// 将pathinfo数据信息映射为实际请求方法

private function getResourse($pathinfo){

 

/**

* 将pathinfo数据信息映射为实际请求方法

* GET /users: 逐页列出所有用户;

* POST /users: 创建一个新用户;

* GET /users/123: 返回用户为123的详细信息;

* PUT /users/123: 更新用户123;

* DELETE /users/123: 删除用户123;

*

* 根据以上规则,将pathinfo第一个参数映射为需要操作的数据表,

* 第二个参数映射为操作的id

*/

 

$info = explode('/', ltrim($pathinfo, '/'));

list($this->resourse, $this->resourseId) = $info;

}

 

/**

* 验证请求

*/

private function authorization(){

$token = $_SERVER['HTTP_CLIENT_TOKEN'];

$authorization = md5(substr(md5($token), 8, 24).$token);

if($authorization != $_SERVER['HTTP_CLIENT_CODE']){

//验证失败,输出错误信息给客户端

$this->outPut($status = 1);

}

}

 

/**

* [getData 获取传送的参数信息]

* @param [type] $pad [description]

* @return [type] [description]

*/

private function getData(){

//所有的参数都是get传参

$this->param = $_GET;

}

 

/**

* 获取资源操作

* @return [type] [description]

*/

protected function _get(){

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 新增资源操作

* @return [type] [description]

*/

protected function _post(){

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 删除资源操作

* @return [type] [description]

*/

protected function _delete(){

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 更新资源操作

* @return [type] [description]

*/

protected function _put(){

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 出入服务端返回的数据信息 json格式

*/

public function outPut($stat, $data=array()){

$status = array(

//0 状态表示请求成功

0 => array(

'code' => 1,

'info' => '请求成功',

'data' =>$data

),

//验证失败

1 => array(

'code' => 0,

'info' => '请求不合法'

)

);

 

try{

if(!in_array($stat, array_keys($status))){

throw new Exception('输入的状态码不合法');

}else{

echo json_encode($status[$stat]);

}

}catch (Exception $e){

die($e->getMessage());

}

}

}

  ApiClient.php

  ?

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

191

192

193

194

195

 

/**

* Created by PhpStorm.

* User: anziguoer@sina.com

* Date: 2015/4/29

* Time: 12:36

* link: http://www.ruanyifeng.com/blog/2014/05/restful_api.html [restful设计指南]

*/

/*** * * * * * * * * * * * * * * * * * * * * * * * * * ***\

* 定义路由的请求方式 *

* *

* $url_model=0 *

* 采用传统的URL参数模式 *

* http://serverName/appName/?m=module&a=action&id=1 *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* PATHINFO模式(默认模式) *

* 设置url_model 为1 *

* http://serverName/appName/module/action/id/1/ *

** * * * * * * * * * * * * * * * * * * * * * * * * * * **

*/

class restClient

{

//请求的token

const token='yangyulong';

 

//请求url

private $url;

 

//请求的类型

private $requestType;

 

//请求的数据

private $data;

 

//curl实例

private $curl;

 

public $status;

 

private $headers = array();

/**

* [__construct 构造方法, 初始化数据]

* @param [type] $url 请求的服务器地址

* @param [type] $requestType 发送请求的方法

* @param [type] $data 发送的数据

* @param integer $url_model 路由请求方式

*/

public function __construct($url, $data = array(), $requestType = 'get') {

 

//url是必须要传的,并且是符合PATHINFO模式的路径

if (!$url) {

return false;

}

$this->requestType = strtolower($requestType);

$paramUrl = '';

// PATHINFO模式

if (!empty($data)) {

foreach ($data as $key => $value) {

$paramUrl.= $key . '=' . $value.'&';

}

$url = $url .'?'. $paramUrl;

}

 

//初始化类中的数据

$this->url = $url;

 

$this->data = $data;

try{

if(!$this->curl = curl_init()){

throw new Exception('curl初始化错误:');

};

}catch (Exception $e){

echo '

';
            <p>print_r($e->getMessage());</p>
            <p>echo '</p>
';

}

 

curl_setopt($this->curl, CURLOPT_URL, $this->url);

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

 

}

 

/**

* [_post 设置get请求的参数]

* @return [type] [description]

*/

public function _get() {

 

}

 

/**

* [_post 设置post请求的参数]

* post 新增资源

* @return [type] [description]

*/

public function _post() {

 

curl_setopt($this->curl, CURLOPT_POST, 1);

 

curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data);

 

}

 

/**

* [_put 设置put请求]

* put 更新资源

* @return [type] [description]

*/

public function _put() {

 

curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');

}

 

/**

* [_delete 删除资源]

* delete 删除资源

* @return [type] [description]

*/

public function _delete() {

curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');

 

}

 

/**

* [doRequest 执行发送请求]

* @return [type] [description]

*/

public function doRequest() {

//发送给服务端验证信息

if((null !== self::token) && self::token){

$this->headers = array(

'Client_Token: '.self::token,

'Client_Code: '.$this->setAuthorization()

);

}

 

//发送头部信息

$this->setHeader();

 

//发送请求方式

switch ($this->requestType) {

case 'post':

$this->_post();

break;

 

case 'put':

$this->_put();

break;

 

case 'delete':

$this->_delete();

break;

 

default:

curl_setopt($this->curl, CURLOPT_HTTPGET, TRUE);

break;

}

//执行curl请求

$info = curl_exec($this->curl);

 

//获取curl执行状态信息

$this->status = $this->getInfo();

return $info;

}

 

/**

* 设置发送的头部信息

*/

private function setHeader(){

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

}

 

/**

* 生成授权码

* @return string 授权码

*/

private function setAuthorization(){

$authorization = md5(substr(md5(self::token), 8, 24).self::token);

return $authorization;

}

/**

* 获取curl中的状态信息

*/

public function getInfo(){

return curl_getinfo($this->curl);

}

 

/**

* 关闭curl连接

*/

public function __destruct(){

curl_close($this->curl);

}

}

  testClient.php

  ?

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

/**

* Created by PhpStorm.

* User: anziguoer@sina.com

* Date: 2015/4/29

* Time: 12:35

*/

 

include './ApiClient.php';

 

$arr = array(

'user' => 'anziguoer',

'passwd' => 'yangyulong'

);

// $url = 'http://localhost/restAPI/restServer.php';

$url = 'http://localhost/restAPI/testServer.php/user/123';

 

$rest = new restClient($url, $arr, 'get');

$info = $rest->doRequest();

 

//获取curl中的状态信息

$status = $rest->status;

echo '

';
            <p>print_r($info);</p>
            <p>echo '</p>
';

  testServer.php

  ?

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

/**

* @Author: anziguoer@sina.com

* @Email: anziguoer@sina.com

* @link: https://git.oschina.net/anziguoer

* @Date: 2015-04-30 16:52:53

* @Last Modified by: yangyulong

* @Last Modified time: 2015-04-30 17:26:37

*/

 

include './ApiServer.php';

 

class testServer extends apiServer

{

/**

* 先执行apiServer中的方法,初始化数据

* @param object $obj 可以传入的全局对象[数据库对象,框架全局对象等]

*/

 

private $obj;

 

function __construct()//object $obj

{

parent::__construct();

//$this->obj = $obj;

//$this->resourse; 父类中已经实现,此类中可以直接使用

//$tihs->resourseId; 父类中已经实现,此类中可以直接使用

}

 

/**

* 获取资源操作

* @return [type] [description]

*/

protected function _get(){

echo "get";

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 新增资源操作

* @return [type] [description]

*/

protected function _post(){

echo "post";

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 删除资源操作

* @return [type] [description]

*/

protected function _delete(){

//逻辑代码根据自己实际项目需要实现

}

 

/**

* 更新资源操作

* @return [type] [description]

*/

protected function _put(){

echo "put";

//逻辑代码根据自己实际项目需要实现

}

}

 

$server = new testServer();

  以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/998362.htmlTechArticlephp基于curl扩展制作跨平台的restfule 接口 这篇文章主要介绍了php基于curl扩展制作跨平台的restfule 接口的相关资料以及详细的代码,有需要的...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

您应该多久再生一次会话ID?您应该多久再生一次会话ID?Apr 23, 2025 am 12:03 AM

会话ID应在登录时、敏感操作前和每30分钟定期重新生成。1.登录时重新生成会话ID可防会话固定攻击。2.敏感操作前重新生成提高安全性。3.定期重新生成降低长期利用风险,但需权衡用户体验。

如何在PHP中设置会话cookie参数?如何在PHP中设置会话cookie参数?Apr 22, 2025 pm 05:33 PM

在PHP中设置会话cookie参数可以通过session_set_cookie_params()函数实现。1)使用该函数设置参数,如过期时间、路径、域名、安全标志等;2)调用session_start()使参数生效;3)根据需求动态调整参数,如用户登录状态;4)注意设置secure和httponly标志以提升安全性。

在PHP中使用会议的主要目的是什么?在PHP中使用会议的主要目的是什么?Apr 22, 2025 pm 05:25 PM

在PHP中使用会话的主要目的是维护用户在不同页面之间的状态。1)会话通过session_start()函数启动,创建唯一会话ID并存储在用户cookie中。2)会话数据保存在服务器上,允许在不同请求间传递数据,如登录状态和购物车内容。

您如何在子域中分享会议?您如何在子域中分享会议?Apr 22, 2025 pm 05:21 PM

如何在子域名间共享会话?通过设置通用域名的会话cookie实现。1.在服务器端设置会话cookie的域为.example.com。2.选择合适的会话存储方式,如内存、数据库或分布式缓存。3.通过cookie传递会话ID,服务器根据ID检索和更新会话数据。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版