2012年2月7日 星期二

AWS PHP SDK:cURL error: SSL certificate problem解決方法

0 Comments
日期:2012-02-07
AWS SDK for PHP 下載:http://aws.amazon.com/sdkforphp/
AWS SDK教學影片:http://aws.amazon.com/articles/SDKs/PHP/4262


參考「AWS SDK教學影片」寫了一個下列的程式去測試AWS SDK

<?php
error_reporting(-1);
header("Content-type: text/plain; charset=utf-8");
require_once "aws-sdk-for-php/sdk.class.php";
$s3 = new AmazonS3();
$response = $s3->list_buckets();
print_r($response);
?>


但在測試時,一直遇到下列的錯誤訊息
Fatal error: Uncaught exception 'cURL_Exception' with message 'cURL resource: Resource id #10; cURL error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed (cURL error code 60). See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of error codes.'


當初以為是「aws-sdk-for-php/config.inc.php」檔案放錯地方,裡面的key跟secret沒有被讀取到而導致這樣的錯誤發生
但是檢查「aws-sdk-for-php/utilities/credentials.class.php」檔案,在set function(line 57)中的$credential_set也有正確的把key跟secret傳遞過來,所以初步排除是Key的錯誤
最後將「aws-sdk-for-php/lib/requestcore/requestcore.class.php」檔案中if ($this->ssl_verification)(line 622)判斷式下方的驗證都改為false(如下),將驗證關閉。

解法1:
<?php
// =========== 前略 ===========
// Verification of the SSL cert
if ($this->ssl_verification)
{
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
}
// =========== 後略 ===========
?>


還有另外的解法,在新增AWS物件後,加入「$s3->ssl_verification=false;」程式碼,把SSL驗證關閉

解法2:
<?php
error_reporting(-1);
header("Content-type: text/plain; charset=utf-8");
require_once "aws-sdk-for-php/sdk.class.php";
$s3 = new AmazonS3();
//********** 多加這一行 **********
$s3->ssl_verification=false;
//********** 多加這一行 **********
$response = $s3->list_buckets();
print_r($response);
?>


最後就可以正常的使用AWS SDK了

ADS