Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (56 sloc) 1.77 KB
<?php
/*
coolsms-message-v4 php example
get group list
*/
$configFile = file_get_contents("./config.json");
$config = json_decode($configFile, true);
function get_header() {
global $config;
# apiKey && apiSecret are acquired from coolsms.co.kr/credentials
$apiKey = $config["apiKey"];
$apiSecret = $config["apiSecret"];
date_default_timezone_set('Asia/Seoul');
$date = date('Y-m-d\TH:i:s.Z\Z', time());
$salt = uniqid();
$signature = hash_hmac('sha256', $date.$salt, $apiSecret);
return "Authorization: HMAC-SHA256 apiKey={$apiKey}, date={$date}, salt={$salt}, signature={$signature}";
}
function get_group_list() {
$url = "https://rest.coolsms.co.kr/messages/v4/groups";
$data = new stdClass();
$data->limit = 2;
$result = request("GET", $url, $data);
print_r("Group list : {$result}\n");
}
function request($method, $url, $data = false) {
echo "{$method} {$url}\n";
try {
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
if ($data) curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data) curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
default:
if ($data) $url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, array(get_header(), "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (curl_error($curl)) {
print curl_error($curl);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
} catch (Exception $err) {
echo $err;
}
}
get_group_list();
?>