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

api github

GitHub is an awesome place to host your open source project code. MooTools, Prototype, and jQuery all use GitHub. As you probably know, the MooTools Forge requires your plugins be hosted on GitHub. The only problem with hosting all my MooTools plugins is that I lose traffic when I want people to see my code. Problem solved: use PHP, the GitHub API, and PHP Markdown to display files of my choice on my website.

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

Our goals with this code will be to:

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

  • Connect to GitHub via the API to retrieve repository information.

    通过API连接到GitHub以检索存储库信息。
  • Retrieve the content of two files from the repository: a source file and the README.md Markdown file.

    从存储库中检索两个文件的内容:源文件和README.md Markdown文件。
  • Cache the information for a given period of time to reduce the load on GitHub.

    在给定的时间段内缓存信息,以减少GitHub上的负载。
  • Use PHP Markdown to output a formatted README.md file.

    使用PHP Markdown输出格式化的README.md文件。

I know that seems like a lot of work but you'll be amazed at how easy the process is.

我知道这似乎需要做很多工作,但是您会惊讶于该过程如此简单。

PHP MarkDown (PHP MarkDown)

You may download PHP Markdown at Michel Fortin's website. It's simple and full of features.

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

PHP (The PHP)

The first step is to build a PHP function that will connect to GitHub using cURL:

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


/* 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;
}


Next we need to define a few settings:

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


/* 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);


The next step is to create another PHP function that grabs the repository information (JSON-encoded, because I love JSON) -- either fresh from GitHub (by first grabbing the most recent commit hash, then grabbing the contents of the two files) or our local cached information:

下一步是创建另一个PHP函数,以获取存储库信息(JSON编码,因为我很喜欢JSON)-从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;
	}
}


Once we've acquired the appropriate information, we output the information to screen:

获取适当的信息后,我们会将信息输出到屏幕:


/* 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 class="js">'.$github_json['js']['blob']['data'].'</pre><br />';
	$content.= trim(str_replace(array('<code>','</code>'),'',Markdown($github_json['readme']['blob']['data'])));
}


That's all! Now I get the benefit of hosting my code on GitHub but displaying it on my own website. I've created a special WordPress template page to do so and recommend you do too!

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

演示? (Demo?)

Visit my Projects page and click on the "Docs" link for any project. All of the information that comes up on individual project pages comes from GitHub. No more manual page creation!

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

翻译自: https://davidwalsh.name/github-markdown

api github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值