1.MVC控制器类的访问与参数解析

  1. namespace demomvc;
  2. class MoneyController
  3. {
  4. public function shop($name,$ye)
  5. {
  6. return '商品:'. $name . ',优惠'. $ye. '元';
  7. }
  8. }
  9. echo $_SERVER['PATH_INFO'];
  10. // 分割过滤成数组
  11. $pathinfo = array_filter(explode('/',$_SERVER['PATH_INFO']));
  12. // 解析控制器
  13. $controller = __NAMESPACE__ .'\\' . array_shift($pathinfo) . 'Controller';
  14. // 解析方法
  15. $action = array_shift($pathinfo);
  16. // 从url中解析出参数
  17. printf('<pre>%s</pre>',print_r($pathinfo,true));
  18. // 将参数保存在params数组中
  19. $params = [];
  20. for ($i = 0; $i<count($pathinfo);$i += 2) {
  21. if (isset($pathinfo[$i+1]))
  22. $params[$pathinfo[$i]] = $pathinfo[$i+1];
  23. }
  24. printf('<pre>%s</pre>',print_r($params,true));
  25. echo '<hr>';
  26. // 输出结果
  27. echo call_user_func_array([(new $controller),$action],$params);

2.通过用户输入城市名查询天气并渲染出来

  1. <?php
  2. // 秘钥
  3. $key = 'key';
  4. // 请求地址
  5. $url = 'http://apis.juhe.cn/simpleWeather/query?';
  6. $serch = $_GET['serch'];
  7. // var_dump($serch);
  8. // 城市
  9. $city = $serch;
  10. // 构造查询参数
  11. $query = http_build_query(['key'=>$key,'city'=>$city]);
  12. // CURL:发起HTTP请求
  13. $cx = curl_init();
  14. // 设置请求的url完整地址
  15. curl_setopt($cx,CURLOPT_URL,$url.$query);
  16. // 设置请求类型为get
  17. curl_setopt($cx,CURLOPT_HTTPGET,true);
  18. // 去掉头信息
  19. curl_setopt($cx,CURLOPT_HEADER,false);
  20. // 默认是浏览器输出,只返回不输出
  21. curl_setopt($cx,CURLOPT_RETURNTRANSFER,true);
  22. $api = curl_exec($cx);
  23. // echo $api;
  24. curl_close($cx);
  25. ?>
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8">
  30. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <title>天气预报</title>
  33. <style>
  34. table {
  35. color: #555;
  36. background-color: #efefef;
  37. border-collapse: collapse;
  38. width: 600px;
  39. text-align: center;
  40. margin: auto;
  41. }
  42. td {
  43. border: 2px solid #FFF;
  44. padding: 5px;
  45. }
  46. table caption {
  47. font-size: 1.2rem;
  48. margin-bottom: 15px;
  49. }
  50. table thead tr:first-of-type {
  51. background-color: darkturquoise;
  52. color: white;
  53. }
  54. #serch {
  55. text-align: center;
  56. padding: 20px;
  57. }
  58. #serch input {
  59. outline: none;
  60. border-radius: 10px;
  61. padding: 10px;
  62. border:1px solid lightcoral
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <form action="" method="get" id="serch">
  68. 查询:<input type="text" name="serch" value="" placeholder="请输入要查询的城市名" />
  69. </form>
  70. <script>
  71. // 获取天气数据
  72. const obj = <?=$api?>;
  73. // 创建表格元素
  74. const table = document.createElement('table');
  75. // 创建表头:城市名+标题
  76. table.createCaption().textContent = obj.result.city + '天气预报';
  77. // 创建表头,并尾部添加新行,将参数填入
  78. const tr = table.createTHead().insertRow(-1);
  79. tr.insertCell(0).innerText = '日期';
  80. tr.insertCell(1).innerText = '气温';
  81. tr.insertCell(2).innerText = '雨雪';
  82. tr.insertCell(3).innerText = '风向';
  83. // 遍历未来几天天气对象数组
  84. obj.result.future.forEach(item=>{
  85. // 先生成一个新行,并插入到尾部
  86. const row = table.insertRow(-1);
  87. let date = new Date(Date.parse(item.date.replace(/-/g,'/')));
  88. // 组装成符合国人阅读习惯的格式
  89. let timeStr = `${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()}日`;
  90. // 遍历每一天的天气对象数组,并填充到生成的单元格中
  91. row.insertCell(0).innerText = timeStr;
  92. row.insertCell(1).innerText = item.temperature;
  93. row.insertCell(2).innerText = item.weather;
  94. row.insertCell(3).innerText = item.direct;
  95. // 将生成的插入到表格元素中
  96. table.appendChild(row);
  97. });
  98. // 将表格添加到页面中
  99. document.body.appendChild(table);
  100. </script>
  101. </body>
  102. </html>

更多相关文章

  1. python关于range函数总结
  2. JavaEE面试题总结,一篇文章带你攻克面试难题
  3. 一文读懂HTTP常见状态码
  4. 高频数据采集请求如何不影响主业务(7)
  5. PHP扩展知识:URL相关函数和api接口案例
  6. 笔记 | Python之函数式编程
  7. Nginx负载均衡配置误区
  8. HTTP概述
  9. Nginx Ingress的一些奇巧淫技

随机推荐

  1. Android NDK环境搭建
  2. Android(安卓)Lifecycle 生命周期管理
  3. HelloWorld-----Google手机操作系统Andro
  4. android客户端与服务端交互的三种方式
  5. android browser 的几个小feature (一)
  6. Android(安卓)地区语言和简写对照表
  7. 视频教程-Android Material Design 新控
  8. 不疯狂!非正常!
  9. Android 应用架构组件(Architecture Compo
  10. android studio 使用android:drawableTop