Skip to content

Instantly share code, notes, and snippets.

@daluu
Last active October 5, 2016 22:58
Show Gist options
  • Save daluu/73815784246a71c01797 to your computer and use it in GitHub Desktop.
Save daluu/73815784246a71c01797 to your computer and use it in GitHub Desktop.
Simple PHP curl demo for Textbelt
<?php
//This demo doesn't cover case of network connectivity issues (failure to send HTTP POST to Textbelt)
//One could just wrap the request around try/catch exception handling for that
//Move the demo function into a separate file to make this into a wrapper library you can require/include and then call from a script
// A simple wrapper function to send SMS via Textbelt using curl in PHP
function sendSmsToTextbelt($number, $message, $locale="USA"){
//determine the Textbelt URL to POST to...
$url = "http://www.textbelt.com/";
switch($locale) {
case "International":
$url .= "intl";
break;
case "Canada":
$url .= "canada";
break;
case "USA":
default:
$url .= "text";
}
//set up the HTTP POST request to Textbelt
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//the 2 fields for POSTing are number & message, add/adjust if you want to customize the other default fields (from, etc.)
$fields = array(
'number' => urlencode($number),
'message' => urlencode($message)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//send request and get result
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output,true);
return $result;
}
//simple demo (might have some issue parsing result, but the SMS does get sent...)
$status = sendSmsToTextbelt("your number here", "Simple PHP demo for Textbelt");
if($status->success){
print "Message sent, but see www.textbelt.com for limitations with service.";
//why this particular message? Because textbelt sent message to the carrier's SMS gateway, but we can't guarantee the gateway passes it on to recipient
}else{
print $status->message;
}
?>
@funkyfela
Copy link

thanks. will give it a shot and let you know.
thanks again!

@funkyfela
Copy link

Hello.
I have tried it but i did not get any response (success or failure) and yet the sms was not sent. what could be wrong? is is my carrier gateway not supported?

this is what i tried, i removed the demo part just as you suggested.

success){ print "Message sent, but see www.textbelt.com for limitations with service."; //why this particular message? Because textbelt sent message to the carrier's SMS gateway, but we can't guarantee the gateway passes it on to recipient }else{ print $status->message; } } ?>

didnt get any response. i tried using country code, didnt work, used the mobile number without country code didnt work and also tried the country code without the + sign still to no avail. what could be d problem?

thanks

@daluu
Copy link
Author

daluu commented Oct 5, 2016

Sorry for late reply, Github didn't notify me of comments to this gist.

You say no get response. What result do you see? I would try a var_dump($status) to see what you get. I would expect you should get some form of output/response (it could be not JSON or some other structure) or the code should have thrown a generic exception back at you.

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