我使用Mailchimp API遇到了一个奇怪的问题,我只是试图根据他们的OAuth文档交换我从他们那里获得的访问令牌的短期代码{{ 3}}
然而,我收到的回复似乎是一些编码问题。这就是我在PHP中所拥有的:(即使不使用正确的client_id,client_secret等也可以复制它。)
<?php
//### INITIALISE CURL #############################
$curl = curl_init();
//### SET CURL OPTIONS ############################
curl_setopt($curl, CURLOPT_URL,"https://login.mailchimp.com/oauth2/token" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=authorization_code&client_id=XXXXXXXXXXXX&client_secret=XXXXXXXXXXX&code=XXXXXXX&redirect_uri=https://mailchimp.test.com" );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: multipart/form-data"));
//### EXECUTE CURL ###############################
$result = curl_exec($curl);
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) =='0') {
echo curl_error($curl);
}
curl_close($curl);
echo $result;
?>
我收到此回复($ result):
�M�� �0F�UJ�N�NQ� %`��' "�Ћ���nb��f=�&9����i'f��]�~j*$��W��Reg��_T1-���;�oc
我如何解决此问题,以便根据Mailchimp的文档上的示例响应,我得到的响应是正确的? (下同)
响应示例:
{"access_token":"5c6ccc561059aa386da9d112215bae55","expires_in":0,"scope":null}
答案 0 :(得分:0)
如果您将以下代码添加到代码中,您将得到正确的答案:
curl_setopt($curl, CURLOPT_ENCODING, '');
例如
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_ENCODING, '');
在此处找到:
https://github.com/drewm/mailchimp-api/pull/69/files
参见这篇文章也可以解决问题
https://kb.mailchimp.com/campaigns/design/i-have-strange-characters-in-my-content