为什么我的 PHP cURL 无法在 SendGrid 上运行?

问题描述 投票:0回答:1

我正在尝试将 API 与我的网站集成。我尝试了三种不同的 API 密钥。我总是收到错误:

“提供的授权无效、已过期或已撤销”

希望有人能指出我正确的方向。

这是我的代码:

<?php
         
   $url = 'https://api.sendgrid.com/v3/mail/send';
   $headers = array(
        'Content-Type: application/json;charset=UTF-8',
        'Accept: application/json',
        'Authorization: Bearer          {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}'
   );                                                                                        
   
   $email = array
   (
      'to'        => "[email protected]",
      'toname'    => "Example User",
      'from'      => "[email protected]",
      'fromname'  => "Your Name",
      'subject'   => "PHP Test",
      'text'      => "I'm text!",
      'html'      => "<strong>I'm HTML!</strong>",
  );
         
   $curl = curl_init($url);

   curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($email));
   curl_setopt($curl, CURLOPT_TIMEOUT, 20);
   curl_setopt($curl, CURLOPT_HEADER, false);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
   curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            
   $response = curl_exec($curl);
   curl_close($curl);
   $result = json_decode(preg_replace('/^'.pack('H*','EFBBBF').'/', '', $response), true);
   var_dump($result);

?>                          
php curl sendgrid
1个回答
0
投票

总结此处评论的结果:

该问题是由实际令牌周围的大括号引起的。

但是,也值得一提的是 SendGrid 有一个 API 库,可以用来代替 cURL。

© www.soinside.com 2019 - 2024. All rights reserved.