PHP Scripts and Classes

Import template data using the TemplateMonster API

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 (inluded 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.

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 frontend (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 diskspace 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)
  4. 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 commandline 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 fieldfunctions, 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 differnet table to get menues 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);
	$ct++;
	if ($ct == 3) echo '
'; }

These example files should help you to start, for a succesfull template business you need to build additional pages for product types like Wordpress themes, osCommerce templates and others. Check this website templates shop or the website from Template Monster.

Template data updates

Using the inluded 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.

TOP