只是为了跟进 Volomike 的回答,这应该在这里得到所有的赞誉,所以要支持他,这是我的扩展代码,涵盖了冲突错误。这是一种程序方法。随意写一个更好的。
请记住,我必须将其作为危机的最后一分钟热修复来处理。我不知道我在这里放了什么可能是不必要的或者可以写得更好。我只需要快速将其捣碎即可使其正常工作。如果有更完整,更清洁的方法,请随时将其写为答案并对此答案发表评论,以便我加你。
//REPLACE WITH YOUR OWN 部分 - 请参阅 Volomike 对整个开发帐户流程的回答。它非常快,所以不要让你气馁。只需注册,获取您的令牌和密钥并将其插入即可。
error_reporting(E_ALL);
ini_set('display_errors','On');
header('Content-Type: text/plain');
// REPLACE WITH YOUR OWN
$sAPIKey = 'your-api-key';
$sToken = 'your-token';
$sListID = 11111111; //your list id
$email = 'email-to-be-added@gmail.com';
// END REPLACE BLOCK
$sURL = "https://api.constantcontact.com/v2/contacts?action_by=" .
"ACTION_BY_OWNER&api_key=$sAPIKey";
//DONT INDENT THIS. remember heredoc needs it to be rammed right up against the wall
$sJSON = <<<EOD
{
"lists": [
{
"id": "$sListID"
}
],
"email_addresses": [
{
"email_address": "$email"
}
]
}
EOD;
$sResponse = file_get_contents($sURL,false,stream_context_create(array(
'http'=>array(
'method' => 'POST',
'header' => "Authorization: Bearer $sToken\nContent-Type: application/json",
'content' => $sJSON,
'ignore_errors' => TRUE
),
'ssl'=>array(
'verify_peer'=>false,
'verify_peer_name'=>false,
'allow_self_signed'=>true,
)
)));
if(strpos($sResponse, 'http.status.email_address.conflict') > -1)
{
$sResponse = file_get_contents("https://api.constantcontact.com/v2/contacts?email=$email&status=ALL&limit=1&api_key=$sAPIKey",false,stream_context_create(array(
'http'=>array(
'header' => "Authorization: Bearer $sToken",
'ignore_errors' => TRUE
),
'ssl'=>array(
'verify_peer'=>false,
'verify_peer_name'=>false,
'allow_self_signed'=>true,
)
)));
$res = json_decode($sResponse);
$id = $res->results[0]->id;
$res->results[0]->lists[] = (object) array('id' => ''.$sListID);
$sResponse = file_get_contents("https://api.constantcontact.com/v2/contacts/$id?action_by=ACTION_BY_OWNER&api_key=$sAPIKey",false,stream_context_create(array(
'http'=>array(
'method' => 'PUT',
'header' => "Authorization: Bearer $sToken\nContent-Type: application/json",
'content' => json_encode($res->results[0]),
'ignore_errors' => TRUE
),
'ssl'=>array(
'verify_peer'=>false,
'verify_peer_name'=>false,
'allow_self_signed'=>true,
)
)));
echo $sResponse;
}
else
{
// didnt have a conflict
echo $sResponse;
}
//there was a problem of some kind if it reaches here