<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: 50px;
		}
		.pagination a, .pagination span {
			padding: 10px;
			font-size: 16px;
			border-radius: 5px;
			border: none;
			background-color: #0077b5;
			color: #fff;
			cursor: pointer;
			margin-right: 10px;
		}
		.pagination a:hover {
			background-color: #005e8d;
		}
		.pagination .current {
			background-color: #005e8d;
		}
		@media screen and (max-width: 768px) {
	  .pagination a, .pagination span {
	    font-size: 14px;
	    padding: 5px;
	    margin-right: 5px;
	  }
	}
</style>
<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']) ? intval($_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);
<pre><code>		echo '&lt;p&gt;共搜索到 ' . $data-&gt;total_count . ' 个项目:&lt;/p&gt;';

		foreach ($data-&gt;items as $item) {
			echo '&lt;p&gt;&lt;a href='' . $item-&gt;html_url . '' target='_blank'&gt;' . $item-&gt;full_name . '&lt;/a&gt;&lt;/p&gt;';
			echo '&lt;p&gt;' . $item-&gt;description . '&lt;/p&gt;';
			echo '&lt;hr&gt;';
		}

		$pages = ceil($data-&gt;total_count / 30);
		if($pages &gt; 1) {
			echo '&lt;div class='pagination'&gt;';
			if($page &gt; 1) {
				echo '&lt;a href='?search='.urlencode($q).'&amp;page='.($page - 1).''&gt;上一页&lt;/a&gt;';
			}
			for($i = 1; $i &lt;= $pages; $i++) {
				if($i == $page) {
					echo '&lt;span class='current'&gt;$i&lt;/span&gt;';
				} else {
					echo '&lt;a href='?search='.urlencode($q).'&amp;page=$i'&gt;$i&lt;/a&gt;';
				}
			}
			if($page &lt; $pages) {
				echo '&lt;a href='?search='.urlencode($q).'&amp;page='.($page + 1).''&gt;下一页&lt;/a&gt;';
			}
			echo '&lt;/div&gt;';
		}
	}
?&gt;
</code></pre>
</div>
<?php
火神项目搜索 - 查找 GitHub 上的开源项目

原文地址: https://www.cveoy.top/t/topic/m4gp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录