<?php
$page_title = 'Step 1 — Ping the API';
require 'header.php';
?>

<p>
    The <span class="endpoint">/users/ping</span> endpoint does nothing except
    confirm your API key is valid. It's the cheapest possible call — use it
    to sanity-check your credentials before debugging anything more complex.
</p>

<h2>Try it</h2>

<form method="post">
    <button type="submit" name="run">Ping Mandrill</button>
</form>

<?php
if (isset($_POST['run']) && is_configured()) {

    // --- The actual API call ---------------------------------
    // No parameters needed — just the API key (added by the wrapper).
    $result = mandrill_call('/users/ping');
    // ---------------------------------------------------------

    if (!mandrill_is_error($result)) {
        echo '<div class="callout ok"><strong>Success.</strong> Your API key is valid.</div>';
    } else {
        echo '<div class="callout warn"><strong>Call failed.</strong> See the response below for details.</div>';
    }

    render_debug($result);
}
?>

<h2>What the code looks like</h2>

<p>The whole thing, minus the HTML wrapper, is this:</p>

<pre><code>require 'lib.php';

$result = mandrill_call('/users/ping');

if (!mandrill_is_error($result)) {
    echo "API key works";
}</code></pre>

<p>And <code>mandrill_call()</code> (from <code>lib.php</code>) is just a thin cURL wrapper:</p>

<pre><code>function mandrill_call($endpoint, $params = []) {
    $params['key'] = MANDRILL_API_KEY;
    $ch = curl_init(MANDRILL_API_BASE . $endpoint);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER =&gt; true,
        CURLOPT_POST           =&gt; true,
        CURLOPT_HTTPHEADER     =&gt; ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS     =&gt; json_encode($params),
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}</code></pre>

<h2>What to expect</h2>

<p>A successful ping returns the literal string <code>"PONG!"</code> wrapped in a JSON object:</p>

<pre><code>{
    "PING": "PONG!"
}</code></pre>

<p>If the key is wrong, you'll get an error object:</p>

<pre><code>{
    "status":  "error",
    "code":    -1,
    "name":    "Invalid_Key",
    "message": "Invalid API key"
}</code></pre>

<p>
    That <code>{status: "error", ...}</code> shape is <em>every</em> Mandrill
    error — worth recognising, because that's what <code>mandrill_is_error()</code>
    in <code>lib.php</code> checks for.
</p>

<p><a href="02-allowlist-list.php">→ Next: list the allowlist</a></p>

<?php require 'footer.php'; ?>
