HTTP:一种超文本的传输协议,是计算机与计算机沟通的一种标准协议,如今通常为端与端之间的通讯。
一、约定内容php
PHP中的header函数用于设置响应头css
<?php header('content-type:text/html'); ?>
补充:html
<?php header('Location:01.php'); ?>
客户端浏览器在接受到这个头信息后自动跳转到指定地址json
JSON
JSON:相似于js字面量的表达数据的手段bootstrap
JSON数据类型
null:数组
null
string:浏览器
"ssq"
boolean:函数
ture
number:url
12
object:spa
{ "name": "ssq", "age": 12, "gender": ture, "boyfrind": null }
array:
["张三", "李四", "王五"]
JSON基本格式
var obj = [ {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]}, {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]}, {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]}, {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]}, {"name": "ss", "age": 12, "email": "ssss", "url": "sssss.com", "images": ["./images/01.jpg"]} ]
JSON的转换
在php中对JSON反序列化
<?php
$contents = file_get_contents('storage.json');
$data = json_decode($contents, true);
?>
及变成PHP中对象数组的形式
![]()
01实例展现
<?php // 获取文件中记录的数据,并展现到表格中(动态生成表格的HTML标签) $contents = file_get_contents('storage.json'); // $contents => JSON 格式的字符串 // 把 JSON 格式的字符串转换为对象的过程叫作反序列化 // json_decode 默认反序列化时 将 JSON 中的对象转换为 PHP 中 stdClass 类型的对象 $data = json_decode($contents, true); // $data => [] ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>音乐列表</title> <link rel="stylesheet" href="bootstrap.css"> </head> <body> <div class="container py-5"> <h1 class="display-4">音乐列表</h1> <hr> <div class="mb-3"> <a href="add.php" class="btn btn-secondary btn-sm">添加</a> </div> <table class="table table-bordered table-striped table-hover"> <thead class="thead-dark"> <tr> <th class="text-center">标题</th> <th class="text-center">歌手</th> <th class="text-center">海报</th> <th class="text-center">音乐</th> <th class="text-center">操做</th> </tr> </thead> <tbody class="text-center"> <?php foreach ($data as $item): ?> <tr> <td><?php echo $item['title'] ?></td> <td><?php echo $item['artist'] ?></td> <td><img src="<?php echo $item['images[0]'] ?>" alt=""></td> <td><audio src="<?php echo $item['source'] ?>" controls></audio></td> <td><button class="btn btn-danger btn-sm">删除</button></td> </tr> <?php endforeach ?> </tbody> </table> </div> </body> </html>
效果图