00001 <?php
00002 # Sample script for RAWS tutorial 4: see http://rambla.eu/raws-tutorial-4-getting-collections-php-client for more info.
00003 #
00004 # Copyright (C) 2010 rambla.be
00005
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 # http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 require_once '../RawsClient/Raws/Rass.php';
00018
00019 # Provide the path to a local testfile, that can be uploaded via RASS
00020 define('LOCAL_FILE', '/my/path/to/a/file.mp4');
00021
00022 # Provide your own RASS credentials here
00023 define('USER', 'xxx'); # your user account name
00024 define('PWD', 'xxx'); # your user account pwd
00025 define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")
00026
00027 define('BASE', 'http:
00028
00029 # all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
00030 try {
00031 # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
00032 $rass = new Rass(USER, PWD, BASE);
00033
00034 # Upload the local file as 'testX.mp4' to the CDN (= account's root-directory)
00035 $entries = array();
00036 $ctr = 1;
00037 while ($ctr < 3) {
00038 $entries[] = $entry = $rass->postItem("/", "test$ctr.mp4", LOCAL_FILE);
00039 echo "Created file with path: " . $entry->path . "\n";
00040 $ctr += 1;
00041 }
00042
00043 # Create a "bucks" directory under the root-directory
00044 $dir_entry = $rass->createDir("bucks", True);
00045 echo "Created directory with path: " . $dir_entry->path . "\n";
00046 $ctr = 1;
00047 # Upload the local file as 'bunnyX.mp4' to the "bucks" directory
00048 while ($ctr < 3) {
00049 $entry = $rass->postItem($dir_entry->path, "bunny$ctr.mp4", LOCAL_FILE);
00050 echo "Created file with path: " . $entry->path . "\n";
00051 $ctr += 1;
00052 }
00053
00054 # Create a query object to limit the returned entries to files only
00055 $query = new Rass_DirQuery("file");
00056
00057 # Retrieve a list of the files inside our root-directory
00058 echo "\nGetting files located under the root-directory:\n";
00059 $feed = $rass->getDirFeed("/", $query);
00060 # Walk through the list of entries (entry == file)
00061 foreach ($feed as $entry) {
00062 # Retrieve the entry element's "kind" and "path" attributes
00063 echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
00064 # Retrieve the value of some file properties
00065 echo "Filename: " . $entry->content->params->name->text . "\n";
00066 echo "Filesize: " . $entry->content->params->size->text . "\n";
00067 # Retrieve the public URL of the file (for download by end-users from the CDN)
00068 echo "Download URL: " . $entry->get_enclosure_url() . "\n";
00069 # Retrieve URL at which we can access the item resource (= wraps a file) using RASS
00070 echo "Entry ID: " . $entry->id->text . "\n";
00071 }
00072
00073 # Retrieve a list of the files inside the 'bucks' sub-directory
00074 echo "\nGetting files located under 'bucks' sub-directory:\n";
00075 $feed = $rass->getDirFeed("/bucks", $query);
00076 foreach ($feed as $entry) {
00077 echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
00078 echo "Filename: " . $entry->content->params->name->text . "\n";
00079 echo "Filesize: " . $entry->content->params->size->text . "\n";
00080 echo "Download URL: " . $entry->get_enclosure_url() . "\n";
00081 echo "Entry ID: " . $entry->id->text . "\n";
00082 }
00083
00084 # Retrieve a list of sub-directories of our root directory
00085 echo "\nGetting sub-directories of the root-direcory:\n";
00086 $query->setKind("dir");
00087 $feed = $rass->getDirFeed("/", $query);
00088 foreach ($feed as $entry) {
00089 echo "\nFound " . $entry->kind . " entry with path = " . $entry->path . "\n";
00090 echo "Filename: " . $entry->content->params->name->text . "\n";
00091 echo "Filesize: " . $entry->content->params->size->text . "\n";
00092 echo "Download URL: " . $entry->get_enclosure_url() . "\n";
00093 echo "Entry ID: " . $entry->id->text . "\n";
00094 }
00095
00096 # cleanup the $entries under the root dir
00097 foreach ($entries as $e) {
00098 $rass->deleteItem($e->path);
00099 echo "Deleted file: " . $e->path . "\n";
00100 }
00101 # cleanup the bucks dir
00102 $rass->deleteDir($dir_entry->path, True);
00103 echo "Deleted dir: " . $dir_entry->path . "\n";
00104
00105 }
00106 catch(Zend_Gdata_App_Exception $e) {
00107 # Report the exception to the user
00108 echo "\nCaught exception: " . get_class($e) . "\n";
00109 echo "Message: " . $e->getMessage() . "\n";
00110 # get the HTTP status code
00111 echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00112 echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00113 # get the raws:error elements
00114 $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00115 echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00116 echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00117 $reasons = $rawsResponse->getReasons();
00118 foreach ($reasons as $reason) {
00119 echo "Raws Reason: " . $reason . "\n";
00120 }
00121 $details = $rawsResponse->getDetails();
00122 foreach ($details as $key => $value) {
00123 echo "Raws Detail: " . $key . " -> " . $value . "\n";
00124 }
00125 }
00126 catch (Zend_Exception $e) {
00127 echo "Caught exception: " . get_class($e) . "\n";
00128 echo "Message: " . $e->getMessage() . "\n";
00129 }