reasonableframework/helper/twilio.api.php

71 lines
1.7 KiB
PHP
Raw Normal View History

2019-04-08 06:57:38 +00:00
<?php
/**
* @file twilio.api.php
* @date 2019-04-08
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Twilio REST API interface module
* @documentation https://www.twilio.com/docs/sms/send-messages
*/
2019-04-15 06:18:52 +00:00
if(!check_function_exists("twilio_get_config")) {
function twilio_get_config() {
$config = get_config();
return array(
"sid" => get_value_in_array("twilio_sid", $config, ""),
"token" => get_value_in_array("twilio_token", $config, ""),
);
}
}
2019-04-10 11:29:53 +00:00
if(!check_function_exists("twilio_send_message")) {
2019-04-10 11:28:40 +00:00
function twilio_send_message($message, $from, $to, $sid, $token) {
$response = false;
if(loadHelper("webpagetool")) {
$request_url = sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", $sid);
$response = get_web_json($request_url, "post", array(
"headers" = array(
2019-04-15 05:15:08 +00:00
"Content-Type" => "application/x-www-form-urlencoded",
2019-04-15 06:18:52 +00:00
"Authentication" => array("Basic", $sid, $token),
2019-04-10 11:28:40 +00:00
),
"data" => array(
"Body" => $message,
"From" => $from,
"To" => $to,
),
);
}
return $response;
}
}
2019-04-10 11:29:53 +00:00
if(!check_function_exists("twilio_send_voice")) {
function twilio_send_voice($url="", $from, $to, $sid, $token) {
2019-04-10 11:28:40 +00:00
$response = false;
2019-04-10 11:29:53 +00:00
if(empty($url)) {
$url = "http://demo.twilio.com/docs/voice.xml";
}
2019-04-10 11:28:40 +00:00
if(loadHelper("webpagetool")) {
$request_url = sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Calls.json", $sid);
$response = get_web_json($request_url, "post", array(
"headers" = array(
2019-04-15 05:15:08 +00:00
"Content-Type" => "application/x-www-form-urlencoded",
2019-04-15 06:18:52 +00:00
"Authentication" => array("Basic", $sid, $token),
2019-04-10 11:28:40 +00:00
),
"data" => array(
"Url" => $url,
"From" => $from,
"To" => $to,
),
);
}
return $response;
}
}