So, why would I use it?
This is the first question that I would typically ask, and the truth is, usually you don't need to. Matrix offers a lot of functionality out of the box. For example, there is a powerful SOAP API. You'll need to make a judgement call on your particular scenario and situation. However, here is when I've typically found myself using the PHP based API:
- Performance - making large amounts of calls over HTTP can be slow in certain environments.
- Integration - I have an application outside of Matrix and I need them to talk and SOAP simply won't suffice.
- Limitations
Prerequisites
Before you can execute any PHP code directly from the terminal, you will need to make sure that you have the "php5-cli" package installed.
To install run the following command:
sudo apt-get install php5-cli
The code
Below is the code that you will need to run to create Standard Page assets. For full instructions on how to run this code, refer to the section below.
There are a couple of things you'll need to take note off so be sure to read the comments (or evil exception monkeys will come and attack you).
<?php // First, include the Matrix bootstrap // When ever a user makes any request to a Matrix page, this file is loaded (i.e. in the index.php file) $SYSTEM_ROOT = "/home/websites/mysource_matrix/"; require_once $SYSTEM_ROOT . "core/include/init.inc"; // Now we need to log in // The easiest user to work with is Root User $root_user = $GLOBALS["SQ_SYSTEM"]->am->getSystemAsset("root_user"); if (!$GLOBALS["SQ_SYSTEM"]->setCurrentUser($root_user)) { echo "Could not login as root user"; } // Retrieve the parent asset $PARENT_ID = "XXXXX"; // replace XXXXX with the asset id of the parent you wish to be using $parent = $GLOBALS["SQ_SYSTEM"]->am->getAsset($PARENT_ID); // Load up the asset we wish to be creating $GLOBALS['SQ_SYSTEM']->am->includeAsset("page_standard"); $asset = new Page_Standard(); // Define it's starting attributes // These are the same attributes you would find if you were to right click in the asset map and select the "New Child" option $asset->setAttrValue("name", "A simple web page"); // Now let's specify where it should be linked and how (i.e. is it TYPE_2?) $linky = array( "asset" => $parent, "link_type" => SQ_LINK_TYPE_2, "is_dependant" => 0, "is_exclusive" => 0, ); // Piece of pie. Create the asset! if (!$asset->create($linky)) { echo "Could not create asset\n"; } else { echo "Asset created: " . $asset->id . "\n"; } ?>
Running the script
To run the script, follow these instructions:
-
Create a file in your /home/websites/ directory and call it "asset_builder.php"
cd /home/websites/
pico asset_builder.php
- Copy and paste the code from above in to this file
- Replace "XXXXX" to the parent asset ID where assets will be created
-
Save and run the script from the terminal:
sudo php asset_builder.php