2016-05-11 71 views
2

大家好,請看看下面的代碼,幫我找出這個問題..「不支持SSL協議版本」當試圖調用API SendGrid

$tos='[email protected]'; 
$subject09='hello madam223'; 
$messageto90='this is a test'; 
$myName_emailis='Sonam verma'; 
    $emaillist_act=array('[email protected]','[email protected]'); 
    $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 <[email protected]>" 
); 
$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 它dosent打印它應該打印響應成功或失敗等。

此代碼在兩個服務器上都很好。

<?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; 
    ?> 

請幫我縮小的問題,因爲這對我來說很重要,請... 我的服務器管理員說,他們不能幫助。

我也縮小了問題,我們有捲曲版本7.38.0和其他服務器有7.19.7

所有錯誤reportings上,但仍然我沒有得到任何東西沒有錯誤沒有成功沒有郵件。

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 
+0

它已經在我用ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); –

+0

讓我檢查一下,請。 –

+0

如何檢查我應該在參數中輸入什麼? –

回答

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。

3

爲了速戰速決,添加curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

$tos='[email protected]'; 
$subject09='hello madam223'; 
$messageto90='this is a test'; 
$myName_emailis='Sonam verma'; 
    $emaillist_act=array('[email protected]','[email protected]'); 
    $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 <[email protected]>" 
); 
$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根證書捆綁,下載一個在捲曲的網站,並將其保存在服務器上:

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

然後在你的php.ini文件中設置一個路徑,例如在Windows上:

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

關閉CURLOPT_SSL_VERIFYPEER允許中間人(MITM)攻擊,你不想要的!