Use ProcessWire’s API in other PHP apps and shell scripts… It’s easy!

ProcessWire's API and data can be used from other PHP scripts, including command line PHP scripts. This is a simple matter of just including ProcessWire's  ./index.php file from any other PHP script.


It doesn't matter if your intended use is HTTP or command line...ProcessWire will auto-detect the state from which it was included.

Once you've included ProcessWire like in the example above, the API is now available to you in the $wire global variable, or via the wire() function. For example, here is how you would retrieve a fictional "Contact Us" page from ProcessWire's $pages API variable. These two are equivalent, so what syntax you prefer to use is up to you:

$mypage = $wire->pages->get("/about/contact/"); 
$mypage = wire('pages')->get("/about/contact/"); 

You can access all of the same API variables that you can from a template, except that there is no default $page variable accessible from the API since ProcessWire is not handling the web request. Of course, you are welcome to retrieve any page you want from the $pages API variable. Likewise you can modify, save, and delete pages as usual.

Creating a ProcessWire command-line script

Here is an example of a command line shell script called that outputs all pages in the site in an indented site map. We do this with a very small amount of code by creating a function called listPage that we use recursively:

include("/home/ryan/www/index.php"); // bootstrap ProcessWire
function listPage($page, $level = 0) {
        echo str_repeat("    ", $level) . $page->title . "\n";
        foreach($page->children as $child) listPage($child, $level+1);
listPage($wire->pages->get("/")); // start at homepage

Note that you would need to replace the lines containing paths to have the equivalent paths on your machine (lines 1 and 3). You would also make the shell script executable, i.e.

chmod +x

Next you would run the shell script:


And here is what the output looked like when I ran it here: 

        What's Unique?
        Why ProcessWire?
    Developer API
        What is the API?
        Template Files
        Plugin Modules

Including ProcessWire from another PHP script

ProcessWire can be included from any other PHP script in the same manner it was included from the command line script above. Just include ProcessWire's root ./index.php file, and you have full access to it's API.

We'll use the same example as in the shell script above, except that we'll use markup to make an HTML5 page with semantic lists and links to each page. We'll call this file sitemap.php and assume it lives in the web root where ProcessWire's index.php file is:

<!DOCTYPE html>
<html lang="en">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Site Map</title>
    <h1>Site Map</h1>
    include("./index.php"); // bootstrap ProcessWire
    function listPage($page, $level = 0) {
        echo "<li><a href='{$page->url}'>{$page->title}</a>";
        if($page->numChildren) {
            echo "<ul>";
            foreach($page->children as $child) listPage($child, $level+1);
            echo "</ul>";
        echo "</li>";
    <?php listPage($wire->pages->get("/")); ?>

Now see the actual live result of this script when run on this site.

You can do anything with ProcessWire's API that you can do from a template, so all of this still applies, as does everything in the developer API.


  • Claes

    Claes 3 years ago 71

    Beautiful, think I'm falling in love with this CMS!

  • Pawel

    Pawel 3 years ago 12

    I run one PW in another? In this way? I need to display data from one PW in another.

    • Can

      Can 2 years ago 41

      There is multi-instance support coming in PW3, either wait for full documentation by Ryan or try from LostKobrakai

      Other option is to make use of the service pages module or manually provide the needed data as json, xml or whatever and then process and display on the other page..

    • Can

      Can 2 years ago 21

      BY the way..the forums are a better place for support questions..the comments here in the blog are not really reviewed especially from old articles... ;-)

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.