meganz / sdk

MEGA C++ SDK
BSD 2-Clause "Simplified" License
1.33k stars 504 forks source link

PHP extensions #513

Open peter279k opened 7 years ago

peter279k commented 7 years ago

Is it possible to make MEGA SDK compile a PHP extnsion?

Thanks.

javiserrano commented 7 years ago

We provide PHP bindings for our SDK. Here you have an example app: https://github.com/meganz/sdk/tree/master/examples/php

peter279k commented 7 years ago

Hi @javiserrano, thank you for your comment. I have other question about this: Do you provide the resuming downloading file in this PHP bindings for megacli?

I also found this following two PRs and I think it's sure to provide it.

https://github.com/meganz/sdk/pull/502 https://github.com/meganz/sdk/pull/438

Thanks.

javiserrano commented 7 years ago

Hi,

The example app is quite basic and hasn't been updated during a long time, so it doesn't support features that we implemented later (like transfer resumption). The same happens with PHP bindings.

However, any function in the intermediate layer of the SDK (megaapi.h) can be used from PHP without much effort. Let me explain how our bindings work.

The public interface of our SDK for final apps is megaapi.h: https://github.com/meganz/sdk/blob/master/include/megaapi.h

You can read the documentation related to each function in that file, or download the documentation generated by Doxygen from the link on our README.md file: https://github.com/meganz/sdk/blob/master/README.md

That documentation applies to all language bindings (Java, Objective C, PHP, etc.). Bindings that we use in our public mobile apps (Java, Objective C, C#) are the most up to date and even some of them (Objective C and C#) are created manually.

Others like PHP and Python are automatically created by SWIG (http://www.swig.org/) and their bindings and example app are not updated very often.

SWIG creates bindings for most functions in megaapi.h using a configuration file: https://github.com/meganz/sdk/blob/master/bindings/megaapi.i

For PHP, for example, SWIG generates a file bindings/php/mega.php that allows to access most functions in megaapi.h.

However, there are some problems with garbage collectors and callbacks that force us to create an extra layer on top of those autogenerated bindings. For PHP, for example, it's bindings/php/megaapi.php.

If you open that file, you can see that it basically provides the same functions in the other file, but creating wrapper objects for listeners.

So, to add a new function, it's only needed to add it to MegaApiPHP (in bindings/php/megaapi.php) and call corresponding function in MegaApi (in bindings/php/mega.php)

Since bindings/php/mega.php is autogenerated, it should contain most functions in megaapi.h that hasn't been explicitly disabled in the configuration file.

To enable transfer resumption, it's needed to call MegaApi::enableTransferResumption: https://github.com/meganz/sdk/blob/master/include/megaapi.h#L6207

in the onRequestFinish callback related to the fetchNodes request, after a login with a session. Here you can see how MEGAsync uses it: https://github.com/meganz/MEGAsync/blob/master/src/MEGASync/MegaApplication.cpp#L5562

We will try to add that feature to our example apps, but we probably won't be able to do it in a short time due to other priorities. However, if you want to try to do it, we can help you solving your doubts here.