TemplateMonster XML API Script

A set of PHP scripts to import all template data from Template Monster using their XML API interface.

With these script it’s possible to create a database with all relevant template data (included in the XML code) and transfer the preview versions of templates (screenshots). After the database is created, another script is able to update the data and screenshots on a daily base.

While a simple template search example script is included, are the main functions of this script for database creation and maintenance.

Note: Using this script requires some moderate knowledge in PHP scripting.

Visit here PHP Script Download

Included functions

  • Creating a smart database model
  • Import static data tables (type, styles, categories etc.
  • Import archived template data (one BIG) XML file
  • transfer small screenshots and homepage screenshots
  • Check missing screenshots
  • Update template data an screenshots
  • Example front-end (template search and simple result)

Server requirements

You need a web server with PHP5 and MySQL5 (4.1 should work too). Because you need to import most of the data via the command line SSH access is required. We suggest to use at least a VPS server or better a dedicated server. The script is using simple_xml and the curl function to process the files. If you store both type of screenshots on your server you need ~ 2,5 GB disk space on your server (the database is ~150MB).

Creating the database and the transfer of screenshots

  1. Install the database using the included SQL script (if needed, create a database first)
  2. Download/unpack the XML files (t_info.xml, t_info_dir.xml) from the TemplateMonster API website
  3. Enter your information into the db.php file (database login, templatemonster ID’s and other information)

Using a SSH connection to your website account:

  • Import the static data (types, categories, etc.) using the file “ImportDirInfo.php” script. All data is included in the single XML file called.
  • Import the bigger XML file for all the other template data, use the script called “ImportXmlInfo.php” DON’T try to run this script via the browser
  • Transfer the screenshots from template monster to your server using the script “ImportScreenshots.php”.
  • Use the script “TestMissingImages.php” once to check/copy missing screenshots.
  • Run the Template Update script “DailyTemplateUpdate.php” once to get the latest templates and screenshots

On most web server it’s required to execute these scripts via the command line or while running a CRON job, this way you give the screenshots and directories the right permissions. For example executing the template update script via the browser will add Apache as user and group id, this might be a problem later.

Template presentation, example files

We included some basic template search with this script to show how the template data works on your own web server.

Important! Don’t change the variable names for the template search or other parts used in example files will not work anymore.

The following variable names are used in these files: category, style, author, keyword, type, price_from and price_to.

Custom PHP functions

  • thumb_box($id, $uri, $name, $author_name, $author_id, $downloads, $price, $exc_price)
    This function creates a template/thumbnail box showing the name, price and other template information. The box has links to the external template detail page, the shopping cart and to the search page..
  • check_value($ext_val, $formelement)
    This is a helper function used within the form field functions, the function checks the existing (post or get) value and returns the right value to the parent functions.
  • create_tpl_search_select($table, $select_name, $label, $curr = ”)
    This select menu will query the given database table and returns the complete html. use it for the different table to get menus for styles, categories, authors, types…
  • create_form_field($formelement, $label = ”, $ext_value = ”, $length = 25)
    A function that will create simple form field from the text type. The function gets the values via the “check_value” function and is used for the price fields and the keyword search field.

Building the SQL statement

The code to build the SQL statement is very flexible and works also without any extra input. Note that the keyword search is only for ONE word.

$sql = "SELECT t.id, t.price, t.exc_price, t.author_id, t.author_nick, t.downloads, t.software_required, t.inserted_date, pack.name, sc.uri FROM templates AS t, dir_packages AS pack, screenshots_list AS sc";
if (!empty($_GET['category'])) $sql .= ", categories AS c";
if (!empty($_GET['style'])) $sql .= ", styles AS st";
$sql .= " WHERE t.package_id = pack.id AND t.id = sc.tpl_id AND sc.small_preview = 1 AND t.state = 1 AND t.is_adult = 0";
if (!empty($_GET['category'])) $sql .= " AND t.id = c.tpl_id AND c.category_id = ".(int)$_GET['category'];
if (!empty($_GET['style'])) $sql .= " AND t.id = st.tpl_id AND st.style_id = ".(int)$_GET['style'];
if (!empty($_GET['author'])) $sql .= " AND t.author_id = ".(int)$_GET['author'];
if (!empty($_GET['keyword'])) {
    if (get_magic_quotes_gpc()) $_GET['keyword'] = stripslashes($_GET['keyword']);
    $sql .= sprintf("'%%%s%%'", mysql_real_escape_string($_GET['keyword']));
if (!empty($_GET['type'])) $sql .= " AND t.type_id = ".(int)$_GET['type'];
if (!empty($_GET['price_from'])) $sql .= " AND t.price >= ".(int)$_GET['price_from'];
if (!empty($_GET['price_to'])) $sql .= " AND t.price <= ".(int)$_GET['price_to'];
$sql .= " ORDER BY t.inserted_date DESC, t.id DESC LIMIT 0, 6";

The template loop

Using the the “thumb_box” it’s very easy to show the template data in your search result. Just enter the database fields from your resultset and you’re ready to go:

$ct = 0;
while ($obj = mysql_fetch_object($result)) {
    echo thumb_box($obj->id, $obj->uri, $obj->name, $obj->author_nick, $obj->author_id, $obj->downloads, $obj->price, $obj->exc_price);
    if ($ct == 3) echo '
<div class="clearme"></div>';

These example files should help you to start, for a successful template business you need to build additional pages for product types like WordPress themes, osCommerce templates and others.

Template data updates

Using the included update script it’s possible to update the data every day without a single click. Just add a CRON job to your account and you’re done! DON’T run the update script via the browser, in most of the cases you will have problems with permissions.

Simple WEBAPI request without using a MySQL data base

This is an simple example about how to get data from the (old) TemplateMonster WEBAPI for your website while using some PHP code (only). Of course this is not the most common way, but it works if the execution time is not the most important factor. 

// For more information: visit http://www.templatemonster.com/webapi/
$num_records = 4; 
$aff_link = "http://www.all4yourwebsite.com/";
$tm_url = "http://www.templatemonster.com/";
$tm_url .= "webapi/templates_screenshots4.php";
$param['last_added'] = "Yes"; // this value is case sensitive
$param['full_path'] = "true";
$param['order'] = "asc";
$param['sort_by'] = "date";
$param['filter'] = "1";
// add additional filters / parameters here
// building querystring from the parameters
$qs = "?";
foreach ($param as $key => $val) {
    $qs .= $key."=".$val."&";
$qs = rtrim($qs, "&");
// now get the records from the TM webapi
$request_from = $tm_url.$qs;
$all_rows = file($request_from);
// now split the data for each row into an multi dim. array
for ($i = 0; $i < $num_records; $i++) {
    $data[$i] = explode("\t", $all_rows[$i]);
$t_row = "<div id=\"thumbs\">\n";
foreach ($data as $row) {
    // read the information about more attr.
    $id = $row[0];
    $price = $row[1];
    $all_images = explode(",", trim($row[15], "{}"));
    $t_row .= "  <div class=\"thumb\">\n";
    $t_row .= "    <div class=\"box\">\n";
    foreach ($all_images as $img) {
        if (preg_match("/-m.jpg$/", $img)) {
            $thumb = $img;
    $thumb_size = getimagesize($thumb);
    $t_row .= "      <a href=\"".$aff_link."\" target=\"_blank\">";
    $t_row .= "<img src=\"".$thumb."\" ".$thumb_size[3]." border=\"0\" alt=\"template no. ".$id."\">\n";
    $t_row .= "</a>\n";
    $t_row .= "    </div>\n";
    $t_row .= "    <p>Price: $".$price."</p>\n";
    $t_row .= "  </div>\n";
$t_row .= "</div>";
// How to use? Just "echo $t_row" inside yours document body.