00001 <?php
00002 # Sample script for RAWS tutorial 6: see http://rambla.eu/tutorial-6-handling-atom-links-and-pagination-php 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 ## TUTORIAL PRE-CONFIG: FIRST CREATE DIRECTORY + UPLOAD FILES TO HAVE SOME TEST-DATA
00035
00036 # Create a "tutorial6" directory under the root-directory
00037 $item_entry = null;
00038 $dir_entry = $rass->createDir("tutorial6", True);
00039 echo "Created directory with path: " . $dir_entry->path . "\n";
00040 $ctr = 1;
00041 # Upload the local file 15 times as 'testXX.mp4' to the "tutorial6" directory (creating some test data)
00042 while ($ctr < 16) {
00043 $item_entry = $rass->postItem($dir_entry->path, "test$ctr.mp4", LOCAL_FILE);
00044 echo "Created file with path: " . $item_entry->path . "\n";
00045 $ctr += 1;
00046 }
00047
00048
00049 ## GETTING LINK ELEMENTS FOR AN ENTRY
00050
00051 # Directly getting at an item entry's link elements
00052 echo "\nGetting all link elements for entry with path: " . $item_entry->path . "\n";
00053 foreach($item_entry->link as $link) {
00054 echo "\nType of relation: " . $link->rel;
00055 echo "\nLink URI: " . $link->href;
00056 echo "\nExpected type: " . $link->type . "\n";
00057 }
00058
00059 # Using a helper method to get to an entry's enclosure URL
00060 echo "\nGetting self link via helper method: " . $item_entry->get_self_url();
00061 echo "\nGetting alternate link via helper method: " . $item_entry->get_alternate_url();
00062 echo "\nGetting edit link via helper method: " . $item_entry->get_edit_url();
00063 echo "\nGetting enclosure link (= download location of the file on the CDN) via helper method: " . $item_entry->get_enclosure_url() . "\n";
00064
00065
00066 ## GETTING LINK ELEMENTS FOR A FEED
00067
00068 # Retrieve a list of the files inside the 'tutorial6' sub-directory
00069 $query = new Rass_DirQuery("file");
00070 $feed = $rass->getDirFeed($dir_entry->path, $query);
00071
00072 # Directly getting at the item entry's link elements
00073 echo "\nGetting all link elements for feed with ID: " . $feed->id . "\n";
00074 foreach($feed->link as $link) {
00075 echo "\nType of relation: " . $link->rel;
00076 echo "\nLink URI: " . $link->href;
00077 echo "\nExpected type: " . $link->type . "\n";
00078 }
00079
00080
00081 ## USING PAGINATION (via helper functions)
00082
00083 # Since we've uploaded 15 files, we can retrieve partial results by suggesting to RASS that we only want 10 results in a single response
00084 $query->setPaginateBy("10");
00085 $feed = $rass->getDirFeed($dir_entry->path, $query);
00086
00087 # since the request only contains 10 entries, there should be a 'next' + 'last' link inside the feed element
00088 echo "\nNext link URI: " . $feed->get_next_link() . "\n";
00089 echo "Last link URI: " . $feed->get_last_link() . "\n";
00090
00091 # There's also a helper function to directly get at the 'next' feed (takes the original feed as argument)
00092 $next_feed = $rass->getNextFeed($feed);
00093 echo "\nThis feed contains all remaining entries => new call to get_next_link() will return empty string: " . $next_feed->get_next_link() . "\n";
00094
00095
00096 ## PAGINATION BEST PRACTICES
00097
00098 # if you want to retrieve all results from a resource that supports pagination, you might want to use a while loop
00099 echo "\nGetting all files under " . $dir_entry->path . " using pagination:\n";
00100 $feed = $rass->getDirFeed($dir_entry->path, $query);
00101 while($feed)
00102 {
00103 foreach ($feed as $entry) {
00104 # process your entries here..
00105 echo "\nFound entry with path = " . $entry->path;
00106 }
00107 # get next feed, by sending a new request to the next link inside this page
00108 $feed = $rass->getNextFeed($feed);
00109 if ($feed) {
00110 echo "\nGetting next page..";
00111 }
00112 }
00113
00114
00115 ## TUTORIAL CLEANUP
00116
00117 # cleanup the tutorial6 dir
00118 $rass->deleteDir($dir_entry->path, True);
00119 echo "\nDeleted dir: " . $dir_entry->path . "\n";
00120
00121 }
00122 catch(Zend_Gdata_App_Exception $e) {
00123 # Report the exception to the user
00124 echo "\nCaught exception: " . get_class($e) . "\n";
00125 echo "Message: " . $e->getMessage() . "\n";
00126 # get the HTTP status code
00127 echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00128 echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00129 # get the raws:error elements
00130 $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00131 echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00132 echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00133 $reasons = $rawsResponse->getReasons();
00134 foreach ($reasons as $reason) {
00135 echo "Raws Reason: " . $reason . "\n";
00136 }
00137 $details = $rawsResponse->getDetails();
00138 foreach ($details as $key => $value) {
00139 echo "Raws Detail: " . $key . " -> " . $value . "\n";
00140 }
00141 }
00142 catch (Zend_Exception $e) {
00143 echo "Caught exception: " . get_class($e) . "\n";
00144 echo "Message: " . $e->getMessage() . "\n";
00145 }