Sample code for 30+ languages & platforms
PHP

Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

See more OpenSSL Examples

How to create a PKCS12 (.p12 or .pfx) from a certificate file and private key file: Demonstrates how to duplicate this OpenSSL command:
Duplicate openssl pkcs12 –export –in certfile.cer –inkey certfile.key –out certfile.pfx

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Cert;
use Chilkat2\Pfx;
use Chilkat2\PrivateKey;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$pkey = new PrivateKey();

// Load the private key from the file.
try {
    $pkey->LoadAnyFormatFile('certFile.key', '');

    $cert = new Cert();
    // The LoadFromFile method auto-recognizes the format...
    $cert->LoadFromFile('certfile.cer');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// We'll need a cert chain object to create the PKCS12, so get it
// from the cert.
ERROR: Method not in Chilkat2 PHP: Cert.GetCertChain
$certChain = $cert->ERROR();
if (!$cert->LastMethodSuccess) {
    echo $cert->LastErrorText, "\n";
    exit;
}

// Create the PFX object, add the cert and private key, and write to a .pfx file.
$pfx = new Pfx();

// The cert(s) are automatically added in the call to AddPrivateKey
try {
    $pfx->AddPrivateKey($pkey, $certChain);

    // Write the .pfx to a file.
    $password = 'myPassword';
    $pfx->ToFile($password, 'certfile.pfx');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Success.\n";