开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 GitHub API 和 PHP 获取存储库信息

GitHub 是托管开源项目代码的绝佳场所。 MooTools、Prototype 和 jQuery 都使用 GitHub。您可能知道,MooTools Forge 要求您的插件托管在 GitHub 上。托管我所有的 MooTools 插件的唯一问题是,当我希望人们看到我的代码时,我会失去流量。问题已解决:使用 PHP、GitHub API 和 PHP Markdown 在我的网站上显示我选择的文件。

我们使用此代码的目标是:

  • 通过 API 连接到 GitHub 以检索存储库信息。
  • 从存储库检索两个文件的内容:源文件和 README.md Markdown 文件。
  • 将信息缓存一段给定的时间以减少 GitHub 上的负载。
  • 使用 PHP Markdown 输出格式化的 README.md 文件。

我知道这看起来工作量很大,但您会惊讶于这个过程是多么简单。

PHP 降价

您可以在 Michel Fortin 的网站上下载 PHP Markdown。它简单且功能齐全。

PHP

第一步是构建一个将使用 cURL 连接到 GitHub 的 PHP 函数:

/* gets url */
function get_content_from_github($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}

接下来我们需要定义一些设置:

/* static settings */
$plugin = 'Overlay';
$cache_path = $_SERVER['DOCUMENT_ROOT'].'/plugin-cache/';
$cache_file = $plugin.'-github.txt';
$github_json = get_repo_json($cache_path.$cache_file,$plugin);

下一步是创建另一个获取存储库信息(JSON 编码,因为我喜欢 JSON)的 PHP 函数——要么是从 GitHub 中获取的(首先获取最新的提交哈希,然后获取两个文件的内容)文件)或我们的本地缓存信息:

/* gets the contents of a file if it exists, otherwise grabs and caches */
function get_repo_json($file,$plugin) {
	//vars
	$current_time = time(); $expire_time = 24 * 60 * 60; $file_time = filemtime($file);
	//decisions, decisions
	if(file_exists($file) && ($current_time - $expire_time < $file_time)) {
		//echo 'returning from cached file';
		return json_decode(file_get_contents($file));
	}
	else {
		$json = array();
		$json['repo'] = json_decode(get_content_from_github('http://github.com/api/v2/json/repos/show/darkwing/'.$plugin),true);
		$json['commit'] = json_decode(get_content_from_github('http://github.com/api/v2/json/commits/list/darkwing/'.$plugin.'/master'),true);
		$json['readme'] = json_decode(get_content_from_github('http://github.com/api/v2/json/blob/show/darkwing/'.$plugin.'/'.$json['commit']['commits'][0]['parents'][0]['id'].'/Docs/'.$plugin.'.md'),true);
		$json['js'] = json_decode(get_content_from_github('http://github.com/api/v2/json/blob/show/darkwing/'.$plugin.'/'.$json['commit']['commits'][0]['parents'][0]['id'].'/Source/'.$plugin.'.js'),true);
		file_put_contents($file,json_encode($json));
		return $content;
	}
}

一旦我们获得了适当的信息,我们就将信息输出到屏幕:

/* build json */
if($github_json) {
	
	//get markdown
	include($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/walshbook3/PHP-Markdown-Extra-1.2.4/markdown.php');
	
	//build content
	$content = '<p>'.$github_json['repo']['repository']['description'].'</p>';
	$content.= '<h2>MooTools JavaScript Class</h2><pre >'.$github_json['js']['blob']['data'].'</pre><br />';
	$content.= trim(str_replace(array('<code>','</code>'),'',Markdown($github_json['readme']['blob']['data'])));
}

就是这样!现在我受益于在 GitHub 上托管我的代码,但在我自己的网站上显示它。为此,我创建了一个特殊的 WordPress 模板页面,并建议您也这样做!

演示?

访问我的项目页面并单击任何项​​目的“文档”链接。各个项目页面上出现的所有信息都来自 GitHub。无需再创建手动页面!

未经允许不得转载:我爱分享网 » 使用 GitHub API 和 PHP 获取存储库信息

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