RAWS tutorial 4: getting collections with the PHP client

In the previous tutorial, we learned how to retrieve ATOM collections from RAWS. More specifically, we retrieved information about our files and directories on the CDN using the RASS API's dir resource. This time, we will repeat these requests using the PHP client from tutorial 2.

The complete source code for this tutorial is also part of the client download package ('rass_basics_get_feed.php' in the 'samples' directory). You can try running it yourself, after having edited the named constants on top of the script. For more information about using the PHP client, see tutorial 2.

Pre-configuration

This tutorial assumes that some files and directories have already been created on the CDN for user 'monty'. The sample script uses a local file (-> path needs to be set in the named constant 'LOCAL_FILE') to create the files.

  • Files located under the root-directory: test1.mp4, test2.mp4.
  • Files located under a 'bucks' sub-directory: bunny1.mp4, bunny2.mp4.

Get files list

First we'll retrieve a list of all files inside our root-directory, by sending a GET request to "http://rass.cdn01.rambla.be/dir/?kind=file". To do this, we create a Rass connection object and call getDirFeed() on it, passing "/" as the first argument. As the second argument, we pass a Rass_DirQuery object which has a 'kind' property that has been set to "file" (in the constructor, see below for more).

If the request succeeds, our method will return a Rass_DirFeed object. Otherwise, it will raise a Zend_Gdata_App_Exception.

require_once './RawsClient/Raws/Rass.php';
$rass = new Rass("monty", "mypwd", "http://rass.cdn01.rambla.be/");
$query = new Rass_DirQuery("file");
$dir_feed = $rass->getDirFeed("/", $query);

The returned $dir_feed object contains an array of Rass_DirEntry objects, which encapsulate the ATOM entries from the response (see tutorial 2). These entries provide information about our files and/or directories.

foreach ($feed as $entry) {
# Retrieve the entry element's "kind" and "path" attributes
echo "Found " . $entry->kind . " entry with path = " . $entry->path . "\n";
# Retrieve the value of some file properties
echo "Filename: " . $entry->content->params->name->text . "\n";
echo "Filesize: " . $entry->content->params->size->text . "\n";
# Retrieve the public URL of the file (for download by end-users from the CDN)
echo "Download URL: " . $entry->get_enclosure_url() . "\n";
}
>> Found file entry with path = /test1.mp4
>> Filename: test1.mp4
>> Filesize: 13827
>> Download URL: http://monty.cdn01.rambla.be/test1.mp4
>>
>> Found file entry with path = /test2.mp4
>> ...

Manipulating query-string arguments

Query-string arguments are used by RAWS to further specify a request. Depending on the resource, they can be used as a search query, a resource filter, a response format indicator...

The PHP client defines resource specific query-string objects (= all derived from RawsQuery) which follow a naming convention. For example, if you want to send a request to the RASS 'dir' resource, you instantiate a Rass_DirQuery object.

The actual query-string arguments can be passed in the constructor of these objects or can be set on them as properties. In our previous example, we created an instance of Rass_DirQuery like this:

$query = new Rass_DirQuery("file");

Which can also be written as:

$query = new Rass_DirQuery()
$query->setKind("file")

Get files list for sub-directory

To get the list of files stored under our 'bucks' sub-directory, we only need to change the first getDirFeed() parameter from "/" to "/bucks". The client will now send a GET request to 'http://rass.cdn01.rambla.be/dir/bucks/?kind=file'.

$dir_feed = $rass->getDirFeed("/bucks", $query);
foreach ($feed as $entry) {
echo "Found " . $entry->kind . " entry with path = " . $entry->path . "\n";
}
>> Found file entry with path = /bucks/bunny1.mp4
>> Found file entry with path = /bucks/bunny2.mp4

Get list of sub-directories

In this case, we simply set the 'kind' property of the Rass_DirQuery object to "dir". This will result in a GET request to "http://rass.cdn01.rambla.be/dir/?kind=dir".

$query->setKind("dir");
$feed = $rass->getDirFeed("/", $query);
foreach ($feed as $entry) {
echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
}
>> Found dir entry with path = /bucks