【问题标题】:"Unsupported SSL protocol version" when trying to call SendGrid API尝试调用 SendGrid API 时出现“不支持的 SSL 协议版本”
【发布时间】:2016-05-11 16:20:31
【问题描述】:

大家好,请看下面的代码并帮助我找出问题..

 $tos='soanm2@gmail.com';
$subject09='hello madam223';
 $messageto90='this is a test';
 $myName_emailis='Sonam verma';
  $emaillist_act=array('sonam@gmail.com','sakshi@gmail.com');
  $json_string = array( 'to' =>$emaillist_act,'category' =>   'activity');

  $url = 'https://api.sendgrid.com/';
 $user = 'username';
 $pass = 'password';
 $params = array(
'api_user'  => $user,
'api_key'   => $pass,
'x-smtpapi' => json_encode($json_string),
'to'        => "$tos",
'subject'   => "$subject09",
'html'      => "$messageto90",
'fromname' => $myName_emailis,
'from'      => "domain.com <contact@doamian.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_r($response);
  ?>

此代码适用于其他服务器http://www.reurl.in/6626fdd53 但它不会在我的服务器上做任何事情http://www.reurl.in/26cbacc87 它会打印应该打印响应成功或失败等的任何内容。

此代码在两个服务器上都运行良好。

   <?php
   //Your authentication key
   $authKey = "somethingapikey";

    //Multiple mobiles numbers separated by comma
    $mobileNumber = "$mobileno";

    //Sender ID,While using route4 sender id should be 6 characters long.
     $senderId = "something";

    //Your message to send, Add URL encoding here.

     if(strlen($textsms) > 300){
     $message = urlencode(substr($textsms,0,300));
      $message=$message.'...'. ' www.domain.com';    
      }else{
       $textsms=$textsms.' www.domain.com';    
        $message = urlencode($textsms);    
         }


   //Define route 
   $route = "domain";
  //Prepare you post parameters
  $postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
 );
  //API URL
  $url="https://control.msg91.com/api/sendhttp.php";
  // init the resource
  $ch = curl_init();
  curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
 ));

  //Ignore SSL certificate verification
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    //get response
    $output = curl_exec($ch);
    //Print error if any
    //if(curl_errno($ch))
      //{
    //    echo 'error:' . curl_error($ch);
    //}
  curl_close($ch);
   //echo $output;
    ?>

请帮我缩小问题范围,因为这对我来说非常重要... 我的服务器管理员说他们无能为力。

我还缩小了我们有 curl 版本 7.38.0 而其他服务器有 7.19.7 的问题

所有错误报告都已开启,但我仍然没有收到任何信息没有错误没有成功没有邮件。

  ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);

添加后

$error = curl_error($session);
 echo $error;

我明白了

Unsupported SSL protocol version

【问题讨论】:

  • 它已经在我使用了 ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
  • 请让我检查一下..
  • 如何检查我应该在参数中输入什么?
  • 是的,它说不支持的 SSL 协议版本

标签: php ssl curl sendgrid


【解决方案1】:

如需快速修复,请添加curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

$tos='soanm2@gmail.com';
$subject09='hello madam223';
 $messageto90='this is a test';
 $myName_emailis='Sonam verma';
  $emaillist_act=array('sonam@gmail.com','sakshi@gmail.com');
  $json_string = array( 'to' =>$emaillist_act,'category' =>   'activity');

  $url = 'https://api.sendgrid.com/';
 $user = 'username';
 $pass = 'password';
 $params = array(
'api_user'  => $user,
'api_key'   => $pass,
'x-smtpapi' => json_encode($json_string),
'to'        => "$tos",
'subject'   => "$subject09",
'html'      => "$messageto90",
'fromname' => $myName_emailis,
'from'      => "domain.com <contact@doamian.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);
 curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 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_r($response);
  ?>

警告:将 CURLOPT_SSL_VERIFYPEER 设置为 false 允许中间人攻击。

另一种解决方案

如果您的 PHP 安装没有最新的 CA 根证书捆绑包,请在 curl 网站上下载该捆绑包并将其保存在您的服务器上:

http://curl.haxx.se/docs/caextract.html

然后在您的 php.ini 文件中设置它的路径,例如在 Windows 上:

curl.cainfo=c:\php\cacert.pem

关闭CURLOPT_SSL_VERIFYPEER 允许中间人 (MITM) 攻击,这是您不想要的!

【讨论】:

    【解决方案2】:

    尝试使用不同的 SSL 协议版本。

    换行:

    @curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    

    尝试这些可能的常量,看看哪一个有效:

    CURL_SSLVERSION_TLSv1
    CURL_SSLVERSION_SSLv2
    CURL_SSLVERSION_SSLv3
    CURL_SSLVERSION_TLSv1_0
    CURL_SSLVERSION_TLSv1_1
    CURL_SSLVERSION_TLSv1_2
    

    另外:删除行首的@。 @suppresses 错误。您确实希望在发生任何错误时得到错误。

    如果两者都不起作用,则服务器上可能未安装对所需 ssl 协议版本的支持。然后,您需要更新服务器上使用的 CURL 或 OPENSSL。

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2016-05-21
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多