Blogger Information
Blog 47
fans 3
comment 0
visits 38224
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC控制器类的访问、参数解析、api接口数据获取并渲染
Original
825 people have browsed it

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>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments