Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
创建控制器Start.php
<?php
namespace controllers;
class Start
{
public static function int()
{
$pathinfo = array_filter(explode('/', $_SERVER['PATH_INFO']));
//生成控制器
$controller = __NAMESPACE__ . '\\' . ucfirst(array_shift($pathinfo)).'Controller';
$action = array_shift($pathinfo);
//将url中的参数解析出来
$params = [];
for ($i = 0; $i < count($pathinfo); $i += 2) {
if (isset($pathinfo[$i + 1]))
$params[$pathinfo[$i]] = $pathinfo[$i + 1];
}
echo call_user_func_array([(new $controller), $action], $params);
}
}
入口文件index.php
<?php
//入口文件
use controllers\Start;
require __DIR__.'/vendor/autoload.php';
Start::int();
通过pactinfo访问user控制器中的select方法;http://127.0.0.119/pdo/ifram/index.php/user/select
运行结果图
<?php
// https://api.apiopen.top/getJoke?page=1&count=5&type=video
$ustring = 'https://api.apiopen.top/getJoke?';
$page = 1;
$count = 5;
$type = 'video';
$query = http_build_query(['page' => $page, 'count' => $count, 'type' => $type]);
$url = $ustring . $query;
// 发起一个http请求
$api = curl($url);
function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$api = curl_exec($ch);
curl_close($ch);
return $api;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th>
<th>标题</th>
<th>作者</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
<script>
const api = <?= $api ?>;
let str='';
$.each(api.result,(k,i)=>{
str += '<tr>';
str += '<td>'+i['sid']+'</td>';
str += '<td>'+i['text']+'</td>';
str += '<td>'+i['name']+'</td>';
str +='</tr>';
})
$('tbody').html(str);
</script>
</html>