Skip to content

Instantly share code, notes, and snippets.

@iMojtaba
Created February 26, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMojtaba/1d445a26bdb848b0ba1056d37b2404f9 to your computer and use it in GitHub Desktop.
Save iMojtaba/1d445a26bdb848b0ba1056d37b2404f9 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: mojtaba
* Date: 6/4/18
* Time: 4:25 PM
*/
class FMV_Smsir extends FMV_SMS_Abstract
{
public static function send($to, $verification_code)
{
$token = self::getToken();
if ($token == false) {
return false;
}
$body = array(
'Code' => $verification_code,
'MobileNumber' => $to,
);
$header = array(
'Content-Type: application/json',
'x-sms-ir-secure-token: ' . $token
);
$postString = json_encode($body);
$ch = curl_init('https://ws.sms.ir/api/VerificationCode');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$res_body = curl_exec($ch);
curl_close($ch);
$res_body = json_decode($res_body);
if (is_object($res_body)) {
if ($res_body->IsSuccessful) {
return true;
}
return false;
} else {
return false;
}
}
public static function getToken()
{
$body = array(
'UserApiKey' => self::get_option('api_key'),
'SecretKey' => self::get_option('secret_key'),
'System' => 'php_rest_v_2_0'
);
$header = array(
'Content-Type: application/json'
);
$response = wp_remote_post('https://ws.sms.ir/api/Token', array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => $header,
'body' => $body,
'cookies' => array()
));
if (is_wp_error($response)) {
return false;
} else {
$res_body = json_decode(wp_remote_retrieve_body($response));
$is_successful = $res_body->IsSuccessful;
if ($is_successful == true) {
$token = $res_body->TokenKey;
return $token;
} else {
return false;
}
}
}
public function add_settings_fields()
{
$id = get_called_class();
add_settings_field('secret_key', __('Secret Key', 'force-mobile'), array($this, 'secret_key_callback'),
'force_mobile_general_page_' . $id, $id . '_section');
add_settings_field('api_key', __('API Key', 'force-mobile'), array($this, 'api_key_callback'),
'force_mobile_general_page_' . $id, $id . '_section');
}
public function secret_key_callback()
{
printf('<input class="regular-text" type="text" name="force_mobile_options[' . get_called_class() . '][secret_key]" id="secret_key" value="%s">',
self::get_option('secret_key'));
}
public function api_key_callback()
{
printf('<input class="regular-text" type="text" name="force_mobile_options[' . get_called_class() . '][api_key]" id="api_key" value="%s">',
self::get_option('api_key'));
}
}
new FMV_Smsir(__('Sms.Ir (sms.ir)', 'force-mobile'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment