mirror of
https://github.com/brunofontes/Memsource-API.git
synced 2024-11-23 19:00:50 +00:00
Merge branch 'master' of github.com:brunofontes/Memsource-API
This commit is contained in:
commit
e898aa8749
17
README.md
17
README.md
@ -35,5 +35,22 @@ $token = $memsource->oauth()->getAccessToken($authCode, $client_id, $client_secr
|
||||
|
||||
Safely store this `$token` with the related user data and use it on any
|
||||
|
||||
## Project
|
||||
|
||||
### Project list
|
||||
|
||||
To list all projects...
|
||||
|
||||
```php
|
||||
$memsource = new \BrunoFontes\Memsource();
|
||||
$projectList = $memsource->project()->listProjects;
|
||||
```
|
||||
|
||||
To use filters, add the API filter as parâmeter:
|
||||
|
||||
```php
|
||||
$projectList = $memsource->project()->listProjects(['name' => 'Project X']);
|
||||
```
|
||||
|
||||
## Bilingual Files
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
|
||||
;<?php
|
||||
;die(); // For safety reasons
|
||||
;/*
|
||||
|
||||
[memsource_credentials]
|
||||
id=''
|
||||
secret=""
|
||||
callbackUri=""
|
||||
|
||||
;*/
|
||||
;?>
|
117
index.php
117
index.php
@ -1,117 +0,0 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['token'])) {
|
||||
header('Location: /oauth.php');
|
||||
}
|
||||
|
||||
define('BASE_URL', parse_ini_file('config.ini')['base_url']);
|
||||
|
||||
// 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)
|
||||
{
|
||||
$url = '/api2/v1/projects/' . $projectUid;
|
||||
return apiGet($_SESSION['token'], $url);
|
||||
}
|
||||
|
||||
function listJobs(string $projectUid)
|
||||
{
|
||||
$url = '/api2/v1/projects/' . $projectUid . '/jobs/';
|
||||
return apiGet($_SESSION['token'], $url);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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(
|
||||
$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);
|
||||
}
|
||||
|
||||
function apiJsonPost(string $access_token, string $url, array $postFields, array $extraHeader = [])
|
||||
{
|
||||
$header = array_merge(
|
||||
["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
|
||||
$extraHeader
|
||||
);
|
||||
$curl = curl_init();
|
||||
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);
|
||||
}
|
||||
|
||||
function apiDownloadFile($access_token, string $url, array $postFields, string $filename)
|
||||
{
|
||||
$file = fopen($filename, 'w+');
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => BASE_URL . $url,
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer {$access_token}", 'Content-type: application/json'],
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_TIMEOUT => 500,
|
||||
CURLOPT_FILE => $file,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => json_encode($postFields)
|
||||
]);
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
66
oauth.php
66
oauth.php
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
$config = parse_ini_file('config.ini');
|
||||
$authorize_url = $config['base_url'] . '/oauth/authorize';
|
||||
$token_url = $config['base_url'] . '/oauth/token';
|
||||
|
||||
// callback URL specified when the application was defined--has to match what the application says
|
||||
$callback_uri = $config['callbackUri'];
|
||||
|
||||
// client (application) credentials - located at apim.byu.edu
|
||||
$client_id = $config['id'];
|
||||
$client_secret = $config['secret'];
|
||||
|
||||
if (isset($_GET['code'])) {
|
||||
// what to do if there's an authorization code
|
||||
$access_token = getAccessToken($_GET['code']);
|
||||
$_SESSION['token'] = $access_token;
|
||||
header('Location: /');
|
||||
} elseif (isset($_GET['error'])) {
|
||||
echo 'Access Denied';
|
||||
} else {
|
||||
// what to do if there's no authorization code
|
||||
getAuthorizationCode();
|
||||
}
|
||||
|
||||
// step A - simulate a request from a browser on the authorize_url
|
||||
// will return an authorization code after the user is prompted for credentials
|
||||
function getAuthorizationCode()
|
||||
{
|
||||
global $authorize_url, $client_id, $callback_uri;
|
||||
$authorization_redirect_url = $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
|
||||
header('Location: ' . $authorization_redirect_url);
|
||||
// if you don't want to redirect
|
||||
// echo "Go <a href='$authorization_redirect_url'>here</a>, copy the code, and paste it into the box below.<br /><form action=" . $_SERVER["PHP_SELF"] . " method = 'post'><input type='text' name='authorization_code' /><br /><input type='submit'></form>";
|
||||
}
|
||||
|
||||
// step I, J - turn the authorization code into an access token, etc.
|
||||
function getAccessToken($authorization_code)
|
||||
{
|
||||
global $token_url, $client_id, $client_secret, $callback_uri;
|
||||
|
||||
$authorization = base64_encode("$client_id:$client_secret");
|
||||
$header = ["Authorization: Basic {$authorization}", 'Content-Type: application/x-www-form-urlencoded'];
|
||||
$content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri";
|
||||
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $token_url,
|
||||
CURLOPT_HTTPHEADER => $header,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $content
|
||||
]);
|
||||
$response = curl_exec($curl);
|
||||
$objectResponse = json_decode($response);
|
||||
curl_close($curl);
|
||||
|
||||
if ($response === false) {
|
||||
echo 'Failed', curl_error($curl);
|
||||
} elseif (isset($objectResponse->error)) {
|
||||
echo "Error:<br /> $authorization_code $response";
|
||||
}
|
||||
return $objectResponse->access_token;
|
||||
}
|
Loading…
Reference in New Issue
Block a user