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

时间:2014-10-09 16:56:43

标签: php git github

我正在尝试使用github api将私有Github存储库下载到我的网络服务器。

与我的存储库的连接成功。 但是当我尝试下载文件时,我得到警告: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
)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这比我想象的容易得多。

这里是解决方案:

$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);