2011-10-21 23 views
0

我现在有一个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是行不通的改变变量,但仍然...

+0

定义 “并没有在所有的工作”。来吧。错误?空白页?你的服务器日志说什么? – ceejayoz

+0

它只返回空白页:( – user952691

回答

1

当我加载你的代码,我没有得到一个空白页。我得到了一个“;”,因为这是最后关闭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> 
1

下面的示例代码将帮助您删除特定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); 
相关问题