我現在有一個icontact api.I在我的網頁上有一個註冊表單,並且有「我想收到簡報」複選框。我希望聯繫人選中此框保存在我的IContact接口的用戶列表(其電子郵件應保存)將iContact API集成到我的網頁代碼中
這是我的代碼是這樣的:
Download: icontact.api.functions.php
<?php
$iContactLists[1] = array('id' => '<list-id-number>', 'txt' => 'Weekly Newsletter');
$iContactLists[2] = array('id' => '<list-id-number>', 'txt' => 'Products Promotions and Specials');
$iContactLists[3] = array('id' => '<list-id-number>', 'txt' => 'Event Notifications');
########################################################################
## Example call for the function below... ##
# ##
# addiContact($email, $fname, $lname, $iContactLists[$signup_key]); ##
########################################################################
function addiContact($email, $fname, $lname, $list) {
$listId = $list['id'] ? $list['id'] : 0;
$listName = $list['txt'] ? $list['txt'] : 'UNKNOWN';
$retVal = 0;
$result = getContact($email, $list);
if($contactId = $result['id']) {
$retVal = 1;
$retMsg = "<p>Thank you for subscribing to the {$listName} mailing list!</p>\r\n";
} else {
$result = getContact($email);
$result = !$result['id'] ? addContact($email, $fname, $lname) : $result;
$contactId = $result['id'];
if(0 < $contactId) $result = subscribeContactToList($contactId, $list);
$retVal = $result['val'];
$retMsg = $result['msg'];
}
return array('val' => $retVal, 'msg' => $retMsg, 'id' => $contactId);
}
##################################################################################
## There should not be any need to make direct calls to functions below here. ##
##################################################################################
##################################################################################
define('STATUS_CODE_SUCCESS', 200);
$iContactAPIVars = array(
'apiUrl' => 'https://app.icontact.com/icp',
'appUser' => '<appUser>', 'appPass' => '<appPass>',
'appId' => '<appId>',
'accId' => '<accId>', 'cliId' => '<cliId>'
);
function WebCodeError($error_code) {
$retMsg = "Error Code: `{$error_code}` - ";
switch($error_code) {
case 0:
case "": $retMsg.= "Connection to API Unavailable"; break;
case 200: $retMsg.= "OK"; break;
case 400: $retMsg.= "Bad Request"; break;
case 401: $retMsg.= "Not Authorized"; break;
case 402: $retMsg.= "Payment Required"; break;
case 403: $retMsg.= "Forbidden"; break;
case 404: $retMsg.= "Not Found"; break;
case 405: $retMsg.= "Method Not Allowed"; break;
case 406: $retMsg.= "Not Acceptable"; break;
case 415: $retMsg.= "Unsupported Media Type"; break;
case 500: $retMsg.= "Internal Server Error"; break;
case 501: $retMsg.= "Not Implemented"; break;
case 503: $retMsg.= "Service Unavailable"; break;
case 507: $retMsg.= "Insufficient Space"; break;
default: $retMsg.= "UNHANDLED ERROR"; break;
}
return $retMsg;
}
function callResource($url, $method, $data = null) {
global $iContactAPIVars;
$url = $iContactAPIVars['apiUrl'] . $url;
$handle = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'API-Version: 2.0',
'API-AppId: ' . $iContactAPIVars['appId'],
'API-Username: ' . $iContactAPIVars['appUser'],
'API-Password: ' . $iContactAPIVars['appPass'],
);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
switch($method) {
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'PUT':
curl_setopt($handle, CURLOPT_PUT, true);
$file_handle = @fopen($data, 'r');
curl_setopt($handle, CURLOPT_INFILE, $file_handle);
break;
case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default: break;
}
$response = curl_exec($handle);
$response = json_decode($response, true);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return array('code' => $code, 'data' => $response);
}
function getContact($email, $list = false) {
global $iContactAPIVars;
$listId = $list['id'] ? $list['id'] : 0;
$listName = $list['txt'] ? $list['txt'] : 'UNKNOWN';
$contactId = 0;
$retStatus = "no_status_loaded";
if(!$email)
return array('id' => $contactId, 'msg' => "Invalid parameter values!<br/>\r\n", 'status' => $retStatus);
$callURI = "https://stackoverflow.com/a/{$iContactAPIVars['accId']}/c/{$iContactAPIVars['cliId']}/contacts? email=".$email;
$callURI.= $listId ? "&listId=".$listId : "";
$response = callResource($callURI, 'GET');
if($response['code'] == STATUS_CODE_SUCCESS) {
$contact = $response['data']['contacts'][0];
if($contact['contactId']) {
$contactId = $contact['contactId'];
$retStatus = $contact['status'];
$retMsg = "Successfully found contact!<br/>\r\n";
} else {
$retMsg = "Contact '{$email}' not found".($listId ? "in the {$listName} mailing list" : "").".<br/>\r\n";
}
} else {
$retMsg = "<p>A problem was encountered while looking to see if you already are subscribed to the {$listName} mailing list.</p>\r\n";
$retMsg.= "<p>".WebCodeError($response['code'])."</p>\r\n";
$retMsg.= "<p>Call URI: {$callURI}</p>\r\n";
}
return array('id' => $contactId, 'msg' => $retMsg, 'status' => $retStatus);
}
function addContact($email, $fname, $lname) {
global $iContactAPIVars;
$contactId = 0;
if(!$email || !$fname || !$lname) return array('id' => $contactId, 'msg' => "Invalid parameter values!<br/>\r\n");
$callURI = "https://stackoverflow.com/a/{$iContactAPIVars['accId']}/c/{$iContactAPIVars['cliId']}/contacts";
$callValues = array('email' => $email, 'firstName' => $fname, 'lastName' => $lname, 'status' => 'normal');
$response = callResource($callURI, 'POST', array($callValues));
if($response['code'] == STATUS_CODE_SUCCESS) {
$contactId = $response['data']['contacts'][0]['contactId'];
$retMsg = "<p>Contact added!</p>\r\n";
} else {
$retMsg = "<p>A problem was encountered while adding your e-mail information.</p>\r\n";
$retMsg.= "<p>".WebCodeError($response['code'])."</p>\r\n";
$retMsg.= "<p>Call URI: {$callURI}</p>\r\n";
$retMsg.= "<p>Call Values:<br/>\r\n";
foreach($callValues as $key => $val) $retMsg.= " [{$key}] => {$val}<br/>\r\n";
$retMsg.= "</p>\r\n";
}
return array('id' => $contactId, 'msg' => $retMsg);
}
function subscribeContactToList($contactId, $list) {
global $iContactAPIVars;
$listId = $list['id'] ? $list['id'] : 0;
$listName = $list['txt'] ? $list['txt'] : 'UNKNOWN';
$retVal = 0;
if(!$contactId || !$listId) return array('val' => $retVal, 'msg' => "Invalid parameter values!<br/>\r\n");
$callURI = "https://stackoverflow.com/a/{$iContactAPIVars['accId']}/c/{$iContactAPIVars['cliId']}/subscriptions";
$callValues = array('contactId' => $contactId, 'listId' => $listId, 'status' => 'normal');
$response = callResource($callURI, 'POST', array($callValues));
if($response['code'] == STATUS_CODE_SUCCESS) {
$retVal = 1;
$retMsg = "<p>Thank you for subscribing to the {$listName} mailing list!</p>\r\n";
} else {
$retMsg = "<p>A problem was encountered while adding you to the {$listName} mailing list.</p>\r\n";
$retMsg.= "<p>".WebCodeError($response['code'])."</p>\r\n";
$retMsg.= "<p>Call URI: {$callURI}</p>\r\n";
$retMsg.= "<p>Call Values:<br/>\r\n";
foreach($callValues as $key => $val) $retMsg.= " [{$key}] => {$val}<br/>\r\n";
$retMsg.= "</p>\r\n";
}
return array('val' => $retVal, 'msg' => $retMsg);
}
?>;
當我在使用這個all.I是行不通的改變變量,但仍然...
定義 「並沒有在所有的工作」。來吧。錯誤?空白頁?你的服務器日誌說什麼? – ceejayoz
它只返回空白頁:( – user952691