简体   繁体   中英

Download git master repository in php

I would like to download the git master.zip to a directory in php code. I am using cURL to download it and the code I have works, so that aspect is working. I am wondering how to get the correct file from some master URL such as

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

The file I recieve has only 1 byte of information, and I am wondering where I am going wrong. Here is the code I am using for my download

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

Thanks.

If allow_url_fopen is set to On in the php.ini you can simply use file_get_contents() . No curl is required in this case. I successfully downloaded a master.zip of one of my repositories using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM