如何验证 PHP 中的 Mailchimp API 键?

How to validate Mailchimp API key in PHP?

如何验证 Mailchimp API 密钥是否有效或存在于 Mailchimp 中。我想将此密钥保存在数据库中,在此之前我需要验证给定的密钥。我看到这个 并关注它,但它对我不起作用。它什么也没显示。

这是我的代码:

validateApiKey('xxxxxxxxxxxxxxxxxxxxxxxxxx-us1');

function validateApiKey($api_key)
{
$data = array(
    'apikey' => $api_key,
    'cid' => "CID",
);
$data = json_encode($data);


$submit_url = "https://us10.api.mailchimp.com/2.0/reports/opened.json";

$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, $data);

curl_setopt($ch, CURLOPT_USERAGENT, 'MailChimp-PHP/2.0.6');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$result = curl_exec($ch);
curl_close ($ch);

$info = json_decode(json_encode(json_decode($result)), true);
print_r($info);
echo '<pre>';
var_dump($info);
echo '</pre>';

}

如何知道密钥是否有效。有什么建议吗?

提出请求。如果请求有效,则密钥有效。在 v2.0 中,您可以使用 /2.0/helper/ping 端点。在 3.0 中,根 (/3.0/) 是相似的。