API for Ninja Forms – WordPress plugin | WordPress.org

How to Securely Fetch Ninja Forms Submissions via the API

This article explains how to use a custom PHP function, fetchSubmissions, to securely retrieve form submission data from the API for Ninja Forms WordPress plugin that supports both JSON and PDF output formats.


1. Prerequisites: Setting up the API

Before using the PHP script, ensure you have set up the API on your WordPress site as described in the plugin documentation.

That’s great context! Knowing this is an API for Ninja Forms submissions clarifies the purpose of the API endpoints and the PHP script.

Here is the revised article, structured specifically to explain how to use this Ninja Forms Submissions API via the provided PHP function.


2. Understanding the PHP fetchSubmissions Function

The provided PHP function handles the secure communication with the Ninja Forms Submissions API endpoint using cURL.

🐍
index.php
function fetchSubmissions( $apiUrlSubmissions, $apiKey )
{
    // ... cURL setup and execution ...
    
    // **Key Security Step:** Sends the API Key as a Bearer Token in the HTTP Header
    curl_setopt_array($ch, [ 
        CURLOPT_URL => $apiUrlSubmissions, 
        CURLOPT_RETURNTRANSFER => true, 
        CURLOPT_HTTPHEADER => [ 
            'Authorization: Bearer ' . $apiKey, // Bearer Token Authentication
            'Accept: application/json', 
        ], 
    ]);
    // ... Error handling and return ...
}

The function requires two arguments: the full API URL and your generated API key. It is designed to perform Bearer Token Authentication, which the Ninja Forms API requires for secure data access.


3. Configuring the API Request in PHP

In the example usage section of your script, you must replace the placeholder values with your actual site details and API key.

A. Defining the URL and Key

Update the $apiUrlSubmissions and $apiKey variables:

🐘
index.php
// 1. Check for the optional 'format' parameter (e.g., ?format=pdf)
@$format = $_GET['format'];
if( !$format ) {
    $format = '';
}
// 2. Define the full API Endpoint URL.
// Replace 'http://localhost/workspace/' with your actual domain.
// Replace '/form/2/' with the desired Form ID (e.g., /form/10/).
$apiUrlSubmissions = 'https://yourwebsitehere.com/wp-json/nf-submissions/v1/form/2/?format='.$format;
// 3. Define your Secret API Key (Bearer Token).
// Replace this with the key you generated in Settings > NF API Keys.
$apiKey = 'YourActualNinjaFormsApiKeyHere'; 
// 4. Execute the authenticated API call
$data = fetchSubmissions($apiUrlSubmissions, $apiKey);

B. Specifying the Output Format (JSON vs. PDF)

The Ninja Forms API supports two formats, which you control using the format query parameter:

The PHP example automatically appends ?format=pdf if the user includes it in the calling script’s URL (e.g., http://yourscript.php?format=pdf).


4. Setting Headers and Displaying the Data

The final part of the script is crucial for correctly serving the content to the client (browser or application). It ensures the retrieved data is treated as JSON or PDF.

🐘
index.php
// Check the requested format to set the appropriate HTTP header
switch ($format) {
    case 'pdf':
        // Tell the client to expect a PDF file
        header('Content-Type: application/pdf');
    break;
    default:
        // Default to JSON output for standard API interaction
        header('Content-Type: application/json');
}
// Output the content received from the API (JSON string or PDF file data)
echo $data;

If the API successfully returns PDF data, the header will be Content-Type: application/pdf, prompting the browser to download or display the PDF. If it returns JSON, the header ensures the data is correctly interpreted as a JSON object.