php - 尝试调用 SendGrid API 时出现 "Unsupported SSL protocol version"

标签 php ssl curl sendgrid

大家好,请看一下下面的代码并帮助我解决问题..

 $tos='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c0dcd2ddde81f3d4ded2dadf9dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>';
$subject09='hello madam223';
 $messageto90='this is a test';
 $myName_emailis='Sonam verma';
  $emaillist_act=array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f6eaebe4e8c5e2e8e4ece9abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>','<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96e5f7fde5feffd6f1fbf7fffab8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>');
  $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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99a96978d989a8db99d969894909897d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>>"
 );
 $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

最佳答案

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

$tos='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36455957585b0476515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>';
$subject09='hello madam223';
 $messageto90='this is a test';
 $myName_emailis='Sonam verma';
  $emaillist_act=array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0d11101f133e19131f1712501d1113" rel="noreferrer noopener nofollow">[email protected]</a>','<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2655474d554e4f66414b474f4a0845494b" rel="noreferrer noopener nofollow">[email protected]</a>');
  $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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a1adacb6a3a1b682a6ada3afaba3aceca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>>"
 );
 $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);
  ?>

Warning: Setting CURLOPT_SSL_VERIFYPEER to false allows for man-in-the-middle-attacks.

另一种解决方案

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

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

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

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

关闭CURLOPT_SSL_VERIFYPEER会导致中间人 (MITM) 攻击,这是您不希望看到的!

关于php - 尝试调用 SendGrid API 时出现 "Unsupported SSL protocol version",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37168231/

相关文章:

http - GET请求以获取最新事件,在Elasticsearch中始终返回相同的结果

php - over() 子句出错 am 根据 id 计算 mysql 中下一个或上一个值的行

php - 如何将 3 个数组插入到 2 个 MySql 表中?

java - 允许非 ssl 连接 : javax.net.ssl.SSLHandshakeException: 握手失败

python - SSL 错误 - 证书验证失败 - AWS IOT (basicPubSub.py)

php - Home Brew PHP 7.2.5 使用 cURL 安装

javascript - 使用jquery将本地mp3文件发送到api服务器

php - 带有 json 数组的 jquery-autocomplete

php - 警告 : A non-numeric value encountered

ssl - AWS CloudFront 托管 SSL 并转发到 sendgrid 不工作