Memsource-API/index.php

117 lines
3.5 KiB
PHP
Raw Normal View History

2019-02-27 04:41:46 +00:00
<?php
session_start();
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-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();
$resource = getResource($_SESSION['token']);
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;
return apiGet($_SESSION['token'], $url);
}
function listJobs(string $projectUid)
{
2019-06-18 00:23:12 +00:00
$url = '/api2/v1/projects/' . $projectUid . '/jobs/';
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
function apiGet(string $access_token, string $url)
{
$curl = curl_init();
curl_setopt_array(
2019-06-18 00:23:12 +00:00
$curl,
[
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-18 00:23:12 +00:00
$header = array_merge(
["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
$extraHeader
);
$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
]
);
$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,
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;
}