CodeIgniter發送台灣簡訊範例

<?php
$username = "xxx";		// 帳號
$password = "xxx";		    // 密碼
$mobile = "09xxxxxxxx";	// 電話
$message = "測試簡訊";	// 簡訊內容
$MSGData = "";

$msg = "username=".$username."&password=".$password."&mobile=".$mobile."&message=".urlencode($message)."&drurl=".urlencode("http://xxxx.com.tw/admin/drul/");
$num = strlen($msg);		

// 打開 API 閘道
$fp = fsockopen ("api.twsms.com", 80);
if ($fp) {
	$MSGData .= "POST /smsSend.php HTTP/1.1\r\n";
	$MSGData .= "Host: api.twsms.com\r\n";
	$MSGData .= "Content-Length: ".$num."\r\n";
	$MSGData .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$MSGData .= "Connection: Close\r\n\r\n";
	$MSGData .= $msg."\r\n";
	fputs ($fp, $MSGData);
    $Tmp ="";
	// 取出回傳值
	while (!feof($fp)) $Tmp.=fgets($fp,128); 
    $temp2 = explode(PHP_EOL, $Tmp);
	// 關閉閘道
	fclose ($fp);
    /*
     0 傳送完成:HTTP/1.1 200 OK
     1 Date: Wed, 03 Jun 20xx 09:31:36 GMT
     2 Server: Apache
     3 X-Powered-By: PHP/5.2.12
     4 Connection: close
     5 Transfer-Encoding: chunked
     6 Content-Type: text/html
     7 
     8 51 
     9 <smsResp><code>00000</code><text>Success</text><msgid>188xxxx69</msgid></smsResp>
    10 0
    */
    $xml = simplexml_load_string($temp2[9]);
    $xmldata = json_decode( json_encode($xml) , 1);
	echo "傳送完成:".$Tmp."<br/>".$xmldata['msgid'];
} else {
	echo "您無法連接 TwSMS API Server";
}	                
		$this->load->view('welcome_message',$data);

	
	}

	public function drul(){
        $this->load->database();            
        $inputdata = $this->input->get('xml');
        $xml = simplexml_load_string($inputdata);
        $xmldata = json_decode( json_encode($xml) , 1);
        $arraystring =print_r($xmldata);
        $this->msg=$arraystring;

        $dbvalue = array(
          'sms_msg'=> $xmldata['msgid']."status=".$xmldata['statustext'],
          'id' =>$xmldata['code'],
          'ip' => $this->input->ip_address(),
        );
        $this->db->insert('drul',$dbvalue );		
		$data = array(
                    'message' => $xmldata['msgid']
                );
		$this->load->view('welcome_message',$data);
	}

Delphi XE3 簡訊發送問題

把手上的Delphi7自動發送簡訊系統轉到XE3,發現一直出現亂碼。
後來找到原因是IdHTTP.URL.ParamsEncode的問題,在Delphi7跟Delphi XE3處理出來的結果是不同的,於是在網路上找到一段改編自Delphi 7的版本。果然就行了!

function EnCode(ACode: string): string;
var
  I: Integer;
  Hex: string;
  Code: AnsiString;
begin
  Code := AnsiString(ACode);
  for I := 1 to Length(Code) do
    case Code[i] of
      ' ': Result := Result + '+';
      'A'..'Z', 'a'..'z', '*', '@', '.', '_', '-',
        '0'..'9', '$', '!', '''', '(', ')':
        Result := Result + Code[i];
    else
      begin
        Hex := IntToHex(ord(Code[i]), 2);
        if Length(Hex) = 2 then
          Result := Result + '%' + Hex
        else
          Result := Result + '%0' + hex;
      end;
    end;
end;