呼叫 VIdegree Api 的方式

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

呼叫 VIdegree Api 的方式

文章rusli » 週二 11月 13, 2012 1:12 pm

[syntax="php"]
<?php
class Util
{
public static function Post($URL, $data) { //Initiate cURL request and send back the result
$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)) {
echo 'ch:' . $ch;
print curl_error($ch);
} else {
curl_close($ch);
}
return $resulta;
}

public static function replaceData($input){
$input = str_replace("\n", "", $input);
$input = str_replace("\t", "", $input);
$input = str_replace("\r", "", $input);
return $input;
}
}
?>
[/syntax]

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

資料準備

文章rusli » 週二 11月 13, 2012 1:13 pm

[syntax="php"]

$toDatabase = "[{
\"CustomerNo\" : \"1\",
\"CurrentName\" : \"楊東城\",
\"FirstName\" : \"東城\",
\"LastName\" : \"楊\",
\"EnglishName\" : \"Tony Yang\",
\"Gender\" : \"Male\",
\"Birthday\" : \"1966-03-28\",
\"Notes\" : \"備註\",
\"Company\" : \"公司名稱\",
\"Department\" : \"部門\",
\"JobTitle\" : \"職稱\",
\"Nickname\" : \"暱稱\",
\"Labels\" : \"第一個標籤;第二個標籤\",
\"HomeCountry\" : \"國家\",
\"ExtendedField\" :
{
\"屬性一\" : \"屬性一值1\",
\"屬性二\" : \"屬性二值2\",
\"屬性三\" : \"屬性三值3\",
\"屬性四\" : \"屬性四值4\"
},
\"Mobiles\" : [
{
\"ContactMechType\" : \"手機\",
\"Values\" : [\"0933312346;0933312347\"]
}],
\"Fax\" : [
{
\"ContactMechType\" : \"傳真\",
\"Values\" : [\"1234567890#1234\"]
},
{
\"ContactMechType\" : \"公司傳真\",
\"Values\" : [\"1234567890#1234\"]
}],
\"Messagers\" : [
{
\"ContactMechType\" : \"GoogleTalk\",
\"Values\" : [\"g1@google.com\"]
},
{
\"ContactMechType\" : \"Messager\",
\"Values\" : [\"g1@Messager.com\"]
},
{
\"ContactMechType\" : \"Yahoo\",
\"Values\" : [\"g1@Yahoo.com\"]
}],
\"HomePhones\" : [
{
\"ContactMechType\" : \"住家電話\",
\"Values\" : [\"0225878787\",\"0345678903\"]
}],
\"Emails\" : [
{
\"ContactMechType\" : \"我的電郵地址\",
\"Values\" : [\"電郵1@videgree.com;電郵2@videgree.com\"]
}],
\"Urls\" : [
{
\"ContactMechType\" : \"網址\",
\"Values\" : [\"http://www.videgree.com\"]
},
{
\"ContactMechType\" : \"我的部落格\",
\"Values\" : [\"http://blog.videgree.com/第1個;http://blog.videgree.com/第2個\"]
}],
\"Addrs\" : [
{
\"ContactMechType\" : \"住家地址\",
\"Values\" : [\"地址1;地址2\"]
},
{
\"ContactMechType\" : \"我的地址\",
\"Values\" : [\"地址1;地址2\"]
}]
}]";

[/syntax]

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

呼叫方式

文章rusli » 週二 11月 13, 2012 1:15 pm

[syntax="php"]

// 在您的店點取得ApiKey
$API_KEY='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// {your store site name} 請替換成您的店名
$mySite = "https://www.videgree.com/{your store site name}";
$toURL = $mySite . "/Api.mvc/HttpsIndex";

// 去掉不必要的字元
$toDatabase = Util::replaceData($toDatabase);
// Base64 編碼
$Base64Data = base64_encode($toDatabase);

// 送出Json
$postJson = "[\"CreateCustomers\", \"" . $Base64Data . "\", \"" . $API_KEY . "\"]";

// POST 出去
echo '<br>result:' . Util::Post($toURL, $postJson);


[/syntax]

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

範例程式下載

文章rusli » 週二 11月 13, 2012 1:18 pm

PHPPost.rar
(4.25 KiB) 已下載 22 次


回到「php」

誰在線上

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