Skip to content
Gaurav TiwariDocs

PHP

Version 5.2.5 Updated November 22, 2026

One PHP file can forward submissions from a non-WordPress site to Core Forms. It works on ordinary PHP hosting and keeps the API key on the server.

You need PHP 8 or newer with the cURL extension.

Create the Proxy

Finish the WordPress setup, then save this as public/api/submit.php:

<?php
// public/api/submit.php
$api_key  = getenv('CORE_FORMS_API_KEY') ?: '';
$upstream = getenv('CORE_FORMS_UPSTREAM') ?: '';
$form_id  = getenv('CORE_FORMS_FORM_ID') ?: '';
$origins  = array_filter(array_map('trim', explode(',', getenv('CORE_FORMS_ALLOWED_ORIGINS') ?: '')));

const MAX_BYTES = 16 * 1024;

function reply(int $status, string $json): void
{
    http_response_code($status);
    header('Content-Type: application/json');
    header('Cache-Control: no-store');
    echo $json;
    exit;
}

function fail(int $status, string $message): void
{
    reply($status, json_encode(['message' => $message]));
}

if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
    fail(405, 'Method not allowed.');
}
if (!$api_key || !$upstream || !$form_id || !$origins) {
    fail(503, 'The form proxy is not configured.');
}
if (!in_array($_SERVER['HTTP_ORIGIN'] ?? '', $origins, true)) {
    fail(403, 'This origin is not allowed.');
}

$user_agent = str_replace(["\r", "\n"], '', $_SERVER['HTTP_USER_AGENT'] ?? '');
if (!preg_match('/mozilla|safari|chrome|firefox/i', $user_agent)) {
    fail(403, 'Submission blocked.');
}
if ((int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > MAX_BYTES) {
    fail(413, 'Submission too large.');
}

if (str_contains($_SERVER['CONTENT_TYPE'] ?? '', 'application/json')) {
    $raw = file_get_contents('php://input', false, null, 0, MAX_BYTES + 1);
    if (strlen($raw) > MAX_BYTES) {
        fail(413, 'Submission too large.');
    }
    $payload = json_decode($raw);
} else {
    $payload = (object) $_POST; // a normal HTML form post
}
if (!is_object($payload)) {
    fail(400, 'Send the fields as a JSON object.');
}

foreach (get_object_vars($payload) as $value) {
    $is_list = is_array($value) && array_values($value) === $value;
    foreach ($is_list ? $value : [$value] as $item) {
        if ($item !== null && !is_scalar($item)) {
            fail(400, 'Send text, numbers or lists only.');
        }
    }
}
// Always submit to the configured form, whatever the browser sent.
$payload->id = $form_id;

$body = json_encode($payload);
if ($body === false || strlen($body) > MAX_BYTES) {
    fail(413, 'Submission too large.');
}

$request = curl_init(rtrim($upstream, '/') . '/forms/' . rawurlencode($form_id) . '/submit');
curl_setopt_array($request, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => $body,
    CURLOPT_TIMEOUT        => 15,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        'Accept: application/json',
        'X-Core-Forms-Key: ' . $api_key,
        'User-Agent: ' . $user_agent,
    ],
]);

$result = curl_exec($request);
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
$type   = (string) curl_getinfo($request, CURLINFO_CONTENT_TYPE);

if ($result === false || !$status) {
    fail(502, 'The form server could not be reached.');
}
if (!str_contains($type, 'application/json')) {
    fail(502, 'The form server returned an unexpected response.');
}

reply($status, $result);

Show the Form

This page fetches the form’s HTML from WordPress on each request:

<?php
// public/contact.php
$base = getenv('CORE_FORMS_UPSTREAM') ?: '';
$id   = getenv('CORE_FORMS_FORM_ID') ?: '';

$request = curl_init(rtrim($base, '/') . '/forms/' . rawurlencode($id));
curl_setopt_array($request, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10]);
$json = curl_exec($request);
$form = curl_getinfo($request, CURLINFO_HTTP_CODE) === 200 ? json_decode((string) $json, true) : null;
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contact us</title>
</head>
<body>
  <h1>Contact us</h1>
<?php if (!empty($form['html'])) : ?>
  <div data-core-form data-submit-url="/api/submit.php"><?= $form['html'] ?></div>
  <script src="/core-forms-client.js" defer></script>
<?php else : ?>
  <p>The form is unavailable. Please email us instead.</p>
<?php endif; ?>
</body>
</html>

Save the submit script as public/core-forms-client.js. Only output HTML from a WordPress site you control.

Set the Environment

Set these in your hosting panel, container or PHP-FPM pool:

CORE_FORMS_UPSTREAM=https://cms.example.com/wp-json/core-forms/v1
CORE_FORMS_FORM_ID=123
CORE_FORMS_API_KEY=YOUR_SITE_API_KEY
CORE_FORMS_ALLOWED_ORIGINS=https://www.example.com,https://example.com

getenv() doesn’t read .env files. If you use a file, load it with your framework and keep it outside the web root.

In a PHP-FPM pool the lines look like this:

env[CORE_FORMS_UPSTREAM] = "https://cms.example.com/wp-json/core-forms/v1"
env[CORE_FORMS_FORM_ID] = "123"
env[CORE_FORMS_API_KEY] = "YOUR_SITE_API_KEY"
env[CORE_FORMS_ALLOWED_ORIGINS] = "https://www.example.com,https://example.com"

Forms Without JavaScript

The proxy accepts a normal form post, but it answers with JSON. To support visitors without JavaScript, replace the final reply() with a redirect to your thanks or error page.

Before Launch

Share

Was this helpful?

Still stuck? Open a support ticket