I am currently developing a library to call the pCloud API.
So far I've been able to do what I wanted to do, but I happen to have a problem with the http client (Guzzle, Curl).
I would like to use the "fileops" methods to download a file by chunk. So far all is ok, they provide, it is method in the doc (https://docs.pcloud.com/methods/fileops/).
I can use the methods in Postman without problems, in the browser too.
That is, I get my token, then use the "file_open" method to get the file descriptor, then read a part of my file with file_pread.
But doing exactly the same thing on PHP side with Guzzle, Http Client from Symfony, or from pCloud SDK provided by pCloud (https://github.com/pCloud/pcloud-sdk-php) it doesn't work.
It reports that the file remains open as long as you stay on the same session (linked to api token), otherwise it is closed. I have a single client for all requests.
"A descriptor is only valid for the same connection. If a connection closes, all the files are also closed. You can open the same file in multiple connections." from doc
What I do in PHP (not working):
// Create instance and login client
$p = new PCloudService($_ENV['PCLOUD_EMAIL'], $_ENV['PCLOUD_PASSWORD']);
// Open file and return fd
$fileOpen = $p->fileOperationOpen((new FileOpenWithFileIdInput(12426385087, PCloudFileOperation::WRITE)));
dump($p->getUserInfo()->getAuth(), $fileOpen);
// Attemp to read 100 first bytes of file
$fileRead = $p->fileOperationRead((new FilePReadWithFileIdInput($fileOpen, 100, 0)));
// API Throw an error code "1007: Invalid or closed file descriptor."
What I do in my browser/postman (working) :
Retrieving the token with (OK) :
http://eapi.pcloud.com/[email protected]&password=xxxxx&getauth=1
Result : Return the user info with the token
Retrieve the file descriptor with (OK) :
http://eapi.pcloud.com/file_open?auth=API_TOKEN&flags=0x0002&fileid=123456
Result : Return JSON with the number "fd" e.g. number : 2
Recover the first 100 bytes of my file with (OK):
http://eapi.pcloud.com/file_pread?auth=API_TOKEN&fd=2&count=100&offset=0
Result : Returns the first 100 bytes of the file.
I try to see what is the difference between Postman and my guzzle browser but I can't find it... (no cookie saved)
Here is the link to the repos: https://github.com/TheoD02/pCloud-API/blob/master/src/Service/PCloudService.php#L153
An answer from them, but does not help more, because no special support for API :
If a connection is closed, the files are also closed, this is correct.
Opening the same file in several different connections would also create several different sessions. A session has only one token.
The result you get is also correct.
You have opened the file (the same file) several times (on several sessions/ because they are different connections). The file descriptor shows the session increment for the file, while the server side gets 1, since it is the same file.
My Answer:
It seems like the issue you are facing is related to maintaining the same session/connection when making multiple requests to the pCloud API using Guzzle or other HTTP clients in PHP.
One possible solution to this problem is to ensure that you are reusing the same connection/session for all requests related to the same file. This can be achieved by using persistent connections in Guzzle.
You can enable persistent connections by setting the `keep_alive` option to `true` when creating the Guzzle client. This will allow Guzzle to reuse the same connection for multiple requests, which should help maintain the file descriptor open for the duration of the session.
Here is an example of how you can create a Guzzle client with persistent connections:
php
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'http://eapi.pcloud.com/',
'keep_alive' => true,
]);
You can then use this client to make multiple requests related to the same file, ensuring that the file descriptor remains open throughout the session.
Additionally, you may also want to check if there are any specific headers or parameters that need to be included in the requests to maintain the session/connection. Make sure to review the pCloud API documentation for any specific requirements related to session management.
I hope this helps resolve your issue with maintaining the same session/connection when using Guzzle to interact with the pCloud API. Let me know if you have any further questions or need more assistance.
Rate this post
5 of 5 based on 5991 votesComments