Skip to content

Instantly share code, notes, and snippets.

@lesiki
Last active January 1, 2024 02:58
Show Gist options
  • Select an option

  • Save lesiki/9384612 to your computer and use it in GitHub Desktop.

Select an option

Save lesiki/9384612 to your computer and use it in GitHub Desktop.
PHP sample code for sending SMS through the FrontlineCloud API
<?php //FILE: sms_api.php
function sendSMS($number, $message) {
$url = "example"; // Set your frontlinesms or frontlinecloud webconnection url here
$secret = "secret"; // Set the secret here
$request = array(
'secret' => $secret,
'message' => $message,
'recipients' => array(array(
'type' => 'mobile',
'value' => $number
))
);
$req = json_encode($request);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $req );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
return split(',',$result);
}
?>
<?php //FILE: index.php
include 'sms_api.php';
$number = '+123456789';
$text = 'Hi There, how are you?';
$sms_api_result = sendSMS($number, $text);
// Check if SMS was sent. The $sms_api_result boolean indicates whether the API call was successful.
// You can replace the code below with custom handling logic
if ($sms_api_result[0] == 'OK') {
// Ok, SMS received by the API
echo 'The SMS was sent.';
}
else {
// Failure, SMS was not sent
// In this example we display the response to identify the error
print_r($sms_api_result);
}
?>
@ratatatKE

Copy link
Copy Markdown

Hey, Thanks a lot for this demo, however, I get this error that is saying some functions you are using are deprecated.

                      Function split() is deprecated in C:\xampp\htdocs\\index.php on line 26
                      Array ( [0] => 

Could we maybe see how to fix these for future implementations,

So I have read from the php documentation that they would prefer us to use the explode() method, i.e

                      return explode(',',$result); 

Thanks man

@martinayllon

Copy link
Copy Markdown

HI:
I am not be able to run this example. I installed Frontlinesms in windows 8.1 and Ubuntu 14.10 server in a VM. php.info() showed me that php is running. But when I select index.php nothing happen.
Please advice.

@lesiki

lesiki commented Apr 29, 2016

Copy link
Copy Markdown
Author

Note for anyone who comes across this, this example is using the deprecated WebConnection API. Frontline now does this through Activities and the Webhook api. Updated API docs are here:

Will push an update to this shortly.

@lesiki

lesiki commented May 9, 2016

Copy link
Copy Markdown
Author

Updated, this should now work with our current FrontlineCloud code.

@ivanoronee

ivanoronee commented Mar 27, 2017 •

Copy link
Copy Markdown

The segment of code that reads

 'recipients' => array(array( 
                                'type' => 'address', 
                                'value' => $number 
                        ))

should be updated to read

 'recipients' => array(array( 
                                'type' => 'mobile', 
                                'value' => $number 
                        ))

The change to be made is on line 11, 'type' => 'address' to 'type' => 'mobile'

@lesiki

lesiki commented Mar 27, 2017

Copy link
Copy Markdown
Author

You're right @ivanoronee, have updated to reflect this.

@olifranknyambuya

Copy link
Copy Markdown

when i click submit, its returning

Array ( [0] => )

can you please help

@njokinjuguna

Copy link
Copy Markdown

when i click submit, its returning

Array ( [0] => )

please assist

@Njinga

Njinga commented Apr 24, 2019

Copy link
Copy Markdown

anyone who knows how to sort this problem
Array ( [0] => )
help please

@kamondej

Copy link
Copy Markdown

Anyone who managed to sort out the "Array ( [0] => )" error please?

@lesiki

lesiki commented May 4, 2021

Copy link
Copy Markdown
Author

@vaneyck looks like some help is needed on this, maybe someone on the team could have a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment