我正在使用MailChimp 2.0 api并尝试使用php发布列表/订阅调用。该调用返回错误"您必须指定一个apikey值"。
以下是发布帖子的代码:
function json_post ($url, $params)
{
print '<p>url = ' . $url . '</p>';
$data = json_encode ($params);
print '<p>data = ' . $data . '</p>';
$handle = curl_init ($url);
curl_setopt ($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ($handle, CURLOPT_POST_FIELDS, $data);
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($handle, CURLOPT_HTTPHEADER, array ('Content-Type: application/json',
'Content-Length: ' . strlen($data_string)));
$result = curl_exec ($handle);
print '<p>curl_error: ' . curl_errno ($handle) . '</p>';
return $result;
}
印刷陈述显示:
url = https://us10.api.mailchimp.com/2.0/lists/subscribe.json
data = {"apikey":"...","id":"...","email":{"email":"test1@abc.com"},"merge_vars":{"groupings":{"name":"test"}}}
curl_error: 0
{"status":"error","code":-100,"name":"ValidationError","error":"You must specify a apikey value"}
我认为语法有问题。 api键被切断&amp;从我的mailchimp帐户页面粘贴。我已尝试使用和不使用-us10后缀。有什么想法吗?
答案 0 :(得分:1)
订阅:
$email='';
$apikey='';
$listId='';
$data = array(
'email_address'=>$email,
'apikey'=>$apikey,
'merge_vars' => array(),
'id' => $listId,
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false,
'send_welcome' => false,
'email_type' => 'html'
);
$submit_url = "http://us6.api.mailchimp.com/1.3/?method=listSubscribe";
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if (isset($data->error) and $data->error){
//Error
} else {
//Ok
}
答案 1 :(得分:1)
您获得&#34; API Key Missing&#34;但API Key肯定会出现JSON语法错误,而MailChimp并没有特别注意这些错误。您将要确保JSON不会被双重编码或类似的任何内容。
在这种情况下,可能是CURLOPT_POST_FIELDS
- 您正在寻找的实际PHP Constant是CURLOPT_POSTFIELDS
。
你应该使用Guzzle或其他HTTP库来确保你不会对你的JSON进行双重编码,否则会被Curl库的详细程度绊倒。