公司安装智能门锁,采用阔道物联的超级锁 P91,根据接口文档,整理了 PHP 版的 SDK
github: https://github.com/misswell/Kdware
gitee: https://gitee.com/guofengio/Kdware
题有问题暂且不说,说一下解题思路,未总结成方法
<?php
$dir = [
'0'=>['','主目录'],
'1'=>['0','第一级目录1'],
'2'=>['0','第一级目录2'],
'3'=>['0','第一级目录3'],
'4'=>['','主目录2'],
'5'=>['1','第二级目录1'],
];
foreach($dir as $k => &$v){
$v['id']= $k;
$v['pid'] = $v[0];
$v['title'] = $v[1];
unset($dir[$k][0],$dir[$k][1]);
}
echo '<pre>';
print_r($dir);
echo '</pre>';
#递归方法实现无限极分类
function getTree($list,$pid=0,$level=0) {
static $tree = array();
foreach($list as $row) {
if($row['pid'] !== ''){
if($row['pid']==$pid) {
$row['level'] = $level;
$tree[] = $row;
getTree($list, $row['id'], $level + 1);
}
}elseif($row['pid'] === '' && $level == 0){
$tree[] = $row;
}
}
return $tree;
}
$dir = getTree($dir);
echo '<pre>';
print_r($dir);
echo '</pre>';
foreach($dir as $kk => $vv){
if($vv['level'] !== NULL){
$rs[] = str_repeat('--',$vv['level']+1).$vv['title'];
}else{
$rs[] = $vv['title'];
}
}
echo '<pre>';
print_r($rs);
echo '</pre>';
结果是:
Array
(
[0] => Array
(
[id] => 0
[pid] =>
[title] => 主目录
)
[1] => Array
(
[id] => 1
[pid] => 0
[title] => 第一级目录1
)
[2] => Array
(
[id] => 2
[pid] => 0
[title] => 第一级目录2
)
[3] => Array
(
[id] => 3
[pid] => 0
[title] => 第一级目录3
)
[4] => Array
(
[id] => 4
[pid] =>
[title] => 主目录2
)
[5] => Array
(
[id] => 5
[pid] => 1
[title] => 第二级目录1
)
)
Array
(
[0] => Array
(
[id] => 0
[pid] =>
[title] => 主目录
)
[1] => Array
(
[id] => 1
[pid] => 0
[title] => 第一级目录1
[level] => 0
)
[2] => Array
(
[id] => 5
[pid] => 1
[title] => 第二级目录1
[level] => 1
)
[3] => Array
(
[id] => 2
[pid] => 0
[title] => 第一级目录2
[level] => 0
)
[4] => Array
(
[id] => 3
[pid] => 0
[title] => 第一级目录3
[level] => 0
)
[5] => Array
(
[id] => 4
[pid] =>
[title] => 主目录2
)
)
Array
(
[0] => 主目录
[1] => --第一级目录1
[2] => ----第二级目录1
[3] => --第一级目录2
[4] => --第一级目录3
[5] => 主目录2
难点在于相较于一般的无限级分类,多了一级 pid == ‘’ ,这一级单独判断。
cd ~/Downloads git clone https://github.com/phpredis/phpredis.git cd phpredis /home/misswell/soft/php/bin/phpize ./configure --with-php-config=/home/misswell/soft/php/bin/php-config make -j make install
在 php.ini 里添加扩展
vim /home/misswell/soft/php/lib/php.ini
extension=redis
cd ~/Downloads git clone https://gitee.com/swoole/swoole.git cd swoole /home/misswell/soft/php/bin/phpize ./configure --with-php-config=/home/misswell/soft/php/bin/php-config make -j make install
或加上异步 redis
cd ~/Downloads git clone https://gitee.com/swoole/swoole.git cd swoole /home/misswell/soft/php/bin/phpize ./configure --with-php-config=/home/misswell/soft/php/bin/php-config --enable-async-redis make clean make -j make install
返回信息
... Installing shared extensions: /home/misswell/soft/php/lib/php/extensions/no-debug-non-zts-20170718/ Installing header files: /home/misswell/soft/php/include/php/
swoole 使用 异步redis的前置条件
安装 redis
下载 redis 地址: https://redis.io
cd ~/Downloads tar -zxvf redis-4.0.9.tar.gz cd redis-4.0.9 make
启动服务:
cd redis-4.0.9/src ./redis-server
hiredis 地址: https://github.com/redis/hiredis/releases
安装 hiredis
cd ~/Downloads wget https://github.com/redis/hiredis/archive/v0.13.3.zip unzip v0.13.3.zip cd hiredis-0.13.3 make -j sudo make install sudo ldconfig
重新编译 hiredis
cd ~/Downloads/swoole/ ./configure --with-php-config=/home/misswell/soft/php/bin/php-config --enable-async-redis make clean make -j make install ... Installing shared extensions: /home/misswell/soft/php/lib/php/extensions/no-debug-non-zts-20170718/ Installing header files: /home/misswell/soft/php/include/php/
php -m 命令查看 Swoole 是否开启成功
php --ri swoole 命令查看 Swoole 异步是否安装成功 async redis client =>enable
安装 PHP7.2.4 curl 扩展:
学习慕课网-Swoole入门到实战打造高性能赛事直播平台-7.9 章节时报错 undefined curl_init(),导致无法发送阿里大鱼短信,以下是报错信息
PHP Fatal error: Uncaught Error: Call to undefined function Aliyun\Core\Http\curl_init() in /home/misswell/Code/thinkphp_5.1.0_rc/extend/ali/lib/Core/Http/HttpHelper.php:13
原因是缺少 curl 扩展
我的安装代码:
misswell@ubuntu:~/Downloads$ wget http://curl.haxx.se/download/curl-7.32.0.tar.gz misswell@ubuntu:~/Downloads$ tar zxvf curl-7.32.0.tar.gz misswell@ubuntu:~/Downloads$ cd curl-7.32.0 misswell@ubuntu:~/Downloads/curl-7.32.0$ ./configure --prefix=/usr/local/curl misswell@ubuntu:~/Downloads/curl-7.32.0$ cd ../php-7.2.4/ext/curl/ misswell@ubuntu:~/Downloads/php-7.2.4/ext/curl$ /home/misswell/soft/php/bin/phpize misswell@ubuntu:~/Downloads/php-7.2.4/ext/curl$ ./configure --with-php-config=/home/misswell/soft/php/bin/php-config --with-curl=/home/misswell/Downloads/curl-7.32.0 misswell@ubuntu:~/Downloads/php-7.2.4/ext/curl$ make && make install ... Installing shared extensions: /home/misswell/soft/php/lib/php/extensions/no-debug-non-zts-20170718/
解释:
php源码目录:/home/misswell/Downloads/php-7.2.4/
php编译目录:/home/misswell/soft/php/
curl源码目录:/home/misswell/Downloads/curl-7.32.0/
1.curl,主要用于发送http请求,是php的一个扩展包。
2.安装过程:
(1)curl下载:http://curl.haxx.se/download.html
(2)具体安装过程:
wget http://curl.haxx.se/download/curl-7.32.0.tar.gz tar zxvf curl-7.32.0.tar.gz cd curl-7.32.0 ./configure --prefix=/usr/local/curl cd /(php源码目录)/ext/curl 运行phpize: /(php编译目录)/bin/phpize ./configure --with-php-config=/(php编译目录)/bin/php-config --with-curl=/(curl源码目录) make && make install 修改php.ini,增加:extension=curl.so(如果没有安装过其他php扩展,则需要同时配置extension_dir="/(php编译后目录)/lib/php/extensions/no-debug-non-zts-20121212/",上一个步骤生成.so文件的时候,会输出extension_dir目录)
(3)检验是否安装成功:
/(php编译后目录)/bin/php -m | grep curl ——正常打印curl,则表示安装成功(php -m会打印出已安装的扩展模块)
PHP 封装一个 api 接口类
使用:
$this->apiSuccess('返回成功', ['message'=>'yes']);
返回结果:
{
"info": "发布话题成功",
"data": {
"message": "yes"
},
"code": 200
}
调用:
$this->apiError('返回失败');
返回结果:
{
"info":"返回失败",
"code":400
}
代码如下:
<?php
class MY_Controller extends CI_Controller
{
// 当前请求类型
protected $_method = '';
// 当前请求的资源类型
protected $_type = '';
// REST允许的请求类型列表
protected $allowMethod = array('get', 'post', 'put', 'delete');
// REST默认请求类型
protected $defaultMethod = 'get';
// REST允许请求的资源类型列表
protected $allowType = array('html', 'xml', 'json', 'rss');
// 默认的资源类型
protected $defaultType__EXT__ = 'html';
// REST允许输出的资源类型列表
protected $allowOutputType = array(
'xml' => 'application/xml',
'json' => 'application/json',
'html' => 'text/html',
);
/**
* 架构函数
* @access public
*/
public function __construct()
{
parent::__construct();
$this->_type = $this->getAcceptType();
// 请求方式检测
$method = strtolower($_SERVER['REQUEST_METHOD']);
if (!in_array($method, $this->allowMethod)) {
// 请求方式非法 则用默认请求方法
$method = $this->defaultMethod;
}
$this->_method = $method;
}
/**
* 获取当前请求的Accept头信息
* @return string
*/
protected function getAcceptType()
{
$type = array(
'html' => 'text/html,application/xhtml+xml,*/*',
'xml' => 'application/xml,text/xml,application/x-xml',
'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
'js' => 'text/javascript,application/javascript,application/x-javascript',
'css' => 'text/css',
'rss' => 'application/rss+xml',
'yaml' => 'application/x-yaml,text/yaml',
'atom' => 'application/atom+xml',
'pdf' => 'application/pdf',
'text' => 'text/plain',
'png' => 'image/png',
'jpg' => 'image/jpg,image/jpeg,image/pjpeg',
'gif' => 'image/gif',
'csv' => 'text/csv'
);
foreach ($type as $key => $val) {
$array = explode(',', $val);
foreach ($array as $k => $v) {
if (isset($_SERVER['HTTP_ACCEPT']) && stristr($_SERVER['HTTP_ACCEPT'], $v)) {
return $key;
}else{
return false;
}
}
}
return false;
}
// 发送Http状态信息
protected function sendHttpStatus($code)
{
static $_status = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Moved Temporarily ', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
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',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
if (isset($_status[$code])) {
header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
// 确保FastCGI模式下正常
header('Status:' . $code . ' ' . $_status[$code]);
}else{
// 找不到code码时为400
$code = 400;
header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
// 确保FastCGI模式下正常
header('Status:' . $code . ' ' . $_status[$code]);
}
}
/**
* 编码数据
* @access protected
* @param mixed $data 要返回的数据
* @param String $type 返回类型 JSON XML
* @return void
*/
protected function encodeData($data, $type = '')
{
if (empty($data)) return '';
if ('json' == $type) {
// 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
} elseif ('xml' == $type) {
// 返回xml格式数据
$data = xml_encode($data);
} elseif ('php' == $type) {
$data = serialize($data);
}
// 默认直接输出
$this->setContentType($type);
header('Content-Length: ' . strlen($data));
return $data;
}
/**
* 设置页面输出的CONTENT_TYPE和编码
* @access public
* @param string $type content_type 类型对应的扩展名
* @param string $charset 页面输出编码
* @return void
*/
public function setContentType($type, $charset = '')
{
if (headers_sent()) return;
if (empty($charset)) $charset = 'utf-8';
$type = strtolower($type);
if (isset($this->allowOutputType[$type])) //过滤content_type
header('Content-Type: ' . $this->allowOutputType[$type] . '; charset=' . $charset);
}
/**
* 输出返回数据
* @access protected
* @param mixed $data 要返回的数据
* @param String $type 返回类型 JSON XML
* @param integer $code HTTP状态
* @return void
*/
protected function response($data, $type = '', $code = 200)
{
$this->sendHttpStatus($code);
exit($this->encodeData($data, strtolower($type)));
}
protected function aresponse($data, $type = '', $code = 200)
{
$this->asendHttpStatus($code);
exit($this->encodeData($data, strtolower($type)));
}
/**
* apiReturn api返回方法,约定 。int 是 code。string 是 Info,array 是 data
* 其中array型可以传多个,第一个返回参数名为data,其余的按照data_ + 调用时的位置 命名
*/
protected function apiReturn()
{
$code = 200;
$args = func_get_args();
$this->apiResponse($args,$code);
}
protected function apiSuccess(){
$args = func_get_args();
$code = 200;
$this->apiResponse($args,$code);
}
protected function apiError(){
$args = func_get_args();
$code = 400;
$this->apiResponse($args,$code);
}
protected function apiResponse($args,$code=200){
$rs = array();
foreach ($args as $key => $v) {
if (is_array($v)) {
if (isset($rs['data'])) {
$rs['data_'.$key] = $v;
}else{
$rs['data'] = $v;
}
} elseif (is_int($v)) {
$code = $v;
} else {
$rs['info'] = $v;
}
}
$rs['code'] = $code;
$this->response($rs, 'json', $code);
}
}
PHP 中 json_encode() 只将索引数组(indexed array)转为数组格式,而将关联数组(associative array)转为对象格式。
$arr = array(
'0'=>'a','1'=>'b','2'=>'c','3'=>'d'
);
echo json_encode($arr);
结果是:
["a","b","c","d"]
而不是
{"0":"a","1":"b","2":"c","3":"d"}
强制转成对象
$arr = array(
'0'=>'a','1'=>'b','2'=>'c','3'=>'d'
);
echo json_encode((object)$arr);
输出结果:
{"0":"a","1":"b","2":"c","3":"d"}
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; $url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; echo $url;
另:打印 $_SERVER
Continue reading PHP 判断当前地址是 HTTP 还是 HTTPS