I have recently implemented the Graph API to:
Create in a shared folder various directories with names like this => "uc_123455"; on each folder created, the API upload PDFs inside it.
The problem:
It starts OK but after a time it just stop creating folder, it has a total of almost 2323 folders to create and stops after 1000.
The code works like this:
It first get the access_token from Microsoft and load the Graph then get the shared folder ID.
$this->graph = new Graph();
$this->graph->setAccessToken($this->accessToken);
$this->directory_id = $this->fetchDirectoryId($this->graph);
then it start looping:
First it create the name based on what part is of the loop
$subfolderName = "UC_" . $customer->numero_uc;
after this is used to create the folder on this function
$folder = $this->createIntoDirectory($this->directory_id[0], $this->graph, $subfolderName);
and this function works like this
public function createIntoDirectory(string $folderId, Graph $graph, string $ucNumber): string|bool
{
try {
$response = $graph->createRequest('POST', "/me/drives/" . $this->drive_id . "/items/$folderId/children")
->attachBody(json_encode([
"name" => $ucNumber,
"folder" => new stdClass()
], JSON_THROW_ON_ERROR))
->setReturnType(DriveItem::class)
->execute();
return $response->getId();
} catch (GuzzleException|GraphException) {
return $this->fetchExistenteDirectory($folderId, $graph, $ucNumber);
}
}
if already exist an Folder with the name it falls in the exception and call to another function that return the id of the already created folder (I did this in case any PDFs weren't sent to the folder).
Here the function:
public function fetchExistenteDirectory(string $folderId, Graph $graph, string $ucNumber): string|bool
{
try {
$folder = $graph->createRequest('GET', "/me/drives/" . $this->drive_id . "/items/$folderId/children")
->setReturnType(DriveItem::class)
->execute();
foreach ($folder as $item) {
if ($item->getName() === $ucNumber) {
return $item->getId();
}
}
return false;
} catch (GuzzleException|GraphException) {
return false;
}
}
Are there problems with my code to stop generation folders, because it works 100% fine, until then it stops?
Questions:
Where is the PDF? The PDF are converted images from URLs that I fetch from my database.
What is a UC?: is like users, it come from Portuguese word "unidade consumidora" or "consumer unit", each of then come from an array that I fetch from my database.
If you have any questions about the code or the process just ask.
(English isn't my mother tongue, so sorry for any grammatical error).
I tried to create a loop using sleep or increasing time of execution on my PHP but still nothing.