火神项目搜索 - 查找 GitHub 项目
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-top: 50px;
}
form {
margin-top: 50px;
}
input[type=text] {
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: none;
box-shadow: 0 0 5px #ccc;
width: 500px;
max-width: 90%;
margin-right: 10px;
}
input[type=submit], button[type=submit] {
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
border: none;
background-color: #0077b5;
color: #fff;
cursor: pointer;
}
.result {
margin-top: 50px;
text-align: left;
max-width: 90%;
margin-left: auto;
margin-right: auto;
}
.result a {
color: #0077b5;
text-decoration: none;
}
.result a:hover {
text-decoration: underline;
}
.pagination {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
list-style: none;
padding: 0;
}
.pagination li {
margin-right: 10px;
}
.pagination a {
padding: 5px 10px;
border-radius: 5px;
border: 1px solid #0077b5;
color: #0077b5;
text-decoration: none;
}
.pagination a.active {
background-color: #0077b5;
color: #fff;
}
</style>
<pre><code><h1>火神项目搜索</h1>
<form method='get'>
<label for='search'>项目:</label>
<input type='text' name='search' id='search' value='<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>' placeholder='输入关键词搜索'>
<button type='submit'>搜索</button>
</form>
<div class='result'>
<?php
if(isset($_GET['search'])) {
$q = $_GET['search'];
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$url = 'https://api.github.com/search/repositories?q='.urlencode($q).'&page='.$page;
$options = array(
'http' => array(
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$data = json_decode($result);
echo '<p>共搜索到 ' . $data->total_count . ' 个项目:</p>';
foreach ($data->items as $item) {
echo '<p><a href='' . $item->html_url . '' target='_blank'>' . $item->full_name . '</a></p>';
echo '<p>' . $item->description . '</p>';
echo '<hr>';
}
$total_pages = ceil($data->total_count / 30);
if($total_pages > 1) {
echo '<ul class='pagination'>';
if($page > 1) {
echo '<li><a href='?search='.urlencode($q).'&page='.($page - 1).''>上一页</a></li>';
}
for($i = 1; $i <= $total_pages; $i++) {
if($page == $i) {
echo '<li><a href='?search='.urlencode($q).'&page='.$i.'' class='active'>'.$i.'</a></li>';
} else {
echo '<li><a href='?search='.urlencode($q).'&page='.$i.''>'.$i.'</a></li>';
}
}
if($page < $total_pages) {
echo '<li><a href='?search='.urlencode($q).'&page='.($page + 1).''>下一页</a></li>';
}
echo '</ul>';
}
}
?>
</div>
</code></pre>
原文地址: https://www.cveoy.top/t/topic/m4qG 著作权归作者所有。请勿转载和采集!