Quick start for P4 API for PHP on Windows
On this page:
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:
-
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).
-
-
Extract PHP to a known location, for example:
C:\php. -
Go to the Perforce archive and open the folder for the latest release (for example
r25.2for P4 API for PHP 2025.2). -
Download the P4 API for PHP file that matches your PHP version. For example, download
perforce_php84ts.dllfor PHP 8.4 TS (Thread Safe). -
Move the
.dllfile to the PHP extensions directory. For example:C:\php\ext. -
In
C:\php, open thephp.inifile. -
Add the following line in the Dynamic Extensions section:
extension=perforce_php84ts.dll -
Enable OpenSSL.
-
In
php.ini, find the following line and remove the leading;:;extension=openssl -
So it becomes:
extension=openssl
-
-
Save the
php.inifile. -
(Optional) Restart the Command Prompt to make sure the changes take effect.
-
Verify the extension is loaded. In Command Prompt, run:
php -m -
You should see
perforceandopenssllisted 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
<?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:
-
Open a terminal and navigate to the folder containing the script.
-
Run
php connect_to_p4.php(If you have given the script a different name, use that name). -
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.
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
Commands such as change -o and change -i are used instead of helper methods available in other APIs.
<?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:
-
Open a terminal and navigate to the folder containing the script.
-
Run
php check_out_file.php(If you have given the script a different name, use that name). -
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
<?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:
-
Open a terminal and navigate to the folder containing the script.
-
Run
php commit_file.php. -
When asked, enter the changelist number with the file you want to commit.
-
(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.