问题
I have this problem with Mailchimp that my call seems to be denied and fail. Somehow Mailchimp Support can not even see that I tried to call them with this code
$data = array(
'apikey' => "APIKEY-USXX",
'cid' => "CID",
);
$data = json_encode($data);
echo '<pre>';
var_dump($data);
echo '</pre>';
$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, urlencode($data));
$result = curl_exec($ch);
curl_close ($ch);
$info = json_decode(json_encode(json_decode($result)), true);
echo '<pre>';
var_dump($info);
echo '</pre>';
The strange thing is, this works just fine for any other call i make with API 1.3. I'm really lost in what iam doing wrong here. Hope you guys can help me?
Thanks in advance.
** EDIT ** Here is my response:
array(4) {
["status"]=>
string(5) "error"
["code"]=>
int(-100)
["name"]=>
string(15) "ValidationError"
["error"]=>
string(31) "You must specify a apikey value"
}
Final code that worked for me:
$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);
回答1:
Make sure the 'dc' in the submit URL (us10) matches the last portion of your API key.
Also... try adding the following options to the call:
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);
You can also add the following option to debug the call more explicitly
curl_setopt($ch, CURLOPT_VERBOSE, true);
来源:https://stackoverflow.com/questions/31625869/mailchimp-api-v2-validation-error