MockAPI-PHP

Tips

Custom Responses

You can dynamically change the response by specifying the mock_response query parameter.

Request Response File Retrieved
GET /users responses/users/get/default.json
GET /users?mock_response=success responses/users/get/success.json
POST /users?mock_response=failed responses/users/post/failed.json
POST /users?mock_response=400 responses/users/post/400.json

Response Delay

You can set a delay in the response by specifying mockDelay in the JSON file.

Example: responses/users/get/default.json

{
    "id": 1,
    "name": "John Doe",
    "mockDelay": 1000
}

→ The response will be returned after 1 second.

Handling Query Parameters

{
  "query_params": {
    "filter": "name",
    "sort": "asc"
  },
  "body": {}
}

Custom Content-Type Setting

By default, responses are returned as application/json or text/plain, but you can specify any Content-Type.

Returning a CSV File

Register the response as responses/others/get/default.txt (content example below):

id,name,email
1,John Doe,john@example.com
2,Jane Doe,jane@example.com

Making a request with GET others?mock_content_type=text/csv will return others.csv (downloadable if accessed via a browser).

Returning an XML File

Register the response as responses/others/get/userlist.txt (content example below):

<users>
    <user>
        <id>1</id>
        <name>John Doe</name>
    </user>
    <user>
        <id>2</id>
        <name>Jane Doe</name>
    </user>
</users>

Making a request with GET others?mock_response=userlist&mock_content_type=application/xml will return XML-formatted data.

Custom Hooks

This feature allows you to override the predefined response for a specific method + endpoint by using custom hooks. By placing a PHP file in hooks/{METHOD}_{SNAKE_CASE_ENDPOINT}.php, you can enable a custom hook.

Example: Custom hook for the GET users endpoint hooks/get_users.php

<?php

// Example: Hook for GET /users
if (isset($request_data['query_params'])) {
    $filter = $request_data['query_params']['filter'] ?? null;
    // If the `filter` query parameter is specified
    if ($filter) {
        $sort = strtolower($request_data['query_params']['sort']) === 'desc' ? 'desc' : 'asc';
        header('Content-Type: application/json');
        echo json_encode([
            'data' => [
                [
                    'id' => 1,
                    'name' => 'Alice',
                    'age' => 24,
                ],
                [
                    'id' => 2,
                    'name' => 'Bob',
                    'age' => 27,
                ],
            ],
        ]);
        // Terminate script after returning response
        exit;
    }
}

For a request like GET users?filter=name, the following response will be returned:

{
  "data": [
    {
      "id": 1,
      "name": "Alice",
      "age": 24
    },
    {
      "id": 2,
      "name": "Bob",
      "age": 27
    }
  ]
}

Dynamic Parameters

For requests with dynamic parameters such as GET users/{group}/{limit}, parameters are extracted as request parameters if the response root exists at responses/users/get. You can use custom hooks to control responses using these extracted parameters.

Example: Custom hook for the GET users endpoint hooks/get_users.php

<?php

// Example: Hook for dynamic parameters in `GET /users/{group}/{limit}`
$pattern = '/^dynamicParam\d+$/';
$matchingKeys = preg_grep($pattern, array_keys($request_data));
if (!empty($matchingKeys)) {
    // Extract dynamic parameters
    $filteredArray = array_filter($request_data, function ($key) use ($pattern) {
        return preg_match($pattern, $key);
    }, ARRAY_FILTER_USE_KEY);
    extract($filteredArray);
    $group = isset($dynamicParam1) ? $dynamicParam1 : 'default';
    $limit = isset($dynamicParam2) ? (int) $dynamicParam2 : 0;

    header('Content-Type: application/json');
    $response = [
        'group' => $group,
        'limit' => $limit,
        'users' => [],
    ];
    for ($i = 1; $i <= $limit; $i++) {
        $response['users'][] = [
            'id' => $i,
            'name' => "User {$i} (Group: {$group})",
        ];
    }
    echo json_encode($response, JSON_PRETTY_PRINT);
    exit;
}

For a request like GET users/groupA/3, the following response will be returned:

{
    "group": "groupA",
    "limit": 3,
    "users": [
        {
            "id": 1,
            "name": "User 1 (Group: groupA)"
        },
        {
            "id": 2,
            "name": "User 2 (Group: groupA)"
        },
        {
            "id": 3,
            "name": "User 3 (Group: groupA)"
        }
    ]
}