Php 将iContact API集成到我的网页中';s代码

Php 将iContact API集成到我的网页中';s代码,php,api,Php,Api,我现在有了一个icontact api。我的网页上有一个注册表单,并且有一个复选框“我想接收时事通讯”。我希望选中此框的联系人保存在我的icontact订户列表中。(他们的电子邮件应该保存) 我的代码是: Download: icontact.api.functions.php <?php $iContactLists[1] = array('id' => '<list-id-number>', 'txt' => 'Weekly Newsletter'); $iCo

我现在有了一个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);
}


?>;
下载:icontact.api.functions.php

当我加载你的代码时,我没有得到一个空白页。我得到了一个“;”,因为这是最后一个结束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>
#假设有POST数据,我们应该处理注册
如果(计数($\邮政)){
$email=$_POST['email'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$list=$_POST['list'];
$results=addcontact($email、$fname、$lname、$list);
打印($results)#你应该在这里做一些更有用的事情
}
##生成并显示表单
$selectBox='';
foreach($IContactList作为$list){
$selectBox.=“{$list['txt']}\n”;
}
$selectBox.='';
?>
电子邮件:
名字:
姓氏:
列表:

以下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);
$headers=数组(
'接受:text/xml',
'内容类型:text/xml',
“Api版本:2.0”,
“Api应用程序id:”.$app_id,
“Api用户名:”。$user,
“Api密码:”。$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\u exec($ch);
$deleteParse=simplexml\u load\u string($deleteResult);
打印“”;
打印($deleteParse);
打印“”;
卷曲关闭($ch);

定义“根本不起作用”。拜托。错误?空白页?您的服务器日志显示了什么?它只返回一个空白页:(