0

I want to download the latest zip version of a private github repository I'm working on, and I want to do this using a PHP script. However, my current PHP script is just returning "Not Found" - I'm guessing I have an issue with my cURL user/pass setup, but I can't figure it out. My current code is as follows:

$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);

file_put_contents('master.zip', $result);
curl_close ($ch);
Josh979
  • 361
  • 4
  • 17
  • Might be tied to SSL? Turn on trace/verbose([see this](https://stackoverflow.com/questions/3757071/php-debugging-curl))? Specifically look at `VERIFYHOST` option. – ficuscr Aug 21 '17 at 21:16

3 Answers3

1

I was able to get it to work by separating the login page and the file download page into two requests. The following code worked for me:

$username='XXX';
$password='XXX';
$URL='https://github.com/[user]/[reponame]/archive/master.zip';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://github.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result=curl_exec ($ch);
curl_close ($ch);


file_put_contents('master.zip', $result);
Josh979
  • 361
  • 4
  • 17
1

I know that this question is a bit older, but it's worth noting that that GitHub allows for the generation of OAuth keys that allow you to download private repos. This allows you to not keep your username and password in your code.
For future seekers, below is working sample code utilising the OAuth access token generated.
The question was for zipball functionality, however the API allows for tarball as well.

 $fp = fopen('/path/to/yourfile.zip', 'w+');
 $giturl = 'https://api.github.com/repos/{username}/{reponame}/zipball/master?access_token={YourTokenHere}';
 $ch = curl_init($giturl);
 //set file to write to
 curl_setopt($ch, CURLOPT_FILE, $fp);
 //the API will not allow you to download without a user agent so CURLOPT_USERAGENT is important.
 curl_setopt($ch, CURLOPT_USERAGENT, 'PHP/'.phpversion('tidy'));
 //The API URL redirects so the following line is very important
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 $output = curl_exec($ch);

 if(curl_errno($ch)){
     throw new Exception(curl_error($ch));
 }

 //Get the HTTP status code.
 $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

 //Close the cURL handler.
 curl_close($ch);

 //Output result
 if($statusCode == 200){
     echo 'Downloaded Successfully';
 } else{
     echo "Failed downloading - Status Code: " . $statusCode;
 }
O Genthe
  • 161
  • 9
0

In addition to your answer I would suggest using the following if you want to download larger files to save memory.

$fp = fopen ('master.zip', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp); 
Neutrino
  • 35
  • 6