Skip to content

Instantly share code, notes, and snippets.

@nczz
Last active April 15, 2024 16:09
Show Gist options
  • Save nczz/750c3d8d18b9e0ca07ca06a248c11570 to your computer and use it in GitHub Desktop.
Save nczz/750c3d8d18b9e0ca07ca06a248c11570 to your computer and use it in GitHub Desktop.
WordPress 三竹簡訊發送外掛
<?php
/*
Plugin Name: 三竹簡訊 Webhook
Plugin URI: https://gist.github.com/nczz/750c3d8d18b9e0ca07ca06a248c11570
Description: 接收 Webhook 請求發送簡訊
Author: Chun
Version: 1.0
Author URI: https://www.mxp.tw/
*/
/**
** @param username 三竹簡訊用戶帳號
** @param password 三竹簡訊用戶密碼
** @param mobile 台灣手機號碼
** @param text 簡訊內容。切勿混用非中英文等其他語言。
**/
function mxp_mitake_sms_text_webhook() {
if (!isset($_POST['mobile']) || $_POST['mobile'] == "" || !isset($_POST['username']) || $_POST['username'] == "" ||
!isset($_POST['password']) || $_POST['password'] == "" || !isset($_POST['text']) || $_POST['text'] == "") {
wp_send_json_error(array('code' => 502, 'data' => '', 'msg' => '資料不正確的錯誤的請求,加油好嗎!'));
}
$username = $_POST['username'];
$password = $_POST['password'];
$mobile = str_replace(array(' ', '-'), '', $_POST['mobile']);
$text = $_POST['text'];
if (strpos($mobile, '09') !== 0 || strlen($mobile) !== 10) {
wp_send_json_error(array('code' => 503, 'data' => '', 'msg' => '手機格式錯誤惹~'));
}
$package = array(
'username' => $username,
'password' => $password,
'dstaddr' => $mobile,
'smbody' => $text,
'encoding' => 'UTF8',
);
$url = 'http://smexpress.mitake.com.tw:9600/SmSendGet.asp?' . http_build_query($package, '', '&', PHP_QUERY_RFC3986);
$args = array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.1',
'user-agent' => 'WordPress',
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => false,
'stream' => false,
'filename' => null,
);
$response = wp_remote_get($url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
wp_send_json_error(array('code' => 504, 'data' => '', 'msg' => $error_message));
} else {
wp_send_json_success(array('code' => 200, 'data' => json_encode($response)));
}
}
add_action('wp_ajax_nopriv_mxp_mitake_sms_text_webhook', 'mxp_mitake_sms_text_webhook');
@bonbeann
Copy link

bonbeann commented May 7, 2021

您好,我是怡菁,我是在網路上搜尋到您的。因為我想在WP網站上做會員註冊時驗證手機號碼的功能,目前也考慮使用三竹簡訊。正好查到了您的github,很感謝您提供了這段程式碼,對初學者來說非常有參考價值,謝謝。

@cwy104seung28
Copy link

您好,這邊想串接簡訊驗證,想詢問匯入外掛後該如何使用呢?

@nczz
Copy link
Author

nczz commented Sep 26, 2023

@cwy104seung28 這隻外掛只是提供一個 Webhook API ,實際上你還是要寫個前端或是後端來把事件串起來,讓事件去觸發「發送簡訊」來呼叫這外掛裡的方法處理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment