WordPress 三竹簡訊發送外掛
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
您好,我是怡菁,我是在網路上搜尋到您的。因為我想在WP網站上做會員註冊時驗證手機號碼的功能,目前也考慮使用三竹簡訊。正好查到了您的github,很感謝您提供了這段程式碼,對初學者來說非常有參考價值,謝謝。