概述
这里写自定义目录标题
<?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的一个简单restful的BaseController所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复