Development:Web services:Files: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 15: | Line 15: | ||
* contextid | * contextid | ||
* component | |||
* filearea | |||
* itemid | * itemid | ||
* filename | * filename | ||
* filepath | * filepath | ||
It will return an array, can be described as PHP array: | It will return an array, can be described as PHP array: | ||
Line 33: | Line 34: | ||
The service name is "moodle_file_get_files", defined in lib/db/services.php. | The service name is "moodle_file_get_files", defined in lib/db/services.php. | ||
It takes | ====upload==== | ||
This function is used to upload a file to user private area. | |||
It takes 7 parameters, all of them are mandatory: | |||
* contextid | * contextid | ||
* component | |||
* filearea | |||
* itemid | * itemid | ||
* filepath | * filepath | ||
* filename | * filename | ||
* | * filecontent | ||
It will return a boolean value, true/false. | It will return a boolean value, true/false. | ||
The service name is " | The service name is "moodle_file_upload", defined in lib/db/services.php | ||
Sample code (using Zend Framework): | |||
<code php> | |||
$url = MOODLE_WSROOT. '?wstoken=xxxxxxxxxxxxxxxxxxxxxx'; | |||
$zend = new Zend_XmlRpc_Client($url); | |||
$srv = $zend->getProxy(); | |||
$files = $srv->moodle_file_get_files($contextid, $component, $filearea, $itemid, $filepath, $filename); | |||
$file = $srv->moodle_file_upload($contextid, 'user', 'private', 0, '/', 'info.txt', base64_encode('this is file content')); | |||
</code> |
Latest revision as of 07:50, 9 July 2010
These APIs will be used to browse Moodle files by Moodle Web Services.
class moodle_file_external()
This class implements the interface to moodle files, for browsing, downloading and uploading files. It is defined in files/externals.php.
We cannot return the whole files tree by web service API, because to make sure the web services working in every language and platform, we need to define a fixed data structure of return value, but the files tree can change all the time. See more information about web services at: Development:External_services_description.
The class contains following methods:
get_files
This function is used to browse files.
It takes 5 parameters, all of them are optional, if you provide no parameters, it will return the top level content of moodle repository.
- contextid
- component
- filearea
- itemid
- filename
- filepath
It will return an array, can be described as PHP array:
$files = array( 'path' => array(array('name'=>'root', 'path=>'/'), array('name'=>'subdir', 'path=>'/sub/')), 'files' => array( array('filename'=>'readme', 'filepath'=>'/', 'filearea'=>'forum', 'itemid'=>110, 'contextid'=>1, 'isdir'=>true), array('filename'=>'changes', 'filepath'=>'/', 'filearea'=>'forum', 'itemid'=>112, 'contextid'=>1, 'isdir'=>false), ) );
The service name is "moodle_file_get_files", defined in lib/db/services.php.
upload
This function is used to upload a file to user private area.
It takes 7 parameters, all of them are mandatory:
- contextid
- component
- filearea
- itemid
- filepath
- filename
- filecontent
It will return a boolean value, true/false.
The service name is "moodle_file_upload", defined in lib/db/services.php
Sample code (using Zend Framework):
$url = MOODLE_WSROOT. '?wstoken=xxxxxxxxxxxxxxxxxxxxxx';
$zend = new Zend_XmlRpc_Client($url);
$srv = $zend->getProxy();
$files = $srv->moodle_file_get_files($contextid, $component, $filearea, $itemid, $filepath, $filename);
$file = $srv->moodle_file_upload($contextid, 'user', 'private', 0, '/', 'info.txt', base64_encode('this is file content'));