Php 如何使用常数接触API?

Php 如何使用常数接触API?,php,api,constantcontact,Php,Api,Constantcontact,我想使用API的不断联系,并希望插入用户电子邮件使用PHP,而用户注册到该网站 如果有任何帮助,请回复 提前感谢。//填写您的固定联系人登录名和API密钥 // fill in your Constant Contact login and API key $ccuser = 'USERNAME_HERE'; $ccpass = 'PASS_HERE'; $cckey = 'APIKEY_HERE'; // fill in these values $firstName = ""; $l

我想使用API的不断联系,并希望插入用户电子邮件使用PHP,而用户注册到该网站

如果有任何帮助,请回复

提前感谢。

//填写您的固定联系人登录名和API密钥
// fill in your Constant Contact login and API key
$ccuser = 'USERNAME_HERE';
$ccpass = 'PASS_HERE';
$cckey  = 'APIKEY_HERE';

// fill in these values 
$firstName  = "";
$lastName   = "";
$emailAddr  = "";
$zip        = "";

// represents the contact list identification number(s)
$contactListId = INTEGER_OR_ARRAY_OF_INTEGERS_HERE;
$contactListId = (!is_array($contactListId)) ? array($contactListId) : $contactListId;

$post = new SimpleXMLElement('<entry></entry>');
$post->addAttribute('xmlns', 'http://www.w3.org/2005/Atom');

$title = $post->addChild('title', "");
$title->addAttribute('type', 'text');

$post->addChild('updated', date('c'));
$post->addChild('author', "");
$post->addChild('id', 'data:,none');

$summary = $post->addChild('summary', 'Contact');
$summary->addAttribute('type', 'text');

$content = $post->addChild('content');
$content->addAttribute('type', 'application/vnd.ctct+xml');

$contact = $content->addChild('Contact');
$contact->addAttribute('xmlns', 'http://ws.constantcontact.com/ns/1.0/');

$contact->addChild('EmailAddress', $emailAddr);
$contact->addChild('FirstName', $firstName);
$contact->addChild('LastName', $lastName);
$contact->addChild('PostalCode', $zip);
$contact->addChild('OptInSource', 'ACTION_BY_CUSTOMER');

$contactlists = $contact->addChild('ContactLists');

// loop through each of the defined contact lists
foreach($contactListId AS $listId) {
    $contactlist = $contactlists->addChild('ContactList');
    $contactlist->addAttribute('id', 'http://api.constantcontact.com/ws/customers/' . $ccuser . '/lists/' . $listId);
}

$posturl = "https://api.constantcontact.com/ws/customers/{$ccuser}/contacts";
$authstr = $cckey . '%' . $ccuser . ':' . $ccpass;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response

$response = curl_exec($ch);

curl_close($ch);

// returns true on success, false on error
return (!is_numeric($response));
$ccuser='USERNAME_HERE'; $ccpass='PASS_HERE'; $cckey='APIKEY_HERE'; //填写这些值 $firstName=“”; $lastName=“”; $emailAddr=“”; $zip=“”; //表示联系人列表标识号 $contactListId=此处的整数或整数的数组; $contactListId=(!是数组($contactListId))?数组($contactListId):$contactListId; $post=新的SimpleXMLElement(“”); $post->addAttribute('xmlns','http://www.w3.org/2005/Atom'); $title=$post->addChild('title',“”); $title->addAttribute('type','text'); $post->addChild('updated',date('c')); $post->addChild('author',“”); $post->addChild('id','data:,none'); $summary=$post->addChild('summary','Contact'); $summary->addAttribute('type','text'); $content=$post->addChild('content'); $content->addAttribute('type','application/vnd.ctct+xml'); $contact=$content->addChild('contact'); $contact->addAttribute('xmlns','http://ws.constantcontact.com/ns/1.0/'); $contact->addChild('EmailAddress',$emailAddr); $contact->addChild($FirstName',$FirstName); $contact->addChild($LastName',$LastName); $contact->addChild('PostalCode',$zip); $contact->addChild('OptInSource','ACTION'BY'u CUSTOMER'); $contactlists=$contact->addChild('contactlists'); //循环浏览每个已定义的联系人列表 foreach($contactListId作为$listId){ $contactlist=$contactlist->addChild('contactlist'); $contactlist->addAttribute('id','http://api.constantcontact.com/ws/customers/“.$ccuser./lists/”.$listId); } $postrl=”https://api.constantcontact.com/ws/customers/{$ccuser}/contacts”; $authstr=$cckey.'%'$ccuser.“:”$ccpass; $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$postrl); curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC); curl_setopt($ch,CURLOPT_USERPWD,$authstr); 卷曲设置($ch,卷曲设置桩,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$post->asXML()); curl_setopt($ch,CURLOPT_HTTPHEADER,数组(“内容类型:application/atom+xml”); curl_setopt($ch,CURLOPT_头,false);//不返回标题 curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);//如果您将其设置为0,它将带您进入具有http响应的页面 $response=curl\u exec($ch); 卷曲关闭($ch); //成功时返回true,错误时返回false 返回(!is_numeric($response));
如果您无法实现此功能,您可能需要添加此curl\u setopt:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

