我正在使用 ElasticEmail API 从我的 php 网站发送电子邮件。我确信我复制粘贴了正确的 API key 。我的帐户也处于事件状态。这是我的代码:
$postStr = array(
'apikey' => 'MY-KEY',
'from' => $fromEmail,
'fromName' => $fromEmail,
'subject' => '[Bug Report]',
'to' => $notifyEmail,
'bodyHtml' => $bugDetails,
'isTransactional' => true);
var_dump($postStr);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false
));
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
vardump 内容:
array(7) { ["apikey"]=> string(36) "----my apikey here-----" ["from"]=> string(20) "[email protected]" ["fromName"]=> string(20) "[email protected]" ["subject"]=> string(12) "[Bug Report]" ["to"]=> string(23) "[email protected]" ["bodyHtml"]=> string(4) "Test" ["isTransactional"]=> bool(true) }
结果是:
{"success":false,"error":"Incorrect apikey"}
我不知道我哪里做错了。
编辑:我尝试安装 Postman。并测试它似乎有效。我猜我的数组构建有问题?
最佳答案
好的,我找到了答案。看来我的猜测是正确的,我构建postfields的方式是不正确的。正确的方法是在将数组传递给 CURLOPT_POSTFIELDS 之前对数组使用 http_build_query。
然后就可以正常工作了。
代码:
$postSafe = http_build_query($postStr);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postSafe,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false
));
关于php - Elasticemail 不断收到错误的 apikey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698188/