1
mirror of https://github.com/rclone/rclone synced 2024-11-20 21:27:33 +01:00

librclone: add PHP bindings and test program

This commit is contained in:
Jordi Gonzalez Muñoz 2022-07-20 18:20:12 +02:00 committed by GitHub
parent 9b76434ad5
commit f1166757ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 0 deletions

View File

@ -217,6 +217,14 @@ This needs expanding and submitting to pypi...
Rust bindings are available in the `librclone` crate: https://crates.io/crates/librclone
## PHP
The `php` subdirectory contains how to use the C library librclone in php through foreign
function interface (FFI).
Useful docs:
- [PHP / FFI](https://www.php.net/manual/en/book.ffi.php)
## TODO
- Async jobs must currently be cancelled manually at the moment - RcloneFinalize doesn't do it.

53
librclone/php/rclone.php Normal file
View File

@ -0,0 +1,53 @@
<?php
/*
PHP interface to librclone.so, using FFI ( Foreign Function Interface )
Create an rclone object
$rc = new Rclone( __DIR__ . '/librclone.so' );
Then call rpc calls on it
$rc->rpc( "config/listremotes", "{}" );
When finished, close it
$rc->close();
*/
class Rclone {
protected $rclone;
private $out;
public function __construct( $libshared )
{
$this->rclone = \FFI::cdef("
struct RcloneRPCResult {
char* Output;
int Status;
};
extern void RcloneInitialize();
extern void RcloneFinalize();
extern struct RcloneRPCResult RcloneRPC(char* method, char* input);
extern void RcloneFreeString(char* str);
", $libshared);
$this->rclone->RcloneInitialize();
}
public function rpc( $method, $input ): array
{
$this->out = $this->rclone->RcloneRPC( $method, $input );
$response = [
'output' => \FFI::string( $this->out->Output ),
'status' => $this->out->Status
];
$this->rclone->RcloneFreeString( $this->out->Output );
return $response;
}
public function close( ): void
{
$this->rclone->RcloneFinalize();
}
}

55
librclone/php/test.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/*
Test program for librclone
*/
include_once ( "rclone.php" );
const REMOTE = 'gdrive:/';
const FOLDER = "rcloneTest";
const FILE = "testFile.txt";
$rc = new Rclone( __DIR__ . '/librclone.so' );
$response = $rc->rpc( "config/listremotes", "{}" );
print_r( $response );
$response = $rc->rpc("operations/mkdir",
json_encode( [
'fs' => REMOTE,
'remote'=> FOLDER
]));
print_r( $response );
$response = $rc->rpc("operations/list",
json_encode( [
'fs' => REMOTE,
'remote'=> ''
]));
print_r( $response );
file_put_contents("./" . FILE, "Success!!!");
$response = $rc->rpc("operations/copyfile",
json_encode( [
'srcFs' => getcwd(),
'srcRemote'=> FILE,
'dstFs' => REMOTE . FOLDER,
'dstRemote' => FILE
]));
print_r( $response );
$response = $rc->rpc("operations/list",
json_encode( [
'fs' => REMOTE . FOLDER,
'remote'=> ''
]));
print_r( $response );
if ( $response['output'] ) {
$array = @json_decode( $response['output'], true );
if ( $response['status'] == 200 && $array['list'] ?? 0 ) {
$valid = $array['list'][0]['Name'] == FILE ? "SUCCESS" : "FAIL";
print_r("The test seems: " . $valid . "\n");
}
}
$rc->close();