Installing P4 API for PHP

Depending on if you are using a Windows, macOS or Linux machine determines how to install P4 API for PHP

Installing P4 API for PHP on Windows

To install P4 API for PHP on Windows, follow these steps:

Download the relevant files and apps

To build the P4 API for PHP, download the following:

PHP

Download the latest stable version of PHP 8.4 from the PHP website:

  • Select a Thread Safe version.

  • Select x64 (64-bit) and the latest stable version (currently 8.4).

  • Download the ZIP file (not the installer).

P4 API for PHP

You will also need the P4PHP extension, this is a .dll file.

  • Download this extension can from the Perforce archive.

  • This file must match:

    • The version of PHP you have (for example 8.4).

    • Either Thread Safe or not.

  • For example, download perforce_php84ts.dll for PHP 8.4 TS (Thread Safe).

Configure P4 API for PHP

When you have all the required files and apps, follow these steps:

  1. Go to the directory that contains PHP.

  2. Copy the perforce .dll file into the PHP ext folder. For example, the file path for this .dll file would be:

    C:\php\ext\perforce_php84ts.dll

  3. In the PHP directory, find and open the php.ini file, and add the following line to the bottom of the file:

    extension=perforce_php84ts.dll

  4. Save php.ini

  5. Verify the .dll file has loaded. In the command prompt, navigate to the PHP directory and run the following:

    php -m

  6. You should see perforce listed in the modules in the output.

(Optional) Test the P4 API for PHP installation

To test the test the P4 API for PHP installation, follow these steps:

  1. Create a folder for testing, for example: C:\p4test)

  2. In this folder, create a test file, for example, test_p4.php.

  3. Enter the following into test_p4.php:

    Copy
    <?php
    error_reporting(E_ALL & ~E_DEPRECATED); // hide deprecated warnings

    try {
        // Create a new P4 connection
        $p4 = new P4();

        // Set server, user, and workspace
        $p4->Port = 'perforce:1666';       // Replace with your server
        $p4->User = 'your-username';       // Replace with your username
        $p4->Client = 'your-workspace';    // Replace with your workspace

        // Connect
        $p4->Connect();
        echo "Connected to Perforce successfully!\n";

        // Disconnect
        $p4->Disconnect();
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
    Make sure you update the Port, User and Client fields.
  4. Open the command prompt and navigate to your test folder: cd C:\p4test

  5. Run the script: php test_p4.php

  6. If the test is successful, you will see the following output: Connected to Perforce successfully!

Installing P4 API for PHP on macOS or Linux

To install P4 API for PHP on macOS or Linux, follow these steps:

Download the relevant files and apps

To build P4 API for PHP, you’ll need the following PHP files:

  • The PHP header files and libraries.

  • The phpize command-line utility.

The best way to get all these is to download everything from the source and then compile PHP yourself. This will ensure phpize Is built automatically, and headers will be in the include/ directory of your build tree.

You will also need the following P4 apps:

  • P4 API for PHP

  • P4 API for C/C++

Find the version of the P4 apps in the Perforce archive and download them.

The versions of both apps must be the same. To ensure you are getting the correct versions, download them both from same release folder in the archive. The latest release is 2025.1 which you can find here.

When downloading P4 API for C/C++, you must get a version of the app that matches:

  • Your system’s OpenSSL version

  • Your system’s C library version

For example, p4api-glibc2.12-openssl1.1.1.tgz for a system using OpenSSL 1.1.1 and with a C library version 2.12.

Build P4 API for PHP

When you have all the required files and apps, follow these steps:

  1. Create an empty directory in which to extract the P4 API for PHP and P4 API for C/C++ archives.

  2. Extract the archives.

  3. Inside the p4php-<version> directory, run the following in your terminal:

    Copy
    phpize
    ./configure --with-perforce=<path to P4 API for C/C++>
    make
  4. To test the build, run the following in your terminal: make test

    The test harness requires the P4 Server executable in the current directory.
  5. Install P4 API for PHP by running the following in your terminal: make install

    Installation usually requires root or admin privileges.
  6. Edit php.ini to enable P4 API for PHP in PHP. Add the following line to the php.ini file:

    • extension=perforce.so;

  7. Check this module loads correctly by running the following in your terminal: php -m

  8. Verify the installation has installed correct by viewing the version details. Run the following in your terminal: php --ri perforce.

    If you see the version details, P4 API for PHP has been installed correctly.