2014-10-09 45 views
0

正在尝试使用github api将我的网站服务器的私人Github存储库下载到我的网络服务器上。如何使用PHP api将GitHub存储库下载为归档文件?

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

这里IST我的代码:

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

谁能帮助我?

+0

当我尝试直接访问该文件,它也返回未找到。我会怀疑这个文件不在那里,或者这个名字在某种程度上是不同的。 – 2014-10-10 00:05:41

回答

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); 
相关问题