Last active
October 5, 2016 22:58
-
-
Save daluu/73815784246a71c01797 to your computer and use it in GitHub Desktop.
Simple PHP curl demo for Textbelt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.