128 lines
4.8 KiB
PHP
128 lines
4.8 KiB
PHP
<?php
|
|
|
|
require_once("config.inc");
|
|
|
|
class Uploader {
|
|
|
|
private static function sendVerifyRequest($request_url, $token, $w_id, $file_path, $file_name ){
|
|
|
|
$myfile = fopen($file_path, "r") or die("Unable to open file!");
|
|
$csv_content = fread($myfile,filesize($file_path));
|
|
fclose($myfile);
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => $request_url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => "",
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"api_token\"\r\n\r\n" . $token . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"workspace_id\"\r\n\r\n" . $w_id . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"filedata\";
|
|
filename=\"" . $file_name . "\"\r\nContent-Type: text/csv\r\n\r\n" . $csv_content . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"toggl_new\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
|
|
CURLOPT_HTTPHEADER => array(
|
|
"cache-control: no-cache",
|
|
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
|
|
),
|
|
));
|
|
|
|
$response = curl_exec($curl);
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
if ($err) {
|
|
return "cURL Error #:" . $err;
|
|
} else {
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
private static function sendRequest($request_url, $token, $w_id, $u_id){
|
|
$curl = curl_init();
|
|
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => $request_url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => "",
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => "POST",
|
|
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"upload_id\"\r\n\r\n" . $u_id . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"api_token\"\r\n\r\n" . $token . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"workspace_id\"\r\n\r\n". $w_id . "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
|
|
name=\"toggl_new\"\r\n\r\ntrue\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
|
|
CURLOPT_HTTPHEADER => array(
|
|
"cache-control: no-cache",
|
|
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
|
|
),
|
|
));
|
|
|
|
$response = curl_exec($curl);
|
|
$err = curl_error($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
if ($err) {
|
|
return false;
|
|
} else {
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
public static function uploadFile($file){
|
|
global $config;
|
|
$name = basename($file, ".csv");
|
|
$workspace_id = explode("/", $config[$name])[5];
|
|
|
|
$verify_result = json_decode(Uploader::sendVerifyRequest($config['validate_URL'], $config['API_token'], $workspace_id, "/opt/lampp/htdocs/" . $file, $name),true);
|
|
|
|
$upload_id = $verify_result["data"]["upload_id"];
|
|
|
|
if ($verify_result["data"]["confirmable"]){
|
|
$import_result = Uploader::sendRequest($config['upload_URL'], $config['API_token'], $workspace_id, $upload_id);
|
|
if (!$import_result){
|
|
sleep(1);
|
|
$status_result = json_decode(Uploader::sendRequest($config['status_URL'], $config['API_token'], $workspace_id, $upload_id),true);
|
|
$status_text = $status_result["data"]["status"];
|
|
$step = 0;
|
|
$error = false;
|
|
do{
|
|
if ($status_text == "Complete") break;
|
|
if ($status_text == "Running"){
|
|
$step = $step + 1;
|
|
if ($step == 5){
|
|
$error = true;
|
|
break;
|
|
}
|
|
sleep(1);
|
|
continue;
|
|
}
|
|
}while(1);
|
|
|
|
if (!$error){
|
|
echo (" OK ");
|
|
}else{
|
|
echo (" NOT OK - status! ");
|
|
echo ($status_result["data"]["status"]);
|
|
}
|
|
}else{
|
|
echo (" NOT OK - upload!");
|
|
}
|
|
}else{
|
|
echo (" NOT OK - verify !");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
?>
|