Skip to content

PHP API Client for Mythradon

This guide provides a PHP module that helps developers interact with the Mythradon REST API. It supports standard HTTP methods and includes optional HMAC-based authentication.

Getting Started

To use the API client, you'll need the following:

  • Your API_KEY (required)

  • Your SECRET_KEY (optional, for HMAC authentication)

  • Your Mythradon instance URL


Download Mythradon PHP API Client

Download the Mythradon PHP API Client Library and unzip the contents into an appropriate folder.

The zip file consists of the following:

File Description
MythradonApiTest.php Use this test script to verify that your platform instance can successfully connect to and use the REST API.
Client.php Handles authenticated HTTP requests to the Mythradon API using cURL.
Header.php Represents a single HTTP header (name/value pair).
Loader.php Manually loads all core components of the MythradonApi namespace (for environments without PSR-4).
Response.php Represents the HTTP response returned by the API, including headers, status code, and body.
ResponseError.php Exception class thrown when an API request fails (non-2xx response).

File Structure

MythradonApiTest.php
MythradonApi/
├── Client.php
├── Header.php
├── Loader.php
├── Response.php
├── ResponseError.php


MythradonApiTest.php – Test Script for Mythradon PHP API Client

This script provides a basic test of the MythradonApi\Client class to verify connectivity and functionality with a Mythradon API instance. It demonstrates how to:

  1. Authenticates with the API using either an API key or HMAC credentials.
  2. Create a new contact via a POST request.
  3. Update that contact with a new last name using a PUT request.
  4. Handle and log errors if the API responds with a failure.

Configuration

At the top of the script, you'll need to define your connection details:

1
2
3
$url = 'https://<your-platform-domain>'; // Base URL of your environment (No Trailing slash required)
$apiKey = '<YourApiKey>';                // Your API key
$secretKey = null;                       // Optional HMAC secret key

The script supports two types of authentication:

  • API Key only: - Calls setApiKey() if only $apiKey is provided.

  • HMAC Authentication: - Calls both setApiKey() and setSecretKey() if both values are provided.

If neither is provided, it throws a RuntimeException.

To use HMAC, both $apiKey and $secretKey must be provided.


Examples

Create Contact

1
2
3
4
5
6
7
8
9
$response = $client->request(
    Client::METHOD_POST,
    'Contact',
    [
        'firstName' => 'API',
        'lastName' => uniqid(),
    ],
    [new Header('X-Skip-Duplicate-Check', 'true')]
);

This creates a new Contact entity, optionally skipping duplicate checking with a custom header.


Update Contact

1
2
3
4
5
6
7
$response = $client->request(
    Client::METHOD_PUT,
    'Contact/' . $contactId,
    [
        'lastName' => $contactLastName . ' Updated'
    ]
);

Updates the newly created contact with an updated last name.


Error Handling

1
2
3
4
5
catch(ResponseError $e){
    $response = $e->getResponse();
    echo "Error Code: " . $response->getCode();
    echo "Error body: " . $response->getBodyPart();
}

Captures and prints errors returned by the API for easy debugging.


File Structure Context

This test script expects that the Mythradon SDK classes are accessible via:

require_once('MythradonApi/Loader.php');

Ensure your autoloader is set up correctly for testing.


Usage

Run the script in your terminal:

php MythradonApiTest.php


Features

  • Simple wrapper for RESTful requests

  • Supports GET, POST, PUT, PATCH, DELETE, and OPTIONS

  • Compatible with API Key and HMAC authentication

  • Handles errors and parses JSON responses

  • Optional support for custom ports and timeouts


Notes

  • The id field of each record is required for operations like update or delete.

  • Use the Metadata API or Entity Manager to explore available entity names and attributes.

  • This module is intended for server-side use with PHP.


See also

API Client Examples: