Astroboa PHP Client
The Astroboa PHP client is build on top of the Astroboa Resource API and allows for performing all CRUD operations on any repository.
Installation
Download astroboa-php-client from github (use the provided buttons "Download as zip" or "Download as tar.gz"). Extract the zip file in some directory of your preference. Next you should include this directory in your system's PHP include path. If you are not allowed to modify the system's PHP include path, then you may do just fine by creating your programs in the same directory where you extracted the Astroboa PHP client.
Verify that everything is in place running the following:
php -r "require_once('AstroboaClient.php'); print 'Client installed';"
Create an AstroboaClient
You may create an AstroboaClient by invoking its constructor:
AstroboaClient($repositoryIPAddressOrFQDN="localhost", $repositoryName="astroboa", $username=null, $password=null)
Parameters:
- $repositoryIPAddressOrFQDN [string]: the server name or IP address that hosts one or more Astroboa repositories. Optionally you may specify the port on the server that Astroboa listens for connections on Resource API.
- $repositoryName [string]: the name of the repository.
- $username [ string ]: the username of the agent that connects to the repository
- $password [ string ]: the password of the agent that connects to the repository
If no username and password are specified, then an unauthenticated client is created. An example is shown below:
<?php
require_once("AstroboaClient.php");
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$astroboaClient = new AstroboaClient($astroboaHost, $repositoryName);
?>
You may also create an authenticated AstroboaClient which allows for creating, updating, deleting objects, taxonomies and topics in a repository as show below:
<?php
require_once("AstroboaClient.php");
$astroboaHost = "http://demo.betaconcept.com";
$repositoryName = "myrepository";
$username="demo";
$password="astroboa-demo";
$astroboaClient = new AstroboaClient($astroboaHost, $repositoryName, $username, $password);
?>
Query objects
The AstroboaClient offers two methods for quering objects, the getObjectCollection and getObjectCollectionByObjectType.
The getObjectCollectionByObjectType() allows for retrieving a collection of object of a certain type:
function getObjectCollectionByObjectType($objectType, $offset=0, $limit=50, $orderBy)
Parameters:
- objectType [string]: The type of objects to retrieve, for example personObject, organizationObject, or any user defined object type like eBookObject, movieObject, etc
- offset [integer]: offset to start fetching objects from. Used with limit for pagination
- limit [integer]: the number of objects to retrieve
- orderBy [String]: a comma delimited list of properties; for example profile.title, profile.modified
Response:
The getObjectCollectionByType methods return a JSON decoded array of retrieved objects.
The code listing below presents how to retrieve e-book objects found in some repository ordered by their title:
<?php
require_once('AstroboaClient.php');
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$astroboaClient = new AstroboaClient($astroboaHost, $repositoryName);
$eBookCollection = $astroboaClient->getObjectCollectionByObjectType("eBookObject", 0, 50, "profile.title");
print "Total books found: " . $eBookCollection["totalResourceCount"];
foreach($eBookCollection["resourceCollection"]["resource"] as $eBook) {
print "Title : " . $eBook["profile"]["title"];
print "\n";
}
?>
The getObjectCollection method allows for performing more fine grained object queries:
function getObjectCollection($query, $projectionPaths, $offset=0, $limit=50, $orderBy)
Parameters:
- cmsQuery [string]: a query that defines which objects should be retrieved. Read more on cmsQuery usage.
- projectionPaths [string]: a comma delimited list of property paths that specify which properties the returned object should contain. Usage of projectionPaths is highly recommended to achieved fast response times.
- offset [integer]: offset to start fetching objects from. Used with limit for pagination.
- limit [integer]: the number of object to retrieve
- orderBy [string]: a comma delimited list of object properties; for example profile.title, profile.modified
Response:
The getObjectCollection methods return a JSON decoded array of retrieved objects.
The following code listing presents how to retrieve all objects whose title (actually profile.title) contains the word 'peace' and fetch only the first 20 objects ordered by their modification time (profile.modified). The query will not return all the properties of the objects found, but only their profile property. Note that different object types may be fetched as we did not constrain the object type. e.g. fetch only book or articles.
<?php
require_once("AstroboaClient.php");
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$client = new AstroboaClient($astroboaHost, $repositoryName);
$eBookCollection = $client->getObjectCollection("profile.title CONTAINS \"peace\"",
"profile",
0,
20,
"profile.modified");
print "Total books found: " . $eBookCollection["totalResourceCount"] . "\n";
foreach ($eBookCollection["resourceCollection"]["resource"] as $eBook) {
print "Title : " . $eBook["profile"]["title"] . "\n";
}
?>
Retrieve a single object
The getObjectByIdOrName method returns an object identified by its id or name:
function getObjectByIdOrName($idOrName)
Parameters:
- idOrName [string] : the id or system name of the object to retrieve
Response:
The getObjectIdOrName method returns a JSON decoded representation of the retrieved object.
Create a new object
The addObject method adds a new object to a repository. You have to use an authenticated AstroboaClient to add a new object to a repository:
function addObject($object)
Parameters:
-
$object [array] : The $object parameter is an array/map that describes the object's structure and its property values. You should always include the contentObjectTypeName property to specify the type of the object you are about to add. Alongside you should provide all mandatory properties that the object requires. Here is an example:
<?php $objectToAdd = array(); $objectToAdd["contentObjectTypeName"] = "genericContentObjectResourceObject"; $objectToAdd["profile"] = array("title" => "Title for the text object"); ?>
Create a new object with binary data
The addObjectWithBinaryData() method adds a new object with binary data to a repository. As with regular objects you have to use an authenticated AstroboaClient to add a new object with binary data to a repository:
function addObjectWithBinaryData($object, $multiparts)
Parameters:
- $object [array]: The $object parameter is an array/map that describes the object's structure and its property values. You should always include the contentObjectTypeName property to specify the type of the object you are about to add.
- $multiparts [array]: The $multiparts parameter is a map that holds information about binary data attached to the $object to add. You must not set the value of the $multiparts yourself. You should rather use the setBinaryProperty method.
The setBinaryProperty method prepares a binary property of an object to accept the binary data of a file:
function setBinaryProperty(&$objectBinaryProperty, &$multiparts, $filenamePath)
Parameters:
- &$objectBinaryProperty: The &$objectBinaryProperty is a reference to an object's binary property (a property whose type is binaryChannel). For example, to refer to the image property of the genericContentResourceObject, the objectBinaryProperty should be &$object["image"] assuming that $obj represents the genericContentResourceObject to add. Similarly, to refer to the content property of the fileResourceObject, the objectBinaryProperty should be &$obj["content"]. When working with multi-value properties like the fileResource of the arrayOfFileResourceTypeObject ( a fileResource is a complex property that defines the content property whose type is binaryChannel), the $objectBinaryProperty must refer to a single binary channel property, for example $obj["fileResource"][0]["content"], $obj["fileResource"][1]["content"], etc.
- &$multiparts: The $multiparts parameter is a map that holds information about binary data attached to the $object to add. You should provide an empty array here or an array that was previously set by the setBinaryProperty() method.
- $filenamePath [string]: a full path to a file
The code listing below presents how to add a new genericContentResourceObject with an image attached:
<?php
require_once("AstroboaClient.php");
$serverName = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($serverName, $repositoryName, $username, $password);
$filenamePath = "/tmp/rainbow.png";
$object = array("contentObjectTypeName" => "genericContentResourceObject");
$object["profile"] = array ("title" => "rainbow", "contentObjectStatus" => "published");
$multiparts = array();
// &$object["image"] entry will be created by setBinaryProperty
$client->setBinaryProperty(&$object["image"], &$multiparts, $filenamePath);
$client->addObjectWithBinaryData($object, $multiparts);
?>
Here is how to add a fileResource object with a pdf file attached:
<?php
require_once("AstroboaClient.php");
$serverName = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($serverName, $repositoryName, $username, $password);
$filenamePath = "/tmp/guide.pdf";
$object = array("contentObjectTypeName" => "fileResourceObject");
$object["profile"] = array ("title" => "some-guide", "contentObjectStatus" => "published");
$multiparts = array();
// &$object["content"] entry will be created by setBinaryProperty
$client->setBinaryProperty(&$object["content"], &$multiparts, $filenamePath);
$client->addObjectWithBinaryData($object, $multiparts);
?>
The arrayOfFileResourceTypeObject allows for creating a list of files or in other words a file list collection. The code listing below shows how to create a file list consisting of an image and pdf file
<?php
require_once("AstroboaClient.php");
$serverName = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($serverName, $repositoryName, $username, $password);
$object = array("contentObjectTypeName" => "arrayOfFileResourceTypeObject");
$object["profile"] = array ("title" => "my-file-list", "contentObjectStatus" => "published");
$multiparts = array();
$object["fileResource"] = array();
$object["fileResource"][0] = array("title" => "bridge");
$filenamePath = "/tmp/bridge.jpg";
// &$object["fileResource"][0]["content"] entry will be created by setBinaryProperty
$client->setBinaryProperty(&$object["fileResource"][0]["content"], &$multiparts, $filenamePath);
$object["fileResource"][1] = array("title" => "guide");
$filenamePath = "/tmp/guide.pdf";
// &$object["fileResource"][1]["content"] entry will be created by setBinaryProperty
$client->setBinaryProperty(&$object["fileResource"][1]["content"], &$multiparts, $filenamePath);
$client->addObjectWithBinaryData($object, $multiparts);
?>
Update an object
The updateObject method updates an already existing object in a repository. You have to use an authenticated AstroboaClient to update an existing object:
function updateObject($object)
Parameters:
-
$object [array]: The $object parameter is an array/map that describes the object's structure and its property values. You should always include the contentObjectTypeName property to specify the type of the object you are about to add. Besides that you need to set a value for key 'cmsIdentifier' which designates which object to update. Here is a valid sample for updating a personObject:
<?php $objectToUpdate = array(); $objectToUpdate["contentObjectTypeName"] = "personObject"; $objectToUpdate["cmsIdentifier"] = "joe-doe"; $objectToUpdate["name"] = array("givenName" => "Joe", "familyName" => "Doe"); ?>
Update an object with Binary Data
The updateObjectWithBinaryData() method updates an existing object which contains binary data. As with regular objects you have to use an authenticated AstroboaClient to add a new object with binary data to a repository:
function updateObjectWithBinaryData($object, $multiparts)
Parameters:
- $object [array]: The $object parameter is an array/map that describes the object's structure and its property values. You should always include the contentObjectTypeName property to specify the type of the object you are about to add.
- $multiparts [array]: The $multiparts parameter is a map that holds information about binary data attached to the $object to add. You must not set the value of the $multiparts yourself. You should rather use the setBinaryProperty method.
You should use the setBinaryProperty to prepare an object's property to accept binary data in the same way as when you are creating an object with binary data.
The code listing below shows how to retrieve an array of file resources and update its first file resource:
<?php
require_once('AstroboaClient.php');
$astroboaHost = "demo.betaconcept.com";
$repository = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($astroboaHost, $repository, $username, $password);
if (! is_null($client)) {
print "Client successfully initialized\n";
}
$filelist = $client->getObjectByIdOrName("test-file-list");
$filenamePath = "/tmp/somefile.pdf";
$multiparts = array();
$client->setBinaryProperty(&$filelist["fileResource"][0]["content"], &$multiparts, $filenamePath);
$client->updateObjectWithBinaryData($filelist, $multiparts);
?>
Delete an Object
The deleteObjectByIdOrName method removes an of a repository. You have to use an authenticated AstroboaClient to delete an object:
function deleteObjectByIdOrName($objectIdOrName)
Parameters:
- $objectIdOrName [string]: the id or system name of object to remove
Retrieve a single Taxonomy
The getTaxonomyByIdOrName method returns a taxonomy identified by its id or name.
function getTaxonomyByIdOrName($idOrName)
Parameters
- idOrName [string] : the id or system name of the taxonomy to retrieve
Response:
The getTaxonomyByIdOrName method returns a JSON decoded representation of the retrieved taxonomy
Create a new Taxonomy
The addTaxonomy method creates a new taxonomy in a repository. You have to use an authenticated AstroboaClient to create a new taxonomy:
function addTaxonomy($taxonomy)
Parameters:
- $taxonomy [array] : The $taxonomy parameter is an array/map that describes the taxonomy's structure and its property values. Here is an example:
Here is how to create a music taxonomy:
// The user demo@astroboa-demo (user demo of repository astroboa-demo)
// is not allowed to create/update/delete taxonomies
<?php
require_once("AstroboaClient.php");
$client = new AstroboaClient("demo.betaconcept.com", "astroboa-demo", "demo", "astroboa-demo");
$taxonomyToAdd = array();
$taxonomyToAdd["name"] = "music-taxonomy";
$taxonomyToAdd["localization"] = array("label" => array ("en" => "Music"));
$resp = $client->addTaxonomy($taxonomyToAdd);
print "Response for add taxonomy: $resp[http_code] \n";
?>
Update a Taxonomy
The updateTaxonomy method updates an already existing taxonomy in a repository. You have to use an authenticated AstroboaClient to update a taxonomy:
function updateTaxonomy($taxonomy)
Parameters:
- $taxonomy [array]: The $taxonomy parameter is an array/map that describes the taxonomy's structure and its property values. The $taxonomy array should always contain the "cmsIdentifier" key or else an Exception is thrown. The following code snippet shows how to create update a taxonomy's english localization label.
The code snippet below shows how to update the music taxonomy and add a label for French.
// The user demo@astroboa-demo (user demo of repository astroboa-demo)
// is not allowed to create/update/delete taxonomies
<?php
require_once("AstroboaClient.php");
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($astroboaHost, $repositoryName, $username, $password);
$taxonomyToUpdate = array();
$taxonomyToUpdate["cmsIdentifier"] = "d53be6e0-2db7-4b8f-430F-8a3849b2925f";
$taxonomyToUpdate["localization"] = array("label" => array("en" => "Music", "fr" => "Musique"));
$resp = $client->updateTaxonomy($taxonomyToUpdate);
print "Response for update taxonomy: $resp[http_code] \n";
?>
Delete a Taxonomy
The deleteTaxonomyByIdOrName method removes a taxonomy of a repository. You have to use an authenticated AstroboaClient to delete a taxonomy:
function deleteTaxonomyByIdOrName($topicIdOrName)
Parameters:
- $taxonomyIdOrName [string]: the id or system name of taxonomy to remove
Query Topics
The getTopicCollection() allows for retrieving a collection of topics specified by a query:
function getTopicCollection($query, $projectionPaths, $offset=0, $limit=50, $orderBy)
Parameters:
- cmsQuery [string]: a query that defines which topics should be retrieved. Read more on query usage.
- projectionPaths [string]: a comma delimited list of property paths that specify which properties the returned topic should contain. Usage of projectionPaths is highly recommended to achieved fast response times.
- offset [integer] : offset to start fetching topics from. Used with limit for pagination.
- limit [integer] : number of topic to retrieve.
- orderBy [string] : a comma delimited list of topic properties, TBD:show example
Retrieve a single Topic
The getTopicByIdOrName retrieves a single topic from a repository.
The getTopicByIdOrName method returns a topic identified by its id or name:
function getTopicByIdOrName($idOrName)
Parameters:
- idOrName [string] : the id or system name of the topic to retrieve
Response:
The getTopicByIdOrName method returns a JSON decoded representation of the retrieved taxonomy:
Create a new Topic
The addTopic method add a new topic to a repository. You have to use an authenticated AstroboaClient to create a new topic.
function addTopic($topic)
Parameters:
- $topic [array] : The $topic parameter is an array/map that describes the topic's structure and its property values.
Here is how to create a rock-music topic and place in music taxonomy:
// The user demo@astroboa-demo (user demo of repository astroboa-demo)
// is not allowed to create/update/delete topics
<?php
require_once("AstroboaClient.php");
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($astroboaHost, $repositoryName, $username, $password);
$topicToAdd = array();
$topicToAdd["name"] = "rock-music";
$topicToAdd["localization"] = array("label" => array ("en" => "Rock music"));
$topicToAdd["taxonomy"] = array("name" => "music-taxonomy");
$resp = $client->addTopic($topicToAdd);
print "Response for add topic: $resp[http_code] \n";
?>
Update a Topic
The updateTopic method updates an already existing topic in a repository. You have to use an authenticated AstroboaClient to update a topic:
function updateTopic($topic)
Parameters:
- $topic [array]: The $topic parameter is an array/map that describes the topic's structure and its property values. You should always include a value for key "cmsIdentifier" which designates which topic you intend to update.
Here is an example which set the allowsReferrerContentObjects to false, so as no object can use this topic as tag:
// The user demo@astroboa-demo (user demo of repository astroboa-demo)
// is not allowed to create/update/delete topics
<?php
require('AstroboaClient.php');
$astroboaHost = "demo.betaconcept.com";
$repositoryName = "astroboa-demo";
$username = "demo";
$password = "astroboa-demo";
$client = new AstroboaClient($astroboaHost, $repositoryName, $username, $password);
$topicToUpdate = array();
$topicToUpdate["cmsIdentifier"] = "d53be6e0-2db7-4a8f-8034-8a3849a3b25b";
$topicToUpdate["allowsReferrerContentObjects"] = false;
$resp = $client->updateTopic($topicToUpdate);
print "Response for update: $resp[http_code] \n";
?>
Delete a Topic
The deleteTopic method removes a topic of a repository. You have to use an authenticated AstroboaClient to delete a topic:
function deleteTopicByIdOrName($topicIdOrName)
Parameters:
- $topicIdOrName [string]: the id or system name of topic to remove
Last Modified: 17 December 2011
Loading...