php - Elasticemail 不断收到错误的 apikey

标签 php post

我正在使用 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/

相关文章:

c# - 在 ASP MVC 中获取变量数量未知的发布数据

php - 如何连接两个表并在php中返回结果

php - Arduino 和网络服务器之间的连接监控

android facebook api 发布

jquery - 如何使用getJSON,用post方式发送数据?

php - 如何获取 Doctrine 生成BIGINT类型的sql?

php - 如何处理不同类型的用户?

php - Nginx 允许访问特定位置,并为所有其他返回 444

PHPUnit:在 <ClassNameTest>.php 中找不到类 <ClassName>

ajax - GET 与 AJAX 中的 POST?