【问题标题】:php sendgrid - Using PHP with cURL example - not workingphp sendgrid - 将 PHP 与 cURL 示例一起使用 - 不起作用
【发布时间】:2015-04-23 20:00:03
【问题描述】:

我正在尝试使用在 sendgrid 网站上发布的使用 PHP 的 cURL 函数查询 sendgrid API 的相同代码。

我使用的代码是:

<?php

$url = 'https://api.sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
// Tell PHP not to use SSLv3 (instead opting for TLS)
curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

?>

但每次我运行它时,我都会遇到以下错误:

注意:使用未定义的常量 CURL_SSLVERSION_TLSv1_2 - 假定为“CURL_SSLVERSION_TLSv1_2”

我从字面上搜索了谷歌,但找不到任何解决方案。请帮忙。

谢谢!

【问题讨论】:

    标签: php curl sendgrid


    【解决方案1】:

    我已经成功地让上面的代码像这样工作...... (我还应该注意到,由于邮件内容,它最终出现在我的垃圾邮件中!)

    $url = 'https://api.sendgrid.com/';
    $user = 'USERNAME';
    $pass = 'PASSWORD';
    
    $params = array(
        'api_user'  => $user,
        'api_key'   => $pass,
        'to'        => 'example3@sendgrid.com',
        'subject'   => 'testing from curl',
        'html'      => 'testing body',
        'text'      => 'testing body',
        'from'      => 'example@sendgrid.com',
      );
    
    $request =  $url.'api/mail.send.json';
    
    // Generate curl request
    $session = curl_init($request);
    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);
    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    // Tell PHP not to use SSLv3 (instead opting for TLS)
    //curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    
    //Turn off SSL
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);//New line
    curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);//New line
    
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    
    // obtain response
    $response = curl_exec($session);
    
    // print everything out
    var_dump($response,curl_error($session),curl_getinfo($session));
    
    curl_close($session);
    

    【讨论】:

      【解决方案2】:

      你运行的是什么 PHP 版本?

      来自http://php.net/manual/en/curl.constants.php

      CURL_SSLVERSION_TLSv1_2(整数)
      自 PHP 5.5.19 和 5.6.3 起可用

      您的服务器可能运行的版本低于此值,并且该常量未定义。

      【讨论】:

      • 我的 PHP 版本是 5.6.3。我需要升级/降级这个版本吗?
      猜你喜欢
      • 2011-12-02
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 2012-10-24
      • 2010-12-11
      相关资源
      最近更新 更多