DELPHI利用SUPEROBJECT以及INDY元件下載JSON格式做解析。
原始碼 https://github.com/superlevin/XE3JsonParser
程式下載 https://www.dropbox.com/s/bytp9vsxzht59fd/JSONParser.zip?dl=0
大型網站架構..net 架構師.rabbitMQ.redis.行動開發.APP開發教學.PHP Laravel開發..net core C# 開發.架構師之路.Delphi開發.資料庫程式.進銷存.餐飲POS系統
DELPHI利用SUPEROBJECT以及INDY元件下載JSON格式做解析。
原始碼 https://github.com/superlevin/XE3JsonParser
程式下載 https://www.dropbox.com/s/bytp9vsxzht59fd/JSONParser.zip?dl=0
主要是修補缺少的二個檔案
bin\converters\java2op\bootclasses.jar
bin\converters\java2op\bootclasses.xml
下載之後將檔案copy到以下的路徑即可
bin\converters\java2op\bootclasses.jar
bin\converters\java2op\bootclasses.xml
http://superobject.googlecode.com
https://github.com/hgourvest/superobject
Delphi從2009才開始支援JSON格式,先前的版本需使用第三方函式庫才能解決。這邊提供不錯的SuperObject給大家。
使用方式很簡單,例如從台北市政府資料開放平台取得臺北市公廁點位資訊
[json]
[{“unit”:”台北市政府環境保護局”,”title”:”公廁坐落:士林官邸”,”dep_content”:”座數:8,特優級:2,優等級:6,普通級:0,加強級:0,無障礙設施”,”address”:”臺北市士林區福林路60號”,”lng”:”121.530152″,”lat”:”25.094898″,”modifydate”:”2013-03-25T17:58:20+08:00″},{“unit”:”台北市政府環境保護局”,”title”:”公廁坐落:天母公園”,”dep_content”:”座數
:1,特優級:0,優等級:1,普通級:0,加強級:0″,”address”:”臺北市士林區中山北路七段219號邊”,”lng”:”121.530071″,”lat”:”25.125855″,”modifydate”:”2015-08-06T00:00:00+08:00″}]
[/json]
[pascal]
uses superobject, supertypes, superxmlparser;
var vjson: Isuperobject;
vitem:Tsuperarray;
i,j:integer;
s:string;
begin
vjson:= so(‘[{"unit":"台北市政府環境保護局","address":"臺北市士林區福林路60號"},{"unit":"台北市政府環境保護局","address":"臺北市士林區中山北路七段219號邊"}]’);
vitem:=vjson.AsArray;
for i:=0 to vitem.Length-1 do begin
memo2.Lines.Add(vitem[i][‘unit’].AsString+’ ‘+vitem[i][‘address’].AsString);
end;
end;
[/pascal]
那如果格式如下
[json]
{“zoo”:”壽山動物園”,”animals”:[{“name”:”猴子”,”years”:”12″},{“name”:”猩猩”,”years”:”5″}]}
[/json]
[pascal]
var vjson: Isuperobject;
vitem:Tsuperarray;
i:integer;
begin
vjson:= so(‘{"zoo":"壽山動物園","animals":[{"name":"猴子","years":"12"},{"name":"猩猩","years":"5"}]}’);
memo2.lines.add(vjson[‘zoo’].asstring);
vitem:= vjson[‘animals’].AsArray;
for i:=0 to vitem.Length -1 do begin
memo1.Lines.Add(vitem[i][‘name’].AsString+’ ‘+vitem[i][‘years’].AsString );
end;
[/pascal]
在微軟的Microsoft Azure Marketplace有許多不錯的資料可以應用,今天就分享如果利用XE8搭配Microsoft Translator線上翻譯服務來做翻譯。
一、註冊Azure帳戶
點選 https://datamarket.azure.com/dataset/bing/microsofttranslator 上的登入,使用個人。然後依照步驗註冊就好。
二、訂閱服務
進入 https://datamarket.azure.com/dataset/bing/microsofttranslator ,點選2000000字元數/月的免費方案註冊。
三、註冊程式
程式中需要client_id跟client_secret ,所以到 https://datamarket.azure.com/developer/applications 註冊程式。
client_id就是用戶端識別碼
client_secret就是用戶端密碼
四、開始建立程式
新增一個Blank Application
接著在上面增加三個元件,TRESTClient、TRESTRequest跟TRESTResponse。重新命名為RESTClientAuthToken、RESTRequestAuthToken跟RESTResponseAuthToken.
RESTClientAuthToken的BaseURL設定 https://datamarket.accesscontrol.windows.net/v2
RESTRequestAuthToken的Method改為rmPOST、然後Resource設為OAuth2-13(參考 https://msdn.microsoft.com/en-us/library/hh454950.aspx)
然後在Params增加四個參數如下
在畫面上增加3個label、3個edit以及一個button
加上程式碼
[pascal]
var
token: string;
begin
RESTRequestAuthToken.Params.ParameterByName(‘client_secret’).Value := EditClient_Secret.Text;
RESTRequestAuthToken.Params.ParameterByName(‘client_id’).Value := EditClient_ID.Text;
RESTRequestAuthToken.Execute;
if RESTResponseAuthToken.GetSimpleValue(‘access_token’,token) then
begin
EditToken_value.Text := token;
end;
[/pascal]
取得Token值後我們可以開始進行翻譯,參考的文件為(https://msdn.microsoft.com/en-us/library/ff512387.aspx),一樣增加三個元件,TRESTClient、TRESTRequest跟TRESTResponse。重新命名為RESTClientTranslate、RESTRequestTranslate跟RESTResponseTranslate.。
RESTClientTranslate的BaseURL設為 http://api.microsofttranslator.com/v2/Http.svc
接著在RESTRequestTranslate的Resource設為Translate?text={text}&from={from}&to={to}
一樣在裡面增加四個Params,from跟to需要對應相關的語言代碼(參考https://msdn.microsoft.com/en-us/library/hh456380.aspx)
在畫面上增加combobox二個,以及二個memo及button。
開啟view→LiveBindings Designer,將RESTRequestTranslate中的Params.text指到Memo1的Text(輸入),然後RESTResponseTranslate.的Content指向Memo2的Text(輸出)。
最後補上翻譯的程式碼如下。
[pascal]
RESTRequestTranslate.Params.ParameterByName(‘Authorization’).Value := ‘bearer ‘+EditToken_value.Text;
RESTRequestTranslate.Params.ParameterByName(‘from’).Value := cbFrom.Selected.Text;
RESTRequestTranslate.Params.ParameterByName(‘to’).Value := cbToo.Selected.Text;
RESTRequestTranslate.Execute;
[/pascal]
番外篇:
語音的部份(參考 https://msdn.microsoft.com/en-us/library/ff512420.aspx)
增加一個RESTRequestPlay,然後設定Resource為 speak?text={text}&language={language}&to={to}&format=audio/mp3&options=MinSize 。
三個Param為 Authorization、text、language
以及增加mediaplayer。
程式如下
[pascal]
var
MS: TMemoryStream;
TempFile: string;
begin
RESTRequestPlay.Params.ParameterByName(‘Authorization’).Value := ‘bearer ‘+EditToken_value.Text;
RESTRequestPlay.Params.ParameterByName(‘language’).Value := RESTRequestTranslate.Params.ParameterByName(‘to’).Value;
RESTRequestPlay.Params.ParameterByName(‘text’).Value := RESTResponseTranslate.Content;
RESTRequestPlay.Execute;
MS := TMemoryStream.Create;
try
MS.WriteData(RESTResponsePlay.RawBytes,Length(RESTResponsePlay.RawBytes));
TempFile := TPath.ChangeExtension(TPath.GetTempFileName,’.mp3′);
MS.SaveToFile(TempFile);
MediaPlayer1.FileName := TempFile;
MediaPlayer1.Play;
finally
MS.Free;
end;
end;
[/pascal]
API參考
https://msdn.microsoft.com/en-us/library/dd576287.aspx
程式碼下載
https://drive.google.com/file/d/0BxMN7KkA7p3NZlU5clItaGxiMzQ/view?usp=sharing
原文:
http://blogs.embarcadero.com/stephenball/2015/06/30/using-azure-translator-services-with-delphi/
[sql]
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`)
);
[/sql]
修改 application/config/config.php
[php]
$config[‘sess_driver’] = ‘database’;
$config[‘sess_save_path’] = ‘ci_sessions’;
[/php]
以往許多人喜歡在資料庫的密碼欄使用明碼儲存,但站在資訊安全角度來說不是挺安全的,可以產生一個Salt值後,增加安全性。
[php]
// 隨機產生一組長度為10的Salt值
$salt = substr(md5(uniqid(rand(), true)), 0, 10);
// 存入資料庫的密碼欄,使用Salt值加上md5雜湊後存回資料庫
$new_password = md5(md5($old_password.$salt));
[/php]
使用者登入後,先從資料庫讀出salt值,接著再一樣加上salt值加上md5後比對就可以了!
1 下載PHPExcel,將PHPExcel解壓縮到Application的third_party目錄下
2 在libraries目錄下新增 excel.php
[php]
<?php
if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel {
public function __construct() {
parent::__construct();
}
}
?>
[/php]
使用
[php]
function createexcel(){
// 產生php 範例
$this->load->library(‘excel’);
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘中文’);
$objPHPExcel->getActiveSheet()->setCellValue(‘B2’, ‘許’);
$objPHPExcel->getActiveSheet()->setCellValue(‘C3’, ‘test3’);
$objPHPExcel->getActiveSheet()->setCellValue(‘D3’, ‘test4’);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007’);
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename="excel_report_’ . date(‘ymd’) . ‘.xls"’);
// header(‘Content-Disposition: attachment; filename="file.xls"’);
header(‘Cache-Control: max-age=0’);
$objWriter->save(‘php://output’);
}
[/php]
這次更新分為二種版本,分為有買維護合約且有效期的以及沒有買維護合約的版本。差別如下~
This update is provided to XE8 customers without an Update Subscription
Subscription Update 1 Features and Fix List
This update is provided to active Update Subscription customers
延伸連結
Fix List for XE8 Update 1:
http://edn.embarcadero.com/article/44470
[php]
<?php
$xml_data ='<?xml version="1.0" encoding="UTF-8"?>
<FILE>
<HEAD>
<FILEDESC>ORDER</FILEDESC>
</HEAD>
<CONTENT>
<DATA>
<ID>XXXX</ID>
<MEMO>XXXXXXX</MEMO>
</DATA>
</CONTENT>
</FILE>
‘;
$URL = "http://apisite";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
[/php]