<?php
namespace
appcomponents;
use
yiibaseComponent;
set_time_limit(0);
class
SMS
extends
Component
{
public
$apiKey
;
public
$secretKey
;
public
$lineNumber
;
public
function
send(
$to
,
$text
)
{
$token
=
$this
->getToken();
if
(
$token
!= false) {
$postData
=
array
(
'Messages'
=> [
$text
],
'MobileNumbers'
=>
is_array
(
$to
) ?
$to
: [
$to
],
'LineNumber'
=>
$this
->lineNumber,
'SendDateTime'
=>
''
,
'CanContinueInCaseOfError'
=>
'false'
,
);
$url
=
$this
->getAPIMessageSendUrl();
$SendMessage
=
$this
->execute(
$postData
,
$url
,
$token
);
$object
= json_decode(
$SendMessage
);
if
(
is_object
(
$object
)) {
$array
= get_object_vars(
$object
);
if
(
is_array
(
$array
)) {
$result
=
$array
[
'Message'
];
}
else
{
$result
= false;
}
}
else
{
$result
= false;
}
}
else
{
$result
= false;
}
return
$result
;
}
private
function
getToken()
{
$postData
=
array
(
'apiKey'
=>
$this
->apiKey,
'SecretKey'
=>
$this
->secretKey,
'System'
=>
'php_rest_v_1_1'
);
$postString
= json_encode(
$postData
);
$ch
= curl_init(
$this
->getApiTokenUrl());
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
array
(
'Content-Type: application/json'
));
curl_setopt(
$ch
, CURLOPT_HEADER, false);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt(
$ch
, CURLOPT_POST,
count
(
$postString
));
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$postString
);
$result
= curl_exec(
$ch
);
curl_close(
$ch
);
$response
= json_decode(
$result
);
$resp
= false;
if
(
is_object
(
$response
)) {
$resultVars
= get_object_vars(
$response
);
if
(
is_array
(
$resultVars
)) {
@
$IsSuccessful
=
$resultVars
[
'IsSuccessful'
];
if
(
$IsSuccessful
== true) {
@
$TokenKey
=
$resultVars
[
'TokenKey'
];
$resp
=
$TokenKey
;
}
}
}
return
$resp
;
}
private
function
getApiTokenUrl()
{
}
private
function
getAPIMessageSendUrl()
{
}
private
function
execute(
$postData
,
$url
,
$token
)
{
$postString
= json_encode(
$postData
);
$ch
= curl_init(
$url
);
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
array
(
'Content-Type: application/json'
,
'x-sms-ir-secure-token: '
.
$token
));
curl_setopt(
$ch
, CURLOPT_HEADER, false);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt(
$ch
, CURLOPT_POST,
count
(
$postString
));
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$postString
);
$result
= curl_exec(
$ch
);
curl_close(
$ch
);
return
$result
;
}
}