1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php
error_reporting(0); header('Content-Type: text/html; charset=UTF-8'); date_default_timezone_set("PRC"); define("REPO","repo"); define("USER","user"); define("MAIL","mail"); define("TOKEN","token");
function upload($url, $content) { $ch = curl_init(); $defaultOptions=[ CURLOPT_URL => $url, CURLOPT_FOLLOWLOCATION => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST=>"PUT", CURLOPT_POSTFIELDS=>json_encode([ "message"=> date("Y-m-d H:i:s "), "committer"=> [ "name"=> USER, "email"=>MAIL, ], "content"=> $content, ]), CURLOPT_HTTPHEADER => [ "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language:zh-CN,en-US;q=0.7,en;q=0.3", "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36", 'Authorization:token '.TOKEN, ], ]; curl_setopt_array($ch, $defaultOptions); $chContents = curl_exec($ch); curl_close($ch); return $chContents; }
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES["pic"]["error"] <= 0) { $filename = 'images' . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/' . md5(time()) . ".png"; $url = "https://api.github.com/repos/" . USER . "/" . REPO . "/contents/" . $filename; $tmpName = './tmp' . md5($filename); move_uploaded_file($_FILES['pic']['tmp_name'], $tmpName); $content = base64_encode(file_get_contents($tmpName)); $res = json_decode(upload($url, $content), true); unlink($tmpName); if ($res['content']['path'] != "") { $return['code'] = 'success'; $return['data']['filename'] = $filename; $return['data']['url'] = 'https://cdn.jsdelivr.net/gh/' . USER . '/' . REPO . '/' . $res['content']['path']; } else { $return['code'] = 500; $return['url'] = null; } } else { $return['code'] = 404; $return['url'] = null; } exit(json_encode($return));
|