02: GETTING A LIST OF COLLECTIONS

API: dmGetCollectionList

INVOKED: <server address>/dmwebservices/index.php?q=dmGetCollectionList/xml

EXAMPLE: https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetCollectionList/xml

The resulting XML records look like:

<collections>
   <collection>
      <alias>/isl6</alias>
      <name>Abraham Lincoln - Documents</name>
      <path>/cdm/sites/16614/data/isl6</path>
      <secondary_alias>isl6</secondary_alias>
   </collection>
   <collection>
      <alias>/algonqui001</alias>
      <name>Algonquin and Lake in the Hills Local History</name>
      <path>/cdm/sites/16614/data/algonqui001</path>
      <secondary_alias>algonqui001</secondary_alias>
   </collection>
   <collection>
      <alias>/arlingto001</alias>
      <name>Arlington Heights History</name>
      <path>/cdm/sites/16614/data/arlingto001</path>
      <secondary_alias>arlingto001</secondary_alias>
   </collection>

Etc.

dmGetCollectionList.php

<?php

$xmlData = file_get_contents('https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetCollectionList/xml');

// Create the document object

$xml = simplexml_load_string($xmlData);

// simplexml_load_string interprets a string of XML ($xml) into an object

$result = array();

// Initializes an array

foreach ($xml->xpath('//collection') as $record) {

// Get the nodes and loop them

$result[] = array(
'alias' => (string) $record->alias,
'name' => (string) $record->name
);

// This loads the data in $xml as key:value pairs into the array $result
}

$resultCount = count($result) - 1;

// How many records are in $result? And, since the first element is 0, subtract 1

for ($i=0;$i<=$resultCount;$i++) {

// Loop through the array $result and display the key:value pairs for each element $i, and then print them

$alias = $result[$i]["alias"];
$name = $result[$i]["name"];
echo "$name: $alias\n";
}

?>

Now, take the same basic code and build a web page around it:

dmGetCollectionListWebPage.php

<?php
$xmlData = file_get_contents('https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetCollectionList/xml');

$xml = simplexml_load_string($xmlData);

$result = array();

foreach ($xml->xpath('//collection') as $record) {
        $result[] = array(
                'alias' => (string) $record->alias,
                'name' => (string) $record->name
        );
}

$resultCount = count($result) - 1;
?>

<html>
        <head>
                <title>My Collections</title>
        </head>
        <body>
                <div id="header">
                        <h1 style="text-align: center;">My Collections</h1>
                </div>
                <div id="list">
                        <ol>
                        <?php
                                for ($i=0;$i<=$resultCount;$i++) {
                                        $alias = $result[$i]["alias"];
                                        $name = $result[$i]["name"];
                                        $alias = str_ireplace("/", "", "$alias");
                                        echo "\t\t<li><strong>$name</strong> <em>($alias)</em></li>\n";
                                }
                        ?>
                        </ol>
                </div>
                <div id="footer">
                        <p />
                        <strong>Our Library</strong>
                </div>
        </body>
</html>