fast2sms api sms service php code

 Fast2SMS is another SMS gateway service that allows sending SMS messages via API. Here's how you can send an SMS using Fast2SMS's API with PHP:


### Step 1: Set Up a Fast2SMS Account

- Sign up for a Fast2SMS account [here](https://www.fast2sms.com/).

- Obtain your API key from the Fast2SMS dashboard.


### Step 2: Send SMS Using Fast2SMS API


Here's a PHP script using `curl` to send an SMS with Fast2SMS:


```php

<?php

// Fast2SMS API credentials

$api_key = 'your_api_key'; // Your Fast2SMS API key


// User details

$user_name = "John Doe";

$user_email = "john.doe@example.com";

$user_phone = "DestinationPhoneNumber"; // The user's phone number where the SMS will be sent


// SMS message content

$message = "User Details:\nName: $user_name\nEmail: $user_email";


// Prepare data for POST request

$data = [

    'authorization' => $api_key,

    'sender_id' => 'FSTSMS', // Sender ID registered on Fast2SMS

    'message' => $message,

    'language' => 'english',

    'route' => 'p', // 'p' for promotional, 't' for transactional

    'numbers' => $user_phone

];


// Initialize cURL

$ch = curl_init('https://www.fast2sms.com/dev/bulkV2');


// Set cURL options

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);


// Execute the request and fetch the response

$response = curl_exec($ch);


// Close cURL session

curl_close($ch);


// Display the response

echo $response;

?>

```


### Step 3: Replace Placeholder Values

- Replace `'your_api_key'` with your Fast2SMS API key.

- Replace `'DestinationPhoneNumber'` with the recipient’s phone number.


### Step 4: Run the Script

Upload the script to your server and run it to send the SMS.


### Example Response


If successful, the response from the API will contain information about the message status. The exact format of the response will depend on Fast2SMS's API.


This script uses the `curl` library to send a POST request to the Fast2SMS API endpoint. The response from the API will indicate whether the SMS was sent successfully or if there was an error. Make sure to handle the API response appropriately in a production environment.

Previous
Next Post »