Javascript 如何获取最新提交日期-Github API
我目前正在编写一个git代表的小数据库,我想知道如果我的数据库中列出了代表,我将如何继续并获得最新提交的日期 我从未使用过github API,我对它有点费劲Javascript 如何获取最新提交日期-Github API,javascript,php,github-api,Javascript,Php,Github Api,我目前正在编写一个git代表的小数据库,我想知道如果我的数据库中列出了代表,我将如何继续并获得最新提交的日期 我从未使用过github API,我对它有点费劲 如果有人能帮我弄明白,我会非常感激的。我发现的所有示例都是ruby中的PHP或JS。如果您想像使用PHP一样使用PHP,我会使用而不是,因为您需要配置 GitHub还要求您在标题中发送用户代理: 例如,您的PHP代码如下所示 $objCurl = curl_init(); //The repo we want to get curl_s
如果有人能帮我弄明白,我会非常感激的。我发现的所有示例都是ruby中的PHP或JS。如果您想像使用PHP一样使用PHP,我会使用而不是,因为您需要配置 GitHub还要求您在标题中发送
用户代理
:
例如,您的PHP代码如下所示
$objCurl = curl_init();
//The repo we want to get
curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");
//To comply with https://developer.github.com/v3/#user-agent-required
curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346");
//Skip verification (kinda insecure)
curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);
//Get the response
$response = curl_exec($objCurl);
print_r( json_decode($response, true) );
注意:您可以继续使用
文件获取内容
并发送用户代理
标题。请参见旧问题,但我想指出(至少在api v3中),您可以使用分支api获取特定分支的最新提交日期。我想在你的情况下,你对大师感兴趣
这看起来像:
https://api.github.com/repos/:owner/:repo/branches/master
请参见我想回答这个确切的问题,因此我制作了一个非常小的演示,演示如何获取最新提交的日期 输出:
ta-dachi
master
2019-03-21T14:50:22Z <----- What you want
b80126c3ea900cd7c92729e652b2e8214ff014d8
https://github.com/ta-dachi/eatsleepcode.tech/tree/master
ta-dachi
主人
2019-03-21T14:50:22Z{
response.json().then(json=>{
log(json);
这是我的国家({
作者:json.commit.author.login,
分支:json.name,
日期:json.commit.commit.author.date,
sha:json.commit.sha,
link:json.\u links.html
});
});
})
.catch(错误=>{
console.log(错误);
});
}
render(){
返回(
{this.state.author}
{this.state.branch}
{this.state.date}
{this.state.sha}
{this.state.link}
);
}
}
render(,document.getElementById(“根”));
这是您将要使用的api。你能展示一下你尝试过的,并解释一下什么不起作用吗?Github有一个所谓的“RESTful API”,很多在线教程都是针对这种API的——如果你不知道如何对此类服务进行民意调查,最好是你学习一个教程,教授提出此类请求的基本原理。这就是我尝试过的:$url=”$obj=json_decode(file_get_contents($url),true);我得到的反馈是:无法打开流:HTTP请求失败!HTTP/1.0 403禁止您将需要执行:curl-X GET-H“缓存控制:无缓存”https://api.github.com/repos///commits
Aight,它可以在我的实时服务器上工作,但不能在我上面写的本地主机上工作,然而,我数据库中的一些链接潜入了它们的文件夹中,而这些文件夹正是我希望看到更新的文件夹,例如:所以我的前一行不起作用。有什么想法吗?谢谢,既然json对象不是数组,我该如何在php中导航它呢?从json_decode中删除,true
,使其成为一个对象。我需要添加curl_setopt($objCurl,CURLOPT_RETURNTRANSFER,true)
停止curl自动输出响应<代码>$response=1在此添加之前。请更新您的代码<代码>$aCommit=json\u解码($sInfo,false)$sTimeLatest=$aCommit[0]->commit->author->date$iTimeLatest=STROTIME($sTimeLatest)我在这里读这些评论之前浪费了15分钟!可能很明显,但是您的repo必须是公共的,GithubAPI才能通过简单的获取请求访问它。
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>React Local</title>
<script
type="application/javascript"
src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/@babel/standalone/babel.min.js"
></script>
<script
type="application/javascript"
src="https://unpkg.com/whatwg-fetch@3.0.0/dist/fetch.umd.js"
></script>
</head>
<body>
<div id="root"></div>
<script type="text/jsx" src="index.jsx"></script>
</body>
/**
* See https://developer.github.com/v3/repos/branches/#get-branch
*
* Example Github api request:
* https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master
*/
class LatestCommitComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
author: "",
branch: "",
date: "",
sha: "",
link: ""
};
}
componentDidMount() {
// Replace this with your own repo
// https://api.github.com/repos/:owner/:repo/branches/master
fetch(
"https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master"
)
.then(response => {
response.json().then(json => {
console.log(json);
this.setState({
author: json.commit.author.login,
branch: json.name,
date: json.commit.commit.author.date,
sha: json.commit.sha,
link: json._links.html
});
});
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<div>
<div>{this.state.author}</div>
<div>{this.state.branch}</div>
<div>{this.state.date}</div>
<div>{this.state.sha}</div>
<div>{this.state.link}</div>
</div>
);
}
}
ReactDOM.render(<LatestCommitComponent />, document.getElementById("root"));