rass_meta_create.php

00001 <?php
00002 # Sample script for RAWS tutorial 10: 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 "tutorial10" directory under the root-directory
00035   $dir_entry = $rass->createDir("tutorial10", 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   # Set the following element: <meta vocab="dc" name="title">The lion sleeps tonight</title>
00043   echo "\nSetting metadata for " . $item_entry->kind . " entry with path = " . $item_entry->path . " (<meta vocab='dc' name='title'>The lion sleeps tonight</title>)\n";
00044   $meta_elems = array(new Rass_Extension_Meta("dc", "title", "The lion sleeps tonight"));
00045   $meta_entry = $rass->setMetadataFromPath($item_entry->path, array(), $meta_elems);
00046   foreach ($meta_entry->content->params->meta as $t) {
00047     echo "Vocab: " . $t->vocab . "\n";
00048     echo "Name: " . $t->name . "\n";
00049     echo "Value: " . $t->text . "\n";
00050   }
00051 
00052   # Add the following element: <meta vocab="media" name="price" type="rent" price="19.99" currency="EUR" />
00053   echo "\nAdding meta element to " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " (<meta vocab='media' name='price' type='rent' price='19.99' currency='EUR' />)\n";
00054   $meta_elems = array(new Rass_Extension_Meta("media", "price", null, array("type" => "rent", "price" => "19.99", "currency" => "EUR")));
00055   $meta_entry = $rass->addMetadata($meta_entry, array(), $meta_elems);
00056   foreach ($meta_entry->content->params->meta as $t) {
00057     echo "\nVocab: " . $t->vocab . "\n";
00058     echo "Name: " . $t->name . "\n";
00059     if ($t->text) {
00060       echo "Value: " . $t->text . "\n";
00061     }
00062     foreach ($t->attrs as $key => $value) {
00063       echo "Attr: " . $key . "=" . $value . "\n";
00064     }
00065   }
00066   
00067   # To replace a data inside an existing meta element, you can modify the array of Rass_Extension_Meta objects...
00068   echo "\nChanging price and adding subscription info for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . "\n";
00069   $meta_entry = $rass->getMetaEntry($item_entry->path);
00070   foreach ($meta_entry->content->params->meta as $t) {
00071     if (($t->name == "price") && ($t->vocab == "media")) {
00072       $a = $t->attrs;
00073       $a["price"] = "20.00";
00074       $a["info"] = "http://www.dummy.jp/subscription_info";
00075       $t->attrs = $a;
00076     }
00077   }
00078   # ... followed by a call to addMetadata() to POST our meta_entry (you can also add new elements at the same time) 
00079   $meta_entry = $rass->addMetadata($meta_entry, array(), array());
00080   foreach ($meta_entry->content->params->meta as $t) {
00081     echo "\nVocab: " . $t->vocab . "\n";
00082     echo "Name: " . $t->name . "\n";
00083     if ($t->text) {
00084       echo "Value: " . $t->text . "\n";
00085     }
00086     foreach ($t->attrs as $key => $value) {
00087       echo "Attr: " . $key . "=" . $value . "\n";
00088     }
00089   }
00090       
00091   # Note: If you use setMetadata() or setMetadataFromPath(), existing meta elements will be removed (only the ones passed in the argument are being set)
00092   echo "\nReplacing metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . ". Adding custom 'myvocab' elements.\n";
00093   $meta_elems = array();
00094   $meta_elems[] = new Rass_Extension_Meta("myvocab", "animal", "lion", array("description" => "sleeping at night"));
00095   $meta_elems[] = new Rass_Extension_Meta("myvocab", "like", null, array("likes" => "1520", "dislikes" => "2"));
00096   $meta_entry = $rass->setMetadata($meta_entry, array(), $meta_elems);
00097   foreach ($meta_entry->content->params->meta as $t) {
00098     echo "\nVocab: " . $t->vocab . "\n";
00099     echo "Name: " . $t->name . "\n";
00100     if ($t->text) {
00101       echo "Value: " . $t->text . "\n";
00102     }
00103     foreach ($t->attrs as $key => $value) {
00104       echo "Attr: " . $key . "=" . $value . "\n";
00105     }
00106   }
00107   
00108     // # Delete all metadata for this entry (if you only have the relative path, use deleteMetaEntry_from_path() instead)
00109   // echo "\nDeleting manual metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . "\n";
00110   // $rass->deleteMetaEntry($meta_entry);
00111   // # We can still retrieve the meta entry, but it will not contain any tags
00112   // $meta_entry = $rass->getMetaEntry($item_entry->path);
00113   // echo "\nAll metadata for " . $meta_entry->kind . " entry with path = " . $meta_entry->path . " has been removed !\n";
00114   // foreach ($meta_entry->content->params->meta as $t) {
00115   //   echo "\nVocab: " . $t->vocab . "\n";
00116   //   echo "Name: " . $t->name . "\n";
00117   //   if ($t->text) {
00118   //     echo "Value: " . $t->text . "\n";
00119   //   }
00120   //   foreach ($t->attrs as $key => $value) {
00121   //     echo "Attr: " . $key . "=" . $value . "\n";
00122   //   }
00123   // }
00124   // 
00125   // 
00126   // # DELETE directory on the CDN. Set second argument to True to delete recursively.
00127   // $rass->deleteDir($dir_entry->path, True);
00128   // echo "Deleted dir: " . $dir_entry->path . "\n";
00129   
00130 
00131 }
00132 catch(Zend_Gdata_App_Exception $e) {
00133     # Report the exception to the user
00134     echo "\nCaught exception: " . get_class($e) . "\n";
00135     echo "Message: " . $e->getMessage() . "\n";
00136     # get the HTTP status code
00137     echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
00138     echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
00139     # get the raws:error elements
00140     $rawsResponse = Raws::parseRawsResponse($e->getResponse());
00141     echo "Raws Code: " . $rawsResponse->getCode() . "\n";
00142     echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
00143     $reasons = $rawsResponse->getReasons();
00144     foreach ($reasons as $reason) {
00145       echo "Raws Reason: " . $reason . "\n";
00146     }
00147     $details = $rawsResponse->getDetails();
00148     foreach ($details as $key => $value) {
00149       echo "Raws Detail: " . $key . " -> " . $value . "\n";
00150     }
00151 }
00152 catch (Zend_Exception $e) {
00153     echo "Caught exception: " . get_class($e) . "\n";
00154     echo "Message: " . $e->getMessage() . "\n";
00155 }

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