ConstantContact的开发人员已经启动了PHP库来处理此类任务

以下版本适用于较早的PHP版本(5.2或更低版本)。从此处下载代码:

下面是一个在您的固定联系人列表中添加订户电子邮件的示例

session_start();
include_once('ConstantContact.php'); // Set path accordingly
$username = 'YOUR_CONSTANT_CONTACT_USER_NAME';
$apiKey = 'YOUR_API_KEY';
$password = 'YOUR_CONSTANT_CONTACT_PASSWORD';

$ConstantContact = new Constantcontact("basic", $apiKey, $username, $password);

$emailAddress = "new_email@test.com";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

// If the search did not return a contact object
if($search == false){

    $Contact = new Contact();
    $Contact->emailAddress = $emailAddress;
    //$Contact->firstName = $firstName;
    //$Contact->lastName = $lastName;
    // represents the contact list identification link(s)
    $contactList = LINK_OR_ARRAY_OF_LINKS_HERE;
    // For example, 
    // "http://api.constantcontact.com/ws/customers/USER_NAME/lists/14"; 
    $Contact->lists = (!is_array($contactList)) ? array($contactList) : $contactList;


    $NewContact = $ConstantContact->addContact($Contact);
    if($NewContact){
        echo "Contact Added. This is your newly created contact's information<br /><pre>";
        print_r($NewContact);
        echo "</pre>";

    }
} else {
    echo "Contact already exist.";
}
下载上述库后,使用以下代码在固定联系人列表中添加电子邮件

session_start();
包括_once('ConstantContact.php');//相应地设置路径
$username='YOUR_CONSTANT_CONTACT_USER_NAME';
$apiKey='YOUR_API_KEY';
$password='YOUR_CONSTANT_CONTACT_password';
$ConstantContact=新ConstantContact(“基本”、$apiKey、$username、$password);
$emailAddress=“新建_email@test.com";
//搜索我们的新电子邮件地址
$search=$ConstantContact->SearchContactsByMail($emailAddress);
//如果搜索未返回联系人对象
如果($search==false){
$Contact=新联系人();
$Contact->emailAddress=$emailAddress;
//$Contact->firstName=$firstName;
//$Contact->lastName=$lastName;
//表示联系人列表标识链接
$contactList=此处链接的链接或链接的数组;
//比如说,,
// "http://api.constantcontact.com/ws/customers/USER_NAME/lists/14"; 
$Contact->list=(!is_数组($contactList))?数组($contactList):$contactList;
$NewContact=$ConstantContact->addContact($Contact);
如果($NewContact){
echo“已添加联系人。这是您新创建的联系人信息
”; 打印(新联系人); 回声“; } }否则{ echo“联系人已存在。”; }
注意:最新版本可在以下URL中给出的GitHub链接上找到:
但我不确定上述示例代码是否适用于较新的库。

请澄清您的问题。我不知道你想做什么;)持续联系为时事通讯管理提供服务。我想在用户注册该站点时将其添加到常量联系人数据库中。表示他们在注册到网站时将自动添加到新闻稿组。谢谢你对我的问题感兴趣。