-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi.php
More file actions
146 lines (113 loc) · 4.5 KB
/
api.php
File metadata and controls
146 lines (113 loc) · 4.5 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
class POEditor_API {
private string $api_url = 'https://api.poeditor.com/v2/';
public $apiKey;
function __construct() {
}
//makes a test request to see if the API key is correct
function validateAPIKey(): bool
{
$check = $this->_makeAPIRequest('languages/available');
if( $check->response->status == 'success' ) {
return true;
}
return false;
}
//creates a new project on POEditor.com
function addProject($name) {
return $this->_makeAPIRequest('projects/add', array('name' => $name));
}
//gets a list of online projects
function getProjects(): false|array
{
$projects_response = $this->_makeAPIRequest('projects/list');
$projects = array();
if($projects_response && isset($projects_response->result->projects)) {
foreach ($projects_response->result->projects as $project) {
//get each project's details
$project_info = $this->_makeAPIRequest('languages/list', array('id' => $project->id));
if($project_info && isset($project_info->result->languages) && count($project_info->result->languages)) {
foreach ($project_info->result->languages as $language) {
$project_item = array('name' => $project->name, 'id' => $project->id, 'language' => $language->name, 'code' => $language->code, 'percentage' => $language->percentage);
$projects[] = $project_item;
}
} else {
$project_item = array(
'name' => $project->name,
'id' => $project->id,
'language' => '',
'code' => '',
'percentage' => 0
);
$projects[] = $project_item;
}
}
return $projects;
}
return false;
}
//get a list of all languages
function getLanguages(): false|array
{
$languages_list_response = $this->_makeAPIRequest('languages/available');
if(count($languages_list_response->result->languages)) {
$languages = [];
foreach ($languages_list_response->result->languages as $lang) {
$languages[$lang->code] = $lang->name;
};
return $languages;
}
return false;
}
function addLanguage($project, $language) {
return $this->_makeAPIRequest('languages/add', array('id' => $project, 'language' => $language));
}
function upload($projectId, $path, $language, $overwrite, $updating, $sync) {
$upload = $this->_makeAPIRequest('projects/upload', array(
'id' => $projectId,
'language' => $language,
'file' => class_exists('CurlFile', false) ? new CURLFile($path, 'application/octet-stream') : "@{$path}",
'updating' => $updating,
'overwrite' => $overwrite,
'sync_terms' => $sync
));
// Check if the API request was successful
if ($upload !== null && isset($upload->response)) {
return $upload; // API call successful, return the response
} else {
// Handle the case when API call failed or didn't return the expected response
// You can log the error or take appropriate action based on your requirements.
return (object) array('response' => (object) array('status' => 'error', 'message' => 'API call failed'));
}
}
function download($projectId, $language, $type) {
$download = $this->_makeAPIRequest('projects/export', array('id' => $projectId, 'language' => $language, 'type' => $type));
return $download;
}
function getProjectLanguages($project) {
return $this->_makeAPIRequest('languages/list', array('id' => $project));
}
private function _makeAPIRequest($endpoint, $data = array()) {
$request_meta = array('api_token' => $this->apiKey);
if( !empty($data) ) {
$data = array_merge($request_meta, $data);
} else {
$data = $request_meta;
}
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $this->api_url . $endpoint);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result = json_decode($result);
return $result;
}
}
?>