-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapi_func.php
79 lines (76 loc) · 2.97 KB
/
api_func.php
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
71
72
73
74
75
76
77
78
<?php
$GITHUB_USERNAME = ''; // 用户名
$GITHUB_REPONAME = ''; // Repository名
$GITHUB_BRANCHNAME = ''; // 分支名
$GITHUB_TOKEN = ''; // TOKEN
$GITHUB_EMAIL = ''; // 邮箱
$GITHUB_NAME = ''; // 昵称
function callInterfaceCommon($URL, $type, $params, $headers)
{
$ch = curl_init($URL);
$timeout = 5;
if ($headers != "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
switch ($type) {
case "GET" :
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "PUT" :
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "PATCH":
curl_setopt($ch, CULROPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
break;
}
$result = curl_exec($ch);
$myfile = fopen("testfile.txt", "w");
fwrite($myfile, $result);
if (curl_errno($ch)) {
return 'Curl Error: ' . curl_error($ch);
}
curl_close($ch);
return 'Success';
}
function upload_file_to_github($abs_filepath, $content)
{
global $GITHUB_USERNAME, $GITHUB_TOKEN, $GITHUB_NAME;
global $GITHUB_REPONAME, $GITHUB_BRANCHNAME, $GITHUB_EMAIL;
$params = sprintf('{"message":"Upload New File",
"branch": "%s",
"content": "%s",
"committer": {
"name": "%s",
"email": "%s"
}
}', $GITHUB_BRANCHNAME, $content, $GITHUB_NAME, $GITHUB_EMAIL);
$url = sprintf('https://api.github.com/repos/%s/%s/contents/%s',
$GITHUB_USERNAME, $GITHUB_REPONAME, $abs_filepath);
$headers = array('User-Agent: ' . $GITHUB_USERNAME, 'Authorization:token ' . $GITHUB_TOKEN);
$result = callInterfaceCommon($url, "PUT", $params, $headers);
if ($result == 'Success') {
$cdnURL = sprintf('https://cdn.jsdelivr.net/gh/%s/%s@%s/%s',
$GITHUB_USERNAME, $GITHUB_REPONAME, $GITHUB_BRANCHNAME, $abs_filepath);
$originURL = sprintf('https://raw.githubusercontent.com/%s/%s/%s/%s',
$GITHUB_USERNAME, $GITHUB_REPONAME, $GITHUB_BRANCHNAME, $abs_filepath);
$sevenCDN = sprintf('https://raw.sevencdn.com/%s/%s/%s/%s',
$GITHUB_USERNAME, $GITHUB_REPONAME, $GITHUB_BRANCHNAME, $abs_filepath);
return $cdnURL . ' ' . $originURL . ' ' . $sevenCDN;
} else {
return $result;
}
}