php MailChimp列表订阅“put”请求
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php MailChimp列表订阅“put”请求相关的知识,希望对你有一定的参考价值。
<?php
/**
* Utility function to subscribe a user to a MailChimp
* list using the MC API 3.0
*
* @param array $args {
* Array of settings.
*
* @type string $api_key Your MailChimp API key.
* @type string $list_id Your MailChimp List ID.
* @type string $email The email you are subscribing to the list.
* @type string $status The status you are setting the user as (subscribed|unsubscribed|cleaned|pending).
* @type array $merge_fields {
* Merge field values.
*
* @type string $FNAME MC default for user first name.
* @type string $LNAME MC default for user last name.
* }
* }
*/
function sync_to_mailchimp( $args ) {
$member_id = md5( strtolower( $args['email'] ) );
$data_center = substr( $args['api_key'], strpos( $args['api_key'], '-' ) +1);
$url = 'https://' . $data_center . '.api.mailchimp.com/3.0/lists/' . $args['list_id'] . '/members/' . $member_id;
$json = json_encode([
'email_address' => $args['email'],
'status' => $args['status'], // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => $args['merge_fields']
]);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_USERPWD, 'user:' . $args['api_key'] );
curl_setopt( $ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json'] );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json );
$result = curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return $http_code;
}
<?php
// Example of how to use the sync_to_mailchimp() function.
$args = array(
'api_key' => 'your api key',
'list_id' => 'your list id',
'email' => 'johndoe@example.com',
'status' => 'subscribed', // "subscribed","unsubscribed","cleaned","pending"
'merge_fields' => array(
'FNAME' => 'first_name',
'LNAME' => 'last_name',
)
);
$result = sync_to_mailchimp( $args );
以上是关于php MailChimp列表订阅“put”请求的主要内容,如果未能解决你的问题,请参考以下文章
我需要通过组件帖子向 mailchimp 订阅列表发出 http 请求
使用 PHP 检查邮件是不是订阅 MailChimp API 3.0 中的列表
发生一个或多个错误。(发送请求时发生错误)Mailchimp 列表集成
向 MailChimp 添加新订阅者返回错误请求
Mailchimp 错误收件人最近的注册请求太多
MailChimp PHP 脚本来拉取列表