【发布时间】:2016-06-14 06:36:24
【问题描述】:
我正在尝试使用 api 从 github 获取最新的提交,但是我遇到了一些错误并且不确定 curl 请求的问题是什么。 CURLINFO_HTTP_CODE 给了我 000。
如果我得到 000 是什么意思,为什么它没有获取 url 的内容?
function get_json($url){
$base = "https://api.github.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base . $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
echo $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $content;
}
echo get_json("users/$user/repos");
function get_latest_repo($user) {
// Get the json from github for the repos
$json = json_decode(get_json("users/$user/repos"),true);
print_r($json);
// Sort the array returend by pushed_at time
function compare_pushed_at($b, $a){
return strnatcmp($a['pushed_at'], $b['pushed_at']);
}
usort($json, 'compare_pushed_at');
//Now just get the latest repo
$json = $json[0];
return $json;
}
function get_commits($repo, $user){
// Get the name of the repo that we'll use in the request url
$repoName = $repo["name"];
return json_decode(get_json("repos/$user/$repoName/commits"),true);
}
【问题讨论】:
-
你有什么问题?
-
所以 URL =
https://api.github.com+users/$user/repos=https://api.github.comusers/$user/repos应该很明显现在的问题是什么。 -
我修复了,但出现 403 错误