简体   繁体   中英

download git repo using php

I want to download a repository file from github after that the user doing an action. The code will not work as expected. A .zip file is created when the code run but the resulting zip file is blank. How I can fix this?

$url = "https://github.com/$u/$repo/archive/master.zip";
$ch = curl_init();
    $f = fopen(__DIR__.'/master.zip', 'w+');
    $opt = [
        CURLOPT_URL => $url,
        CURLOPT_FILE => $f,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_BINARYTRANSFER => true,
        CURLOPT_HEADER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    ];
    curl_setopt_array($ch, $opt);
    $file = curl_exec($ch);

    curl_close($ch);
    fclose($f);

Remove CURLOPT_RETURNTRANSFER => true from your code or put it before CURLOPT_FILE .

This is known interaction as CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.

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