Quick start for P4 API for PHP on Windows

Before you begin

Ensure you have the following:

  • PHP 8.2 or higher.

  • P4 API for C/C++ 2026.1.

  • Access to a running P4 Server 2026.1.

  • The server and port for your P4 Server (for example, localhost:1666)

  • Your P4 username. To find it, run p4 users.

  • The name of a workspace containing files you want to work with. To find a list of workspace names, run p4 clients.

  • (Optional) P4V, to verify changes and changelists.

Install P4 API for PHP

On Windows, P4 API for PHP is installed by downloading .dll files and enabling it in php.ini.

The steps below provide a high-level overview of the installation process:

  1. Download the latest stable version of PHP from the PHP downloads page.

    • Select a Thread Safe (TS) version.

    • Select the version that matches your system (for example, x64).

  2. Extract PHP to a known location, for example: C:\php.

  3. Go to the Perforce archive and open the folder for the latest release (for example r25.2 for P4 API for PHP 2025.2).

  4. Download the P4 API for PHP file that matches your PHP version. For example, download perforce_php84ts.dll for PHP 8.4 TS (Thread Safe).

  5. Move the .dll file to the PHP extensions directory. For example: C:\php\ext.

  6. In C:\php, open the php.ini file.

  7. Add the following line in the Dynamic Extensions section:

    extension=perforce_php84ts.dll

  8. Enable OpenSSL.

    • In php.ini, find the following line and remove the leading ;:

      ;extension=openssl

    • So it becomes:

      extension=openssl

  9. Save the php.ini file.

  10. (Optional) Restart the Command Prompt to make sure the changes take effect.

  11. Verify the extension is loaded. In Command Prompt, run:

    php -m

  12. You should see perforce and openssl listed in the modules in the output.

To learn more on how to install P4 API for PHP, see Installing P4 API for PHP.

Connect the API to P4 Server

Use the following script to connect to P4 Server and view the first ten files in a given workspace.

Update the following credentials in the script:

  • port - Add the server and port to connect to in the format: server:port

  • user - A username for your P4 user.

  • client - The name of the workspace you want to connect to.

Call the script something simple like connect_to_p4.php

Copy
<?php

$p4 = new P4();

// --- Set connection details ---
$p4->port   = "localhost:1666";       // server:port
$p4->user   = "P4UserName";          // username
$p4->client = "php_projects_main";  // workspace name

try {
    // Connect to the server
    $p4->connect();
    echo "Connected!\n";

    // Get server info
    $info = $p4->run("info");
    if (isset($info[0]["serverAddress"])) {
        echo "Connected to: " . $info[0]["serverAddress"] . "\n";
    }

    // Get up to 10 files in the workspace
    $files = $p4->run("have");

    $count = 0;
    foreach ($files as $file) {
        if ($count++ >= 10) {
            break;
        }
        echo " - " . $file["depotFile"] . " #" . $file["haveRev"] . "\n";
    }

    // Disconnect
    $p4->disconnect();
    echo "Disconnected from the P4 Server.\n";

} catch (P4_Exception $e) {
    // Display error messages
    echo "Connection failed: " . $e->getMessage() . "\n";

    foreach ($p4->errors as $error) {
        echo "Error: $error\n";
    }
}

?>

To run the script:

  1. Open a terminal and navigate to the folder containing the script.

  2. Run php connect_to_p4.php (If you have given the script a different name, use that name).

  3. Check the output.

You should see up to 10 files listed in your chosen workspace.

If you receive any errors when connecting to the P4 Server, see Troubleshooting.

Check out a file

The following script will:

  • Connect to P4 Server with your credentials (user and workspace).

  • Create a pending changelist with a simple description.

  • Open the target file for edit in that changelist.

  • Return a brief summary of what has been done.

    • This returns a changelist number. This number is needed when submitting the file.

  • Disconnect from the P4 Server cleanly.

This script disconnects from the P4 Server because opening a file for edit is a one‑time server operation, not a live session. When a file is checked out it remains open until you submit or revert with no connection required.

Update the following credentials in the script:

  • port - Add the server and port to connect to in the format: server:port

  • user - A username for your P4 user.

  • client - The name of the workspace you want to connect to.

  • depotFile - Filepath to the file to edit. For example: //DemoDepot/php/Ascii_art/README.md

Call the script something simple like check_out_file.php

P4 API for PHP interacts directly with the P4 command-line interface.

Commands such as change -o and change -i are used instead of helper methods available in other APIs.

Copy
<?php

$port       = "localhost:1666";
$user       = "P4UserName";
$client     = "php_workspace";
$depotFile  = "//DemoDepot/php/README.md";

$p4 = new P4();

// --- Set connection details ---
$p4->port   = $port;
$p4->user   = $user;
$p4->client = $client;

try {
    // Connect
    $p4->connect();

    $info = $p4->run("info");
    echo "Connected to " . $info[0]["serverAddress"] . " (workspace: $client)\n";

    // 1) Create a pending changelist
    $change = $p4->run("change", "-o")[0];

    $change["Description"] = "Edit $depotFile via P4PHP (" . date("Y-m-d H:i:s") . ")";

    $p4->input = $change;
    $result = $p4->run("change", "-i");

    // Extract changelist number
    preg_match('/Change (\d+)/', $result[0], $matches);
    $changelist = $matches[1];

    echo "Created changelist $changelist\n";

    // 2) Open the file for edit in that changelist
    $p4->run("edit", "-c", $changelist, $depotFile);

    echo "Opened for edit: $depotFile (cl $changelist)\n";

    // 3) Show a short summary
    $opened = $p4->run("opened", $depotFile);

    foreach ($opened as $file) {
        echo $file["depotFile"] . " (" . $file["action"] . ") cl=" 
            . $file["change"] . " -> " . $file["clientFile"] . "\n";
    }

    echo "Changelist number: $changelist\n";
    echo "Make a note of this number. You will need it to submit the file.\n";

    // Disconnect
    $p4->disconnect();
    echo "Disconnected from the P4 Server.\n";

} catch (P4_Exception $e) {
    echo "P4 error: " . $e->getMessage() . "\n";

    foreach ($p4->errors as $error) {
        echo "Error: $error\n";
    }
}

?>

To run the script:

  1. Open a terminal and navigate to the folder containing the script.

  2. Run php check_out_file.php (If you have given the script a different name, use that name).

  3. Make a note of the changelist number. You will need this to commit the file.

Your chosen file is now available to edit.

If you have P4V, you will see a new changelist with your selected file.

Commit a file

The following script will:

  • Prompt you for the changelist number.

    • This changelist number was output in the previous section.

  • Connect to the P4 Server

  • Fetch the changelist to ensure it’s pending.

  • Show the current description, then prompt you to add any more detail for the changelist description.

  • Submit the changelist and print the result.

  • Disconnect from P4 Server.

Update the following credentials in the script:

  • port - Add the server and port to connect to in the format: server:port

  • user - A username for your P4 user.

  • client - The name of the workspace you want to connect to.

Call the script something simple like commit_file.php

Copy
<?php

$port   = "localhost:1666";
$user   = "P4UserName";
$client = "php_workspace";

// Prompt for changelist number
echo "Enter the changelist number to submit: ";
$changelist = trim(fgets(STDIN));

if (!$changelist) {
    echo "No changelist number provided. Exiting.\n";
    exit(1);
}

$p4 = new P4();

// --- Set connection details ---
$p4->port   = $port;
$p4->user   = $user;
$p4->client = $client;

try {
    // Connect
    $p4->connect();

    $info = $p4->run("info");
    echo "Connected to " . $info[0]["serverAddress"] . " (workspace: $client)\n";

    // Fetch the changelist
    $change = $p4->run("change", "-o", $changelist)[0];

    $status = strtolower($change["Status"] ?? "");
    if ($status !== "pending") {
        echo "Changelist $changelist is not pending (status: " . $change["Status"] . ").\n";
        $p4->disconnect();
        return;
    }

    // Show current description
    $currentDesc = rtrim($change["Description"] ?? "");
    echo "Current description:\n---\n$currentDesc\n---\n";

    echo "Add a submit comment (leave blank to keep as-is): ";
    $userNote = trim(fgets(STDIN));

    if ($userNote) {
        $newDesc = $currentDesc ? $currentDesc . "\n\n" . $userNote : $userNote;
        $change["Description"] = $newDesc;

        // ✅ IMPORTANT: use input for spec update
        $p4->input = $change;
        $p4->run("change", "-i");

        echo "Updated changelist description.\n";
    }

    // Submit the changelist
    $result = $p4->run("submit", "-c", $changelist);

    echo "Submit result:\n";
    foreach ($result as $entry) {
        if (is_array($entry)) {
            print_r($entry);
        } else {
            echo "  $entry\n";
        }
    }

    echo "Submitted changelist $changelist\n";

    // Disconnect
    $p4->disconnect();
    echo "Disconnected from the P4 Server.\n";

} catch (P4_Exception $e) {
    echo "P4 error: " . $e->getMessage() . "\n";

    foreach ($p4->errors as $error) {
        echo "Error: $error\n";
    }
}

?>

To run the script:

  1. Open a terminal and navigate to the folder containing the script.

  2. Run php commit_file.php .

  3. When asked, enter the changelist number with the file you want to commit.

  4. (Optional) Enter a changelist description.

Your chosen file has now been committed. If you check P4V, you will no longer be able to see the pending changelist.

Next steps

View the documentation for P4 API for PHP Classes to see what other commands are available when using P4 API for PHP.