我现在有一个icontact api。我的网页上有一个注册表单,并且有一个复选框“我想收到时事通讯。”我希望选中此框的联系人保存在我的icontact订阅者列表中(他们的电子邮件应该保存)
我的代码是:
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 = "/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 = "/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 = "/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);
}
?>;
当我使用它时它根本不起作用。我改变了变量,但仍然......
答案 0 :(得分:1)
当我加载代码时,我没有得到空白页面。我得到了一个“;”,因为这是最后关闭php标签后剩下的内容。
问题可能是您只定义了函数的代码,并且从不调用它们。
我猜你想在代码的末尾有更多这样的东西:
# Assume if there is POST data we should process the signup
if (count($_POST)) {
$email = $_POST['email'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$list = $_POST['list'];
$results = addiContact($email, $fname, $lname, $list);
print_r($results); # You should do something more useful here
}
## Generate and display the form
$selectBox = '<select name="list">';
foreach($iContactLists as $list) {
$selectBox .= "<option value=\"{$list['id']}\">{$list['txt']}</option>\n";
}
$selectBox .= '</select>';
?>
<form method="POST">
Email: <input name="email"/><br />
First name: <input name="fname"/><br />
Last name: <input name="lname"/><br />
List: <?= $selectBox ?><br />
<input type="submit" value="Sign up"/>
</form>
答案 1 :(得分:1)
PHP中的以下示例代码将帮助您从特定的iContact列表中删除联系人。
PHP代码:
$headers = array(
'Accept: text/xml',
'Content-Type: text/xml',
'Api-Version: 2.0',
'Api-AppId: ' . $app_id,
'Api-Username: ' . $user,
'Api-Password: ' . $pass
);
$ch=curl_init("https://app.sandbox.icontact.com/icp/a/$account_id/c/$folder_id/contacts/{contactId}");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$deleteResult = curl_exec($ch);
$deleteParse = simplexml_load_string($deleteResult);
print "<pre>";
print_r($deleteParse);
print "<pre>";
curl_close($ch);