# PHP

> Embed the Muzekit editor in a PHP app without a framework: one file opens the session with cURL, one page mounts the editor.

<Callout title="Let your coding agent do it">Connect Claude Code, Cursor or Codex to the [MCP server](/docs/mcp#set-up-the-editor-in-your-app) and ask: "Set up the Muzekit editor in this PHP app." It writes these same files.</Callout>

## How it works in PHP

`muzekit-session.php` reads the signed-in user from `$_SESSION`, whatever your login sets there, calls Muzekit with cURL and returns only the `client_secret`. Serve it at `/api/muzekit-session` with a rewrite in Apache or Nginx, or point `fetchClientSecret` at the file's own path. `getenv` reads the key from the server: `SetEnv` in Apache, `fastcgi_param` in Nginx, or `env[...]` in a PHP-FPM pool.

## 1. Add your secret key

[Create an editor](/signup) and copy its Test key (`mk_test_…`) from **API keys** in the dashboard. Put it in `.env` on your server:

```bash tab=".env"
MUZEKIT_SECRET_KEY=mk_test_…
```

<Screenshot src="/docs/1-embed-code.webp" original="/docs/raw/1-embed-code.png" alt="The Embed code page in the Muzekit dashboard: the Test key for the server, then the session route and mount code" />

## 2. Open a session on your server

Opens a session for the signed-in user, in `muzekit-session.php (serve it at /api/muzekit-session)`:

```php tab="muzekit-session.php (serve it at /api/muzekit-session)"
<?php
session_start();
$user = $_SESSION['user'] ?? null; // Your signed-in user: replace with your own auth
if (!$user) { http_response_code(401); exit; }

$ch = curl_init('https://api.muzekit.com/v1/sessions');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('MUZEKIT_SECRET_KEY'), 'Content-Type: application/json'],
    CURLOPT_POSTFIELDS => json_encode(['user' => ['id' => (string) $user['id'], 'name' => $user['name']], 'mode' => 'design']),
]);
$session = json_decode(curl_exec($ch), true);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
header('Content-Type: application/json');
http_response_code($status === 201 || $status === 200 ? 200 : $status);
echo json_encode(isset($session['client_secret']) ? ['client_secret' => $session['client_secret']] : $session);
```

## 3. Mount the editor

The editor, in `editor.php`:

```html tab="editor.php"
<div id="muzekit-editor" style="height: 720px"></div>
<script src="https://muzekit.com/sdk/v1.js"></script>
<script>
  const editor = Muzekit.mount("#muzekit-editor", {
    fetchClientSecret: () =>
      fetch("/api/muzekit-session", { method: "POST" })
        .then((r) => r.json())
        .then((s) => s.client_secret),
    on: {
      ready: () => console.log("Muzekit editor ready"),
      save: (e) => console.log("Saved", e.template, e.revision),
      error: (e) => console.error("Muzekit", e.code, e.message),
    },
  });
</script>
```

## 4. Run it

```bash
php -S localhost:8000
```

Sign in, then open `http://localhost:8000/editor.php`. The editor loads in its container and the first session shows on your dashboard's Home. With a Test key, sessions on `localhost` and `127.0.0.1` are free.

## Common problems

### The session file answers nothing

Check that the cURL extension is on (`php -m` lists it) and that `getenv` returns the key in your server's setup. With `php -S`, point `fetchClientSecret` at `/muzekit-session.php`.

### Headers already sent

Nothing may be printed before `header()`: no space before the opening PHP tag and no byte order mark.

### WordPress

Register the route with `register_rest_route`, check `is_user_logged_in()`, and send the `X-WP-Nonce` header from the page.

## Notes

- Secret key: MUZEKIT_SECRET_KEY goes in .env (or your secret store) on the server only. Never in browser code, never committed.
- Every user.id is your own id for that person: their designs and uploads follow it across sessions.
- mode: "design" opens the full editor; "fill" opens a form over a template (send template: "`<key or tpl_ id>`" too).
- Give the container a real height: the editor fills it.

## Deploy and go live

Shared hosting often has no place for environment variables: keep the key in a PHP config file outside the web root and `require` it.

Then make a Live key (`mk_live_…`), add your domain to Live under [Allowed domains](/docs/allowed-domains) and use **Copy to Live** for your templates, look and features.

## Questions

### Which PHP version?

PHP 7.4 or newer. The example uses only the cURL and JSON extensions, which most hosts enable.

<Cards>
  <Card title="Editor modes" description="Design, fill-in and view, and what each shows." href="/docs/editor-modes" />
  <Card title="Browser SDK" description="Events, saving from your own button, and destroy." href="/docs/embed-sdk" />
</Cards>
