Hi @Constantine
Thanks for you reply and help
I tried to used the insert method:
<?php
// Configuration - Replace with your actual values
$apiUrl = 'https://of.mydomain.com/api/2.0/files'; // Your API URL (base URL only)
$jwt_secret = 'zLT2qX6\/Dxgwqny9+9ZqkEvY\/5DxS9GiaKe1u\/15BJ4rtdVuvfAvVSrwnjEAHG76LG\/W4LP1S4Qy+KxKVxYiZSbzwSP7fab32H7IBFa4FniirPbgdkViJRlYTt7drE5O7HXcVHsSqwJOWv0ZfXmlvRQVALyAr64L1Jt\/ZlrFAKU='; // Your JWT secret key
$folderId = 10; // Target folder ID
$filePath = '/home/hello.docx'; // Path to your local file
$fileName = basename($filePath); // Extract filename from path
$createNewIfExist = true; // Whether to create a new file if one with the same name exists.
$keepConvertStatus = true; // Keep the convert status (set to false if you don't need it).
// Function to generate a JWT token (you might already have this)
function generateJWT($payload, $secret) {
$header = json_encode(['typ' => 'JWT', 'alg' => 'HS256']);
$payload = json_encode($payload);
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));
$base64UrlPayload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));
$signature = hash_hmac('sha256', $base64UrlHeader . '.' . $base64UrlPayload, $secret, true);
$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));
return $base64UrlHeader . '.' . $base64UrlPayload . '.' . $base64UrlSignature;
}
// 1. Prepare the API URL
$uploadUrl = $apiUrl . '/' . $folderId . '/insert';
// 2. Prepare the data for the API request. Crucially, the 'file' parameter MUST be null.
$data = [
'file' => null, // Important: Set to null
'title' => $fileName, // Or a custom title if desired
'createNewIfExist' => [$createNewIfExist], // Wrap boolean value in an array
'keepConvertStatus' => $keepConvertStatus
];
$jsonData = json_encode($data);
// 3. Generate JWT token for authorization
$token = generateJWT(['payload' => []], $jwt_secret); // Create a simple payload (or empty).
// 4. Set up cURL for the upload
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $uploadUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true, // Use POST
CURLOPT_POSTFIELDS => $jsonData, // Send the JSON data
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer ' . $token // Include the JWT token
],
CURLOPT_SSL_VERIFYPEER => false, // Disable SSL verification (for testing ONLY - remove in production!)
CURLOPT_SSL_VERIFYHOST => false, // Disable SSL verification (for testing ONLY - remove in production!)
]);
// 5. Execute the API request
$response = curl_exec($curl);
// Check for cURL errors
if (curl_errno($curl)) {
echo 'cURL error: ' . curl_error($curl);
exit;
}
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// 6. Handle the API response
echo "HTTP Code: " . $httpCode . "\n";
echo "API Response:\n";
echo $response . "\n";
// 7. If the upload was successful (HTTP 200), trigger the file upload separately
if ($httpCode == 200) {
$response_data = json_decode($response, true);
// Extract the upload URL from the insert response. Error handling is crucial here!
if (isset($response_data['response']['file']['uploadUri'])) {
$uploadUri = $response_data['response']['file']['uploadUri'];
echo "Proceeding to upload file to: " . $uploadUri . "\n";
// Second cURL request for the actual file upload
$fileCurl = curl_init();
curl_setopt_array($fileCurl, [
CURLOPT_URL => $uploadUri,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => file_get_contents($filePath),
CURLOPT_HTTPHEADER => [
'Content-Type: application/octet-stream', // Important for raw file uploads
'Authorization: Bearer ' . $token // Include JWT token
],
CURLOPT_SSL_VERIFYPEER => false, // Disable SSL verification (for testing ONLY - remove in production!)
CURLOPT_SSL_VERIFYHOST => false, // Disable SSL verification (for testing ONLY - remove in production!)
]);
$fileUploadResponse = curl_exec($fileCurl);
if (curl_errno($fileCurl)) {
echo 'File upload cURL error: ' . curl_error($fileCurl);
}
$fileUploadHttpCode = curl_getinfo($fileCurl, CURLINFO_HTTP_CODE);
curl_close($fileCurl);
echo "File Upload HTTP Code: " . $fileUploadHttpCode . "\n";
echo "File Upload API Response:\n";
echo $fileUploadResponse . "\n";
if ($fileUploadHttpCode != 200){
echo "File upload failed.\n";
} else {
echo "File uploaded successfully!\n";
}
} else {
echo "Error: Could not extract upload URI from initial API response.\n";
print_r($response_data); // Print the decoded response for debugging
}
} else {
echo "Initial API request failed. Check the error messages above.\n";
}
?>
But that gives me:
HTTP Code: 401 API Response: Unauthorized Initial API request failed. Check the error messages above.
can you help me how i can successful upload/insert the file?