2019-02-27 04:41:46 +00:00
|
|
|
<?php
|
2019-03-02 00:00:37 +00:00
|
|
|
session_start();
|
2019-06-15 16:49:37 +00:00
|
|
|
|
2019-03-02 00:00:37 +00:00
|
|
|
if (!isset($_SESSION['token'])) {
|
|
|
|
header('Location: /oauth.php');
|
2019-02-27 04:41:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 00:23:12 +00:00
|
|
|
define('BASE_URL', parse_ini_file('config.ini')['base_url']);
|
2019-06-15 16:49:37 +00:00
|
|
|
|
2019-06-18 00:23:12 +00:00
|
|
|
// print_r(listProjects()); die();
|
|
|
|
print_r(getProject('dOVoecVbGYq85VwGYkJgY0')); die();
|
|
|
|
// print_r(listJobs('dOVoecVbGYq85VwGYkJgY0')); die();
|
|
|
|
downloadBilingualFiles('dOVoecVbGYq85VwGYkJgY0', ['tRJYN7LHPmaS8BVL3Vx8p2', 'DJbSRDTC7Jo004AvIb1V1M'], 'download.mxliff');
|
|
|
|
die();
|
2019-06-15 16:49:37 +00:00
|
|
|
|
2019-03-02 00:00:37 +00:00
|
|
|
$resource = getResource($_SESSION['token']);
|
2019-06-15 16:49:37 +00:00
|
|
|
print_r($resource); die();
|
|
|
|
|
|
|
|
function listProjects()
|
|
|
|
{
|
|
|
|
$url = '/api2/v1/projects';
|
|
|
|
$apiResponse = apiGet($_SESSION['token'], $url);
|
|
|
|
if ($apiResponse['totalPages'] > 1) {
|
|
|
|
//TODO: Repeat request to get other pages
|
|
|
|
}
|
|
|
|
$projects = [];
|
|
|
|
foreach ($apiResponse['content'] as $apiProject) {
|
|
|
|
$projects[] = [
|
|
|
|
'uid' => $apiProject['uid'],
|
|
|
|
'name' => $apiProject['name'],
|
|
|
|
'status' => $apiProject['status'],
|
|
|
|
'dateCreated' => $apiProject['dateCreated']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return $projects;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProject(string $projectUid)
|
|
|
|
{
|
2019-06-18 00:23:12 +00:00
|
|
|
$url = '/api2/v1/projects/' . $projectUid;
|
2019-06-15 16:49:37 +00:00
|
|
|
return apiGet($_SESSION['token'], $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
function listJobs(string $projectUid)
|
|
|
|
{
|
2019-06-18 00:23:12 +00:00
|
|
|
$url = '/api2/v1/projects/' . $projectUid . '/jobs/';
|
2019-06-15 16:49:37 +00:00
|
|
|
return apiGet($_SESSION['token'], $url);
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:23:12 +00:00
|
|
|
function downloadBilingualFiles(string $projectUid, array $jobsUid, string $filename)
|
|
|
|
{
|
|
|
|
$url = '/api2/v1/projects/'.$projectUid.'/jobs/bilingualFile?format=MXLF';
|
|
|
|
foreach ($jobsUid as $key => $jobUid) {
|
|
|
|
$postFields[] = ['uid' => $jobUid];
|
|
|
|
}
|
|
|
|
return apiDownloadFile($_SESSION['token'], $url, ['jobs' => $postFields], $filename);
|
|
|
|
}
|
|
|
|
|
2019-02-27 04:41:46 +00:00
|
|
|
// we can now use the access_token as much as we want to access protected resources
|
2019-06-15 16:49:37 +00:00
|
|
|
function apiGet(string $access_token, string $url)
|
|
|
|
{
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array(
|
2019-06-18 00:23:12 +00:00
|
|
|
$curl,
|
2019-06-15 16:49:37 +00:00
|
|
|
[
|
|
|
|
CURLOPT_URL => BASE_URL . $url,
|
|
|
|
CURLOPT_HTTPHEADER => ["Authorization: Bearer {$access_token}"],
|
|
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
|
|
CURLOPT_RETURNTRANSFER => true
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:23:12 +00:00
|
|
|
function apiJsonPost(string $access_token, string $url, array $postFields, array $extraHeader = [])
|
2019-06-15 16:49:37 +00:00
|
|
|
{
|
2019-06-18 00:23:12 +00:00
|
|
|
$header = array_merge(
|
|
|
|
["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
|
|
|
|
$extraHeader
|
|
|
|
);
|
2019-06-15 16:49:37 +00:00
|
|
|
$curl = curl_init();
|
2019-06-18 00:23:12 +00:00
|
|
|
curl_setopt_array(
|
|
|
|
$curl,
|
|
|
|
[
|
|
|
|
CURLOPT_URL => BASE_URL . $url,
|
|
|
|
CURLOPT_HTTPHEADER => $header,
|
|
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
|
|
CURLOPT_POST => true,
|
|
|
|
CURLOPT_POSTFIELDS => json_encode($postFields),
|
|
|
|
CURLOPT_RETURNTRANSFER => true
|
|
|
|
]
|
|
|
|
);
|
2019-06-15 16:49:37 +00:00
|
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
return json_decode($response, true);
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:23:12 +00:00
|
|
|
function apiDownloadFile($access_token, string $url, array $postFields, string $filename)
|
2019-02-27 04:41:46 +00:00
|
|
|
{
|
2019-06-18 00:23:12 +00:00
|
|
|
$file = fopen($filename, 'w+');
|
2019-02-27 04:41:46 +00:00
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, [
|
2019-06-18 00:23:12 +00:00
|
|
|
CURLOPT_URL => BASE_URL . $url,
|
|
|
|
CURLOPT_HTTPHEADER => ["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
|
2019-02-27 04:41:46 +00:00
|
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
2019-06-18 00:23:12 +00:00
|
|
|
CURLOPT_TIMEOUT => 500,
|
|
|
|
CURLOPT_FILE => $file,
|
|
|
|
CURLOPT_FOLLOWLOCATION => true,
|
2019-06-15 16:49:37 +00:00
|
|
|
CURLOPT_POST => true,
|
2019-06-18 00:23:12 +00:00
|
|
|
CURLOPT_POSTFIELDS => json_encode($postFields)
|
2019-02-27 04:41:46 +00:00
|
|
|
]);
|
|
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
2019-06-18 00:23:12 +00:00
|
|
|
return $response;
|
2019-06-15 16:49:37 +00:00
|
|
|
}
|