我是靠谱客的博主 洁净小笼包,这篇文章主要介绍php的一个简单restful的BaseController,现在分享给大家,希望可以做个参考。

这里写自定义目录标题

复制代码
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
<?php class BaseController { private $httpVersion = "HTTP/1.1"; private $pdo = null; public function getPdo() { if (null == $this->pdo) { $dbms = 'mysql'; //数据库类型 $dbName = 'qbit'; //使用的数据库 $host = 'localhost'; //数据库主机名 $user = 'root'; //数据库连接用户名 $pass = ''; //对应的密码 $dsn = "$dbms:host=$host;dbname=$dbName"; $this->pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true)); } return $this->pdo; } public function doQuery($sql) { $rawData = $this->getPdo()->query($sql); if (true || empty($rawData)) { $statusCode = 404; $rawData = array('error' => 'No sites found!'); } else { $statusCode = 200; } $requestContentType = $_SERVER['HTTP_ACCEPT']; $this->setHttpHeaders($requestContentType, $statusCode); if (strpos($requestContentType, 'application/json') !== false) { $response = $this->encodeJson($rawData); echo $response; } else if (strpos($requestContentType, 'text/html') !== false) { $response = $this->encodeHtml($rawData); echo $response; } else if (strpos($requestContentType, 'application/xml') !== false) { $response = $this->encodeXml($rawData); echo $response; } } public function doCommand($sqls){ $pdo=$this->getPdo(); $pdo->beginTransaction(); try { foreach ($sqls as $sql) { $pdo->exec($sql); } $pdo->commit(); }catch (Exception $e){ $pdo->rollBack(); echo 'Message: ' .$e->getMessage(); } } public function setHttpHeaders($contentType, $statusCode) { $statusMessage = $this->getHttpStatusMessage($statusCode); header($this->httpVersion . " " . $statusCode . " " . $statusMessage); header("Content-Type:" . $contentType); } public function getHttpStatusMessage($statusCode) { $httpStatus = array( 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => '(Unused)', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported'); return ($httpStatus[$statusCode]) ? $httpStatus[$statusCode] : $httpStatus[500]; } public function encodeHtml($responseData) { $htmlResponse = "<table border='1'>"; foreach($responseData as $key=>$value) { $htmlResponse .= "<tr><td>". $key. "</td><td>". $value. "</td></tr>"; } $htmlResponse .= "</table>"; return $htmlResponse; } public function encodeJson($responseData) { return json_encode($responseData); } public function encodeXml($responseData) { // 创建 SimpleXMLElement 对象 $xml = new SimpleXMLElement('<?xml version="1.0"?><site></site>'); foreach($responseData as $key=>$value) { $xml->addChild($key, $value); } return $xml->asXML(); } }

最后

以上就是洁净小笼包最近收集整理的关于php的一个简单restful的BaseController的全部内容,更多相关php内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部