mailchimp 使用 api 3.0 语法错误从数据库批量订阅
mailchimp batch subscribe from database usin api 3.0 syntax error
我需要使用 mailchimp 的 api 和我数据库中的订阅者列表来批量更新我的 mailchimp 列表中的订阅者。
我正在尝试在此处实施问题的解决方案:Mailchimp API 3.0 Batch Subscribe 但我收到错误 "Parse error: syntax error, unexpected '[' in /myfilepath/addsubscribersfromdatabase.php on line 18" 相关代码如下。谁能帮我弄清楚除了“[”之外我还应该使用什么?
<?php
$apikey = 'myapi'; // Your Mailchimp ApiKey
$list_id = 'mylistid'; // your List ID Where you want to add subscriber
$servername = 'myserver';
$username = 'myun';
$password = 'mypw';
$dbname = 'mydatabase';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
$sql = 'SELECT * FROM emails Limit 2';
$result = $conn->query($sql);
$finalData = [];
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$individulData = array(
'apikey' => $apikey,
'email_address' => $row['email'],
'status' => $row['status'],//subscribe,pending,unsubscribe
'merge_fields' => array(
'FNAME' => $row['FNAME'],
'LNAME' => $row['LNAME'],
'BARCODE' => $row['BARCODE'],
'SERVICE' => $row['SERVICE'],
'PURCHASE' => $row['PURCHASE'],
)
);
$json_individulData = json_encode($individulData);
$finalData['operations'][] =
array(
"method" => "PUT",
"path" => "/lists/$list_id/members/",
"body" => $json_individulData
);
}
}
$api_response = batchSubscribe($finalData, $apikey);
print_r($api_response);
$conn->close();
/**
* Mailchimp API- List Batch Subscribe added function
*
* @param array $data Passed you data as an array format.
* @param string $apikey your mailchimp api key.
*
* @return mixed
*/
function batchSubscribe(array $data, $apikey)
{
$auth = base64_encode('user:' . $apikey);
$json_postData = json_encode($data);
$ch = curl_init();
$dataCenter = substr($apikey, strpos($apikey, '-') + 1);
$curlopt_url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
curl_setopt($ch, CURLOPT_URL, $curlopt_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_postData);
$result = curl_exec($ch);
return $result;
}
?>
听起来您使用的 PHP 版本比为该答案编写的要旧。
该行的旧写法是
$finalData = array();
我需要使用 mailchimp 的 api 和我数据库中的订阅者列表来批量更新我的 mailchimp 列表中的订阅者。
我正在尝试在此处实施问题的解决方案:Mailchimp API 3.0 Batch Subscribe 但我收到错误 "Parse error: syntax error, unexpected '[' in /myfilepath/addsubscribersfromdatabase.php on line 18" 相关代码如下。谁能帮我弄清楚除了“[”之外我还应该使用什么?
<?php
$apikey = 'myapi'; // Your Mailchimp ApiKey
$list_id = 'mylistid'; // your List ID Where you want to add subscriber
$servername = 'myserver';
$username = 'myun';
$password = 'mypw';
$dbname = 'mydatabase';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
$sql = 'SELECT * FROM emails Limit 2';
$result = $conn->query($sql);
$finalData = [];
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$individulData = array(
'apikey' => $apikey,
'email_address' => $row['email'],
'status' => $row['status'],//subscribe,pending,unsubscribe
'merge_fields' => array(
'FNAME' => $row['FNAME'],
'LNAME' => $row['LNAME'],
'BARCODE' => $row['BARCODE'],
'SERVICE' => $row['SERVICE'],
'PURCHASE' => $row['PURCHASE'],
)
);
$json_individulData = json_encode($individulData);
$finalData['operations'][] =
array(
"method" => "PUT",
"path" => "/lists/$list_id/members/",
"body" => $json_individulData
);
}
}
$api_response = batchSubscribe($finalData, $apikey);
print_r($api_response);
$conn->close();
/**
* Mailchimp API- List Batch Subscribe added function
*
* @param array $data Passed you data as an array format.
* @param string $apikey your mailchimp api key.
*
* @return mixed
*/
function batchSubscribe(array $data, $apikey)
{
$auth = base64_encode('user:' . $apikey);
$json_postData = json_encode($data);
$ch = curl_init();
$dataCenter = substr($apikey, strpos($apikey, '-') + 1);
$curlopt_url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
curl_setopt($ch, CURLOPT_URL, $curlopt_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_postData);
$result = curl_exec($ch);
return $result;
}
?>
听起来您使用的 PHP 版本比为该答案编写的要旧。
该行的旧写法是
$finalData = array();