What are some best practices for sending SMS through PHP using external services like SMS77.de?

When sending SMS through PHP using external services like SMS77.de, it is important to follow best practices to ensure successful delivery and proper handling of responses. One key practice is to securely store API credentials and use HTTPS for API requests to protect sensitive information. Additionally, it is recommended to handle errors and responses from the API gracefully to provide feedback to the user.

<?php

// API credentials
$api_username = 'your_api_username';
$api_password = 'your_api_password';

// Recipient phone number
$phone_number = '1234567890';

// Message content
$message = 'Hello, this is a test message!';

// API endpoint
$api_url = 'https://gateway.sms77.io/api/sms';

// Prepare data for API request
$data = array(
    'u' => $api_username,
    'p' => $api_password,
    'to' => $phone_number,
    'text' => $message
);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Check for errors
if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'SMS sent successfully!';
}

// Close cURL session
curl_close($ch);

?>