如何使用PHP api下载GitHub存储库作为存档


How to download GitHub repository as archive using PHP api?

我正在尝试使用Github api将一个私有Github存储库下载到我的Web服务器上。

连接到我的存储库成功。但当我尝试下载该文件时,我会得到警告:file_get_contents(https://api.github.com/repos/sistecs/sisMedia/tarball/5.1):无法打开流:HTTP请求失败!HTTP/1.1 404未找到

这是我的代码:

$url = 'https://api.github.com/repos/sistecs/sismedia/releases/614885';
$curl_token_auth = 'Authorization: token ' . $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: sistecs', $curl_token_auth));
$content = curl_exec($ch);
$content = json_decode($content,true);
$context = stream_context_create(array('http' => array(
    'header' => 'User-Agent: sistecs',
)));
$Tarball = file_get_contents($content["tarball_url"],false, $context);
$savefile = fopen("sismedia.tar.gz", "w");
fwrite($savefile, $Tarball);
fclose($savefile);
curl_close($ch); 

以下是api调用的返回

Array
(
[url] => https://api.github.com/repos/sistecs/sisMedia/releases/614885
[assets_url] => https://api.github.com/repos/sistecs/sisMedia/releases/614885/assets
[upload_url] => https://uploads.github.com/repos/sistecs/sisMedia/releases/614885/assets{?name}
[html_url] => https://github.com/sistecs/sisMedia/releases/tag/5.1
[id] => 614885
[tag_name] => 5.1
[target_commitish] => master
[name] => v5.1
[draft] => 
[author] => Array
    (
        [login] => sistecs
        [id] => 6322229
        [avatar_url] => https://avatars.githubusercontent.com/u/6322229?v=2
        [gravatar_id] => 
        [url] => https://api.github.com/users/sistecs
        [html_url] => https://github.com/sistecs
        [followers_url] => https://api.github.com/users/sistecs/followers
        [following_url] => https://api.github.com/users/sistecs/following{/other_user}
        [gists_url] => https://api.github.com/users/sistecs/gists{/gist_id}
        [starred_url] => https://api.github.com/users/sistecs/starred{/owner}{/repo}
        [subscriptions_url] => https://api.github.com/users/sistecs/subscriptions
        [organizations_url] => https://api.github.com/users/sistecs/orgs
        [repos_url] => https://api.github.com/users/sistecs/repos
        [events_url] => https://api.github.com/users/sistecs/events{/privacy}
        [received_events_url] => https://api.github.com/users/sistecs/received_events
        [type] => User
        [site_admin] => 
    )
[prerelease] => 
[created_at] => 2014-10-09T14:07:32Z
[published_at] => 2014-10-09T14:23:44Z
[assets] => Array
    (
    )
[tarball_url] => https://api.github.com/repos/sistecs/sisMedia/tarball/5.1
[zipball_url] => https://api.github.com/repos/sistecs/sisMedia/zipball/5.1
[body] => sisMedia 5 inkl. Benutzerverwaltung
)

有人能帮我吗?

这比我想象的要容易得多。

这里的解决方案:

$context = stream_context_create(array('http' => array(
    'header' => 'User-Agent: sistecs',
)));
$File = file_get_contents("https://api.github.com/repos/sistecs/sisMedia/tarball/5.1?access_token=MYTOKEN",false, $context);
$SaveFile = fopen("temp/sismedia.tar.gz", "w");
fwrite($SaveFile, $File);
fclose($SaveFile);