rass_tag_set.php

00001 <?php
00002 # Sample script for RAWS tutorial 8: see http://rambla.eu/ 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://rass.' . CDN . '.rambla.be/');
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   # Create a "tutorial8" directory under the root-directory
00035   $dir_entry = $rass->createDir("tutorial8", True);
00036   echo "Created directory with path: " . $dir_entry->path . "\n";
00037   
00038   # Upload a file 'test.mp4' to your dir
00039   $item_entry = $rass->postItem($dir_entry->path, "test.mp4", LOCAL_FILE);
00040   echo "Created file with path: " . $item_entry->path . "\n";
00041 
00042   # We can now attach tags to our uploaded file
00043   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(new Rass_Extension_Tag("animal"), new Rass_Extension_Tag("bird")), array());
00044   echo "\nTagged " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (set 'animal' and 'bird')\n";
00045   foreach ($meta_entry->content->params->tag as $t) {
00046     echo "Tag: " . $t->text . "\n";
00047   }
00048   
00049   # If you want to keep your existing tags + add new one(s) => use addMetadata()
00050   $meta_entry = $rass->getMetaEntry($item_entry->path);
00051   $meta_entry = $rass->addMetadata($meta_entry, array(new Rass_Extension_Tag("mockumentary")), array());
00052   echo "\nTagged " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (added 'mockumentary')\n";
00053   foreach ($meta_entry->content->params->tag as $t) {
00054     echo "Tag: " . $t->text . "\n";
00055   }
00056 
00057   # To replace a particular tag with another one, modify the array of Rass_Extension_Tag objects
00058   foreach ($meta_entry->content->params->tag as $t) {
00059     # replace the "mockumentary" tag with "documentary"
00060     if ($t->text == "mockumentary") {
00061       $t->text = "documentary";
00062     }
00063   }
00064   # a new call to addMetadata() will do a POST of our meta_entry (with no new tags added, but one replaced) 
00065   $meta_entry = $rass->addMetadata($meta_entry, array(), array());
00066   echo "\nTagged " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (replaced 'mockumentary' with 'documentary')\n";
00067   foreach ($meta_entry->content->params->tag as $t) {
00068     echo "Tag: " . $t->text . "\n";
00069   }
00070   
00071   # If you use setMetadata() or setMetadataFromPath(), existing tags will be removed (only the ones passed in the argument are being set)
00072   $meta_entry = $rass->setMetadata($meta_entry, array(new Rass_Extension_Tag("ohno")), array());
00073   echo "\nTagged " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (set 'ohno')\n";
00074   foreach ($meta_entry->content->params->tag as $t) {
00075     echo "Tag: " . $t->text . "\n";
00076   }
00077 
00078   // # Delete all metadata for this entry (if you only have the relative path, use deleteMetaEntry_from_path() instead)
00079   // $meta_entry = $rass->deleteMetaEntry($meta_entry);
00080   // # We can still retrieve the meta entry, but it will not contain any tags
00081   // $meta_entry = $rass->getMetaEntry($item_entry->path);
00082   // echo "\nAll metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " has been removed !\n";
00083   // foreach ($meta_entry->content->params->tag as $t) {
00084   //   echo "Tag: " . $t->text . "\n";
00085   // }
00086   // 
00087   // # DELETE directory on the CDN. Set second argument to True to delete recursively.
00088   // $rass->deleteDir($dir_entry->path, True);
00089   // echo "Deleted dir: " . $dir_entry->path . "\n";
00090 
00091 }
00092 catch(Zend_Gdata_App_Exception $e) {
00093     # Report the exception to the user
00094     echo "\nCaught exception: " . get_class($e) . "\n";
00095     echo "Message: " . $e->getMessage() . "\n";
00096     # get the HTTP status code
00097     echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00098     echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00099     # get the raws:error elements
00100     $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00101     echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00102     echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00103     $reasons = $rawsResponse->getReasons();
00104     foreach ($reasons as $reason) {
00105       echo "Raws Reason: " . $reason . "\n";
00106     }
00107     $details = $rawsResponse->getDetails();
00108     foreach ($details as $key => $value) {
00109       echo "Raws Detail: " . $key . " -> " . $value . "\n";
00110     }
00111 }
00112 catch (Zend_Exception $e) {
00113     echo "Caught exception: " . get_class($e) . "\n";
00114     echo "Message: " . $e->getMessage() . "\n";
00115 }

Generated on Mon Mar 28 15:03:20 2011 for RawsClient PHP by  doxygen 1.5.3