Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Created July 3, 2014 13:49
Show Gist options
  • Save taichunmin/7b4f0c3f3ba36f990189 to your computer and use it in GitHub Desktop.
Save taichunmin/7b4f0c3f3ba36f990189 to your computer and use it in GitHub Desktop.
大量寄送台灣大哥大簡訊的程式 2013.03.29
<?php
/*
@author taichunmin <taichunmin@gmail.com>
@program Send Taiwan Mobile SMS by https://messaging.taiwanmobile.com/
@version 2013.03.29 taichunmin
這個程式只是暫時寫到【能夠執行】的程度,錯誤處理還沒有處理得很好。
有些流程可能有些不順,等有動力再來更新吧。
*/
@header("Content-type:text/html; charset=utf-8");
date_default_timezone_set('Asia/Taipei');
function tai_vardebug($debugArray, $display = 0)
{
global $cfg;
if($display==0 && $cfg['debug']==0) return ;
//echo '<pre>'.htmlspecialchars(var_export($debugArray,true)).'</pre>';
highlight_string("<?php \n".var_export($debugArray,true)."\n ?>");
}
$cfg['debug']=1;
class taiwan_mobile_sms_C
{
private $phone, $pass, $curl, $cookieFile, $login = false;
public $curl_info, $curl_header, $curl_html, $errMsg;
public function __construct($phone, $pass)
{
set_time_limit(0);
$this->phone = $phone;
$this->pass = $pass;
$this->cookieFile = 'taiwan_mobile_sms.txt'; // C:\AppServ\Apache2.2\taiwan_mobile_sms.txt
$this->login();
}
public function __destruct()
{
$this->logout();
}
private function curl_init($arg = array())
{
$this->curl = curl_init();
$curlopt = array(
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEFILE => $this->cookieFile, // read cookie
CURLOPT_COOKIEJAR => $this->cookieFile, // write cookies
CURLOPT_FAILONERROR => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22', // chrome://version/
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
);
curl_setopt_array($this->curl, $curlopt);
}
private function post( $url, $arg = array())
{
if( empty($url) )return false;
$this->curl_init();
$curlopt = array(
CURLOPT_POST => true,
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => (is_array($arg['data'])?http_build_query($arg['data']):$arg['data']),
CURLOPT_REFERER => (isset($arg['referer'])?$arg['referer']:''),
CURLOPT_HTTPHEADER => (is_array($arg['headers'])?$arg['headers']:array()),
);
curl_setopt_array( $this->curl, $curlopt);
$html = curl_exec( $this->curl );
$this->curl_info = curl_getinfo ($this->curl);
if( $html !== false )
{
$this->curl_header = explode("\r\n\r\n",$html, $this->curl_info['redirect_count']+2);
$ia = count($this->curl_header)-1;
$this->curl_html = $this->curl_header[ $ia ];
unset( $this->curl_header[ $ia ] );
}
return $this->sleep();
}
public function debug()
{
echo '登入狀態:'.intval($this->login).'<br />'.PHP_EOL;
tai_vardebug($this, true);/*
tai_vardebug($this->curl_info, true);
tai_vardebug($this->curl_header, true);
tai_vardebug($this->curl_html, true);*/
}
public function login()
{
$headers = array(
'Origin: https://messaging.taiwanmobile.com',
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Charset: utf-8;q=0.7,*;q=0.3',
);
$this->post('https://www.catch.net.tw/auth/login.jsp',array(
'headers' => $headers,
'referer' => 'https://messaging.taiwanmobile.com/login.do?token=',
'data' => array(
'msisdn' => $this->phone,
'passwd' => $this->pass,
'from_page' => 'member_login_full',
'return_url' => 'https://messaging.taiwanmobile.com/login.do?token=',
'srv_no' => '',
'service_id' => '',
'from_ch' => '',
'language' => '',
),
));
preg_match_all('`<input type=\'?hidden\'? name=\'?(.*?)\'? value=\'(.*?)\'>`usm', $this->curl_html, $tmp);
$post = array();
foreach( $tmp[2] as $k => $v )
$post[ $tmp[1][$k] ] = $v;
//tai_vardebug($tmp, true);
if( count($post)==15 ) // 登入成功
{
$this->post('https://messaging.taiwanmobile.com/login.do?token=', array(
'data' => $post,
'headers' => $headers,
'referer' => 'https://www.catch.net.tw/auth/login.jsp',
));
}
else if(isset($post['need_captcha']) && $post['need_captcha']=='true') // 出現驗證碼
{
$this->errMsg .= '出現驗證碼。'. PHP_EOL;
/*
驗證碼部分未處理
*/
}
else // 錯誤
{
$this->errMsg .= urldecode($post['message']). PHP_EOL;
}
$this->login = (strpos($this->curl_html, "親愛的 {$this->phone} 歡迎回來") !== false);
return $this;
}
public function send($phone, $msg)
{
if(empty($msg)) return false;
if( !$this->login ) $this->login();
$headers = array(
'Origin: https://messaging.taiwanmobile.com',
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Charset: utf-8;q=0.7,*;q=0.3',
);
if( is_array($phone) )
$phone = implode(',',$phone);
if( !preg_match('/^09\d{8}(,09\d{8})*$/',$phone) ) return false;
$post = array(
'msgType' => '', 'catRid' => '', 'userMsgRid' => '', 'folderType' => '', 'actionType' => 'update', 'imgUrl' => '', 'audioUrl' => '', 'vedioUrl' => '', 'docUrl' => '', 'isMms' => 'false', 'isDocType' => 'false', 'uiMsgType' => '0', 'sendSelf' => '', 'draftUserMsgRid' => '', 'usageCountRecord' => '', 'sendStr' => $phone, 'addressStr' => $phone, 'textContent' => $msg, 'tmpUndo' => '', 'tmpRedo' => '', 'sendType' => '1', 'startHour' => '', 'startMinute' => '', 'beginDate' => '', 'weekInterval' => '1', 'monthInterval' => '1', 'recurrentType' => '4', 'startHour1' => '', 'startMinute1' => '', 'endDate' => '', 'frequency' => '',
);
$this->post('https://messaging.taiwanmobile.com/updateComposer.do',array(
'data' => $post, 'headers' => $headers, 'referer' => 'https://messaging.taiwanmobile.com/composer.do',
));
$post = array_merge($post, array('actionType' => 'preSend'));
$this->post('https://messaging.taiwanmobile.com/composer.do',array(
'data' => $post, 'headers' => $headers, 'referer' => 'https://messaging.taiwanmobile.com/composer.do',
))->Page_session_check();
// 訊息將傳送 檢查
if( strpos($this->curl_html, '訊息將傳送') === false )
return false;
preg_match('@<form id="composerForm".*?</form>@usm', $this->curl_html, $tmp);
$post = $this->get_all_input($tmp[0]);
$post['actionType'] = 'send';
$this->post('https://messaging.taiwanmobile.com/composer.do',array(
'data' => $post, 'headers' => $headers, 'referer' => 'https://messaging.taiwanmobile.com/composer.do',
))->Page_session_check();
// 訊息傳送 檢查
if( strpos($this->curl_html, '訊息已發送') === false )
return false;
return true;
}
private function Page_session_check()
{
if( strpos( $this->curl_info['url'], 'www.catch.net.tw/auth/session_check.jsp' ) === false )
return;
//echo 'Page_session_check<br />';
$headers = array(
'Origin: https://www.catch.net.tw',
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Charset: utf-8;q=0.7,*;q=0.3',
);
preg_match_all('`<input type=\'?hidden\'? name=\'?(.*?)\'? value=\'(.*?)\'>`usm', $this->curl_html, $tmp);
$post = array();
foreach( $tmp[2] as $k => $v )
$post[ $tmp[1][$k] ] = $v;
preg_match('`<form.*?action=\'([^\']*?)\'.*?>`usm', $this->curl_html, $tmp);
$action = $tmp[1];
$this->post($action, array(
'data' => $post, 'referer' => $this->curl_info['url'], 'headers' => $headers,
));
return $this;
}
private function sleep()
{
usleep( rand(1500000,2500000) );
return $this;
}
private function get_all_input( $html = NULL )
{
if( !isset($html) ) $html = $this->curl_html;
$input = array();
preg_match_all('`<input type=[\'"]?(?:hidden|text)[\'"]? name=[\'"]?([^\'"]*)[\'"]? value=[\'"]?([^\'"]*)[\'"]?.*?>`usm', $html, $tmp);
foreach( $tmp[2] as $k => $v )
$input[ $tmp[1][$k] ] = $v;
return $input;
}
public function logout()
{
if( !$this->login ) return $this;
$this->post('https://www.catch.net.tw/auth/logout.jsp', array(
'data' => array('return_url' => 'http://messaging.taiwanmobile.com/login.do?token=&logout=true')
));
$this->post('https://messaging.taiwanmobile.com/autoLogout.do');
$this->post('http://messaging.taiwanmobile.com/login.do?token=&logout=true');
$this->login = false;
return $this;
}
}
global $sms;
/*
Example 1
$sms = new taiwan_mobile_sms_C('phonenumber', 'password');
$sms->send('phonenumber1','Example 1'); // must use UTF-8
Example 2
$sms = new taiwan_mobile_sms_C('phonenumber', 'password');
$sms->send('phonenumber1,phonenumber2','Example 2, Send Two Phone Number'); // must use UTF-8
$sms = new taiwan_mobile_sms_C('phonenumber', 'password');
$sms->send('phonenumber3','Example 2, Use Another Account'); // must use UTF-8
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment