在php下载git master repository

在php下载git master repository

问题描述:

我想下载git master.zip到php代码目录。我正在使用cURL来下载它,并且我已经运行了代码,因此该方面正在工作。我想知道如何得到正确的文件从一些主要的URL,如在php下载git master repository

https://github.com/{group}/{project}/archive/master.zip 

的文件我收到只有1个字节的信息,我想知道我要去的地方错了。这里是我用于下载的代码

<?php 
//just an example repository 
$url = 'https://github.com/octocat/Spoon-Knife/archive/master.zip'; 

$fh = fopen('master.zip', 'w'); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FILE, $fh); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects 
curl_exec($ch); 

print 'Attempting download of '.$url.'<br />'; 
if(curl_error($ch) == '') 
    $errors = '<u>none</u>'; 
else 
    $errors = '<u>'.curl_error($ch).'</u>'; 
print 'cURL Errors : '.$errors; 

curl_close($ch); 

fclose($fh); 
?> 

谢谢。

+0

我收到整个文件 – DevZer0

+0

奇怪,我收到过的文件上的测试库,但不是我从被抓取的实际存储库。任何想法可能是什么问题? –

+0

很难说没有真正遇到问题开始 – DevZer0

如果allow_url_fopen设置为Onphp.ini中,您可以简单地使用file_get_contents()。在这种情况下不需要卷曲。我用成功下载我的仓库之一的master.zip

file_put_contents("master.zip", 
    file_get_contents("https://github.com/metashock/Hexdump/archive/master.zip") 
); 
+0

是我有我的php.ini文件正确设置。我意识到这可能是因为我试图从私有存储库获取master.zip。我没有对存储库的控制,那么有什么办法可以用私有存储库来做到这一点? –

+0

我猜私人的手段*私人*;) – hek2mgl

+0

好吧,谢谢你的帮助 –