Skip to content

Instantly share code, notes, and snippets.

@ecrider
Last active March 18, 2024 03:45
Show Gist options
  • Save ecrider/7985dfd132791a5edbdb09dfb03dd73c to your computer and use it in GitHub Desktop.
Save ecrider/7985dfd132791a5edbdb09dfb03dd73c to your computer and use it in GitHub Desktop.
Download repos with PHP and cURL from GitHub API as zip/tarball
<?php
/**
* Wasted half my evening trying to get this done.
* Those seems to be the only settings that works for
* downloading large repositories as zip/tarball
* from GitHub with PHP and cURL.
*
* If you don't have to use PHP, then use cURL direcly:
* curl -L -v -H "Authorization: token PERSONAL_ACCESS_TOKEN" https://api.github.com/repos/SOME_USER/SOME_REPO/zipball > dooone.zip
*/
$where_we_want_it_saved = 'destination/zipfile.zip';
$zip = fopen($where_we_want_it_saved, 'w');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.github.com/repos/SOME_USER/SOME_REPO/zipball');
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 100);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_FILE, $where_we_want_it_saved);
curl_setopt($curl, CURLOPT_USERAGENT, 'Chrome/64.0.3282.186 Safari/537.36');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: token PERSONAL_ACCESS_TOKEN'));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$httpcode = curl_getinfo($curl , CURLINFO_HTTP_CODE);
$result = curl_exec($curl);
curl_close($curl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment