PHP API
The United Notifications PHP API provides a straightforward way to send notifications through various channels using a simple HTTP POST request. Below is a complete guide on how to integrate the API into your PHP applications, along with an example code snippet.
Code ExampleHere's an example of sending a notification using PHP:
<?php
$request = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query([
"client_key" => "f31c320dcd",
"channel" => "[channel-token]",
"type" => "info",
"title" => "This is a info-notification",
"body" => "This is the body.",
"critical" => false
])
]
]);
$result = file_get_contents(
"https://api.united-notifications.com",
false,
$request
);
// Printing result.
//print_r($result);
?>
This script uses PHP's file_get_contents function with a custom HTTP context to send data to the API. Ensure you replace placeholders like [channel-token] and f31c320dcd with your actual values.
Below is a detailed explanation of the parameters used in the API request:
- client_key: Your unique API client key, which authenticates your request. This key must remain confidential.
- channel: The unique token identifying the channel to which the notification will be sent. Channels can represent groups or individual users.
- type: Specifies the type of notification. Examples include:
info: For informational notifications.warning: For alerts or warnings.error: For critical errors or failures.
- title: A short title summarizing the notification.
- body: The detailed message content of the notification. This field supports plain text.
- critical: A boolean value (
trueorfalse) indicating whether the notification is critical and requires immediate attention.
All requests should be sent to the following endpoint:
https://api.united-notifications.com
This endpoint accepts POST requests with data formatted as application/x-www-form-urlencoded. Ensure your server can connect to this endpoint to successfully deliver notifications.
The API will return a response indicating the success or failure of your request. The response is typically a JSON object with the following fields:
- status: Indicates whether the request was successful (e.g.,
successorerror). - message: Provides additional information about the request, such as error descriptions.
- data: (Optional) Contains additional details or metadata about the notification.
To debug or log the response, uncomment the print_r($result); line in the example code.
For secure communication, the API uses HTTPS to encrypt data transmission. Additionally:
- Keep your
client_keyconfidential and avoid exposing it in front-end applications. - Use secure storage mechanisms for sensitive credentials.
- Validate API responses to ensure the notification was delivered successfully.
If you encounter issues while using the API:
- Ensure the endpoint URL (
https://api.united-notifications.com) is reachable from your server. - Verify that your
client_keyandchannelvalues are correct. - Check your server's PHP configuration to ensure the
file_get_contentsfunction is enabled. - Inspect the API response for error messages to identify specific problems.
By following this guide, you can seamlessly integrate United Notifications into your PHP applications to deliver notifications to your users.