呼叫API

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

呼叫API

文章rusli » 週二 10月 02, 2012 3:30 pm

server post

[syntax="php"]
class Util
{
public static function Post($URL, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $URL);
// curl_setopt($ch, CURLOPT_USERAGENT, $this->_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookie_file_path);
// curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
$resulta = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
return $resulta;
}
}
[/syntax]

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

DES 加解密

文章rusli » 週二 10月 02, 2012 3:30 pm

DES 加解密

[syntax="php"]
class DES
{
public static function replaceData($input){
$input = str_replace("\n", "", $input);
$input = str_replace("\t", "", $input);
$input = str_replace("\r", "", $input);
return $input;
}

public static function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}

public static function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, -1 * $pad);
}

public static function encrypt($key, $data) {
$size = mcrypt_get_block_size('des', 'ecb');
$data = DES::pkcs5_pad($data, $size);
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $data;
}

public static function decrypt($key, $data) {
$td = mcrypt_module_open('des','','ecb','');
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
@mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$result = DES::pkcs5_unpad($decrypted);
return $result;
}
}

[/syntax]

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

如何呼叫使用

文章rusli » 週二 10月 02, 2012 3:34 pm

[syntax="php"]
// 目標url
$toURL = "http://www.videgree.com/{your store site name}/Api.mvc/Index";

// 加密用 api key
$API_KEY='12345678';

// json 資料
$toDatabase = "[{
\"CurrentName\" : \"".$_POST['CurrentName']."\",
\"FirstName\" : \"".$_POST['FirstName']."\",
\"LastName\" : \"".$_POST['LastName']."\",
\"Gender\" : \"".$_POST['Gender']."\",
\"Birthday\" : \"".$_POST['Birthday']."\",
\"Notes\" : \"".$_POST['Notes']."\",
\"Company\" : \"".$_POST['Company']."\",
\"Department\" : \"".$_POST['Department']."\",
\"Nickname\" : \"".$_POST['Nickname']."\",
\"Labels\" : \"".$_POST['Labels']."\",
\"Mobiles\" : [
{
\"ContactMechType\" : \"手機\",
\"Values\" : [\"".$mobile."\"]
}],
\"Fax\" : [
{
\"ContactMechType\" : \"傳真\",
\"Values\" : [\"".$_POST['Fax']."\"]
}],
\"HomePhones\" : [
{
\"ContactMechType\" : \"住家電話\",
\"Values\" : [\"".$HomePhone."\"]
}],
\"Emails\" : [
{
\"ContactMechType\" : \"我的電郵地址\",
\"Values\" : [\"".$Email."\"]
}],
\"Urls\" : [
{
\"ContactMechType\" : \"網址\",
\"Values\" : [\"".$Url."\"]
}],
\"Addrs\" : [
{
\"ContactMechType\" : \"住家地址\",
\"Values\" : [\"".$Addrs1."\"]
},
{
\"ContactMechType\" : \"送貨地址\",
\"Values\" : [\"".$Addrs2."\"]
}]
}]";

$toDatabase = DES::replaceData($toDatabase);
$postData = base64_encode(DES::encrypt($API_KEY, $toDatabase));
$postJson = "[\"CustomerImport\", \"" . $postData . "\"]";

// 把資料 post 出去
Util::Post($toURL, $postJson);
[/syntax]


回到「php」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客