http 使用curl發(fā)起https請求_第1頁
http 使用curl發(fā)起https請求_第2頁
http 使用curl發(fā)起https請求_第3頁
http 使用curl發(fā)起https請求_第4頁
http 使用curl發(fā)起https請求_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

千里之行,始于足下。第2頁/共2頁精品文檔推薦http使用curl發(fā)起https請求http使用curl發(fā)起https請求

今天一具同事反映,使用curl發(fā)起https請求的時候報錯:“SSLcertificateproblem,verifythattheCAcertisOK.Details:error:14090086:SSL

routines:SSL3_GET_SERVER_CERTIFICATE:certificateverifyfailed”

非常明顯,驗證證書的時候浮現(xiàn)了咨詢題。

使用curl假如想發(fā)起的https請求正常的話有2種做法:

辦法一、設(shè)定為別驗證證書和host。

在執(zhí)行curl_exec()之前。設(shè)置option

$ch=curl_init();

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

辦法二、設(shè)定一具正確的證書。

本地ssl判不證書太舊,導(dǎo)致鏈接報錯ssl證書別正確。

我們需要下載新的ssl本地判不文件

http://curl.haxx.se/ca/cacert.pem

放到程序文件名目

curl增加下面的配置

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);;

curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');

大功告成

(本人驗證未經(jīng)過。。。報錯信息為:SSLcertificateproblem,verifythattheCAcertisOK.Details:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIFICATE:certificateverifyfailed)

假如對此感興趣的話能夠參看國外一大神文章。

/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

為了防止某天該文章被Q今復(fù)制過來。內(nèi)容如下:

UsingcURLinPHPtoaccessHTTPS(SSL/TLS)protectedsites

FromPHP,youcanaccesstheusefulcURLLibrary(libcurl)tomakerequeststoURLsusingavarietyofprotocolssuchasHTTP,FTP,LDAPandevenGopher.(Ifyou’vespenttimeonthe*nixcommandline,mostenviroXXXentsalsohavethecurlcommandavailablethatusesthelibcurllibrary)

Inpractice,however,themostcommonly-usedprotocoltendstobeHTTP,especiallywhenusingPHPforserver-to-servercommunication.Typicallythisinvolvesaccessinganotherwebserveraspartofawebservicecall,usingsomemethodsuchasXML-RPCorRESTtoqueryaresource.Forexample,DeliciousoffersaHTTP-basedAPItomanipulateandreadauser’sposts.However,whentryingtoaccessaHTTPSresource(suchasthedeliciousAPI),there’salittlemoreconfigurationyouhavetodobeforeyoucangetcURLworkingrightinPHP.

Theproblem

IfyousimplytrytoaccessaHTTPS(SSLorTLS-protectedresource)inPHPusingcURL,you’relikelytorunintosomedifficulty.Sayyouhavethefollowingcode:(Errorhandlingomittedforbrevity)

//InitializesessionandsetURL.

$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);

//Setsocurl_execreturnstheresultinsteadofoutputtingit.

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

//Gettheresponseandclosethechannel.

$response=curl_exec($ch);

curl_close($ch);

If$urlpointstowardanHTTPSresource,you’relikelytoencounteranerrorliketheonebelow:

Failed:ErrorNumber:60.Reason:SSLcertificateproblem,verifythattheCAcertisOK.Details:error:14090086:SSLroutines:SSL3_GET_SERVER_CERTIFICATE:certificateverifyfailed

TheproblemisthatcURLhasnotbeenconfiguredtotrusttheserver’sHTTPScertificate.TheconceptsofcertificatesandPKIrevolvesaroundthetrustofCertificateAuthorities(CAs),andbydefault,cURLissetuptonottrustanyCAs,thusitwon’ttrustanywebserver’scertificate.Sowhydon’tyouhaveproblemsvisitingHTTPssitesthroughyourwebbrowser?Asithappens,thebrowserdeveloperswereniceenoughtoincludealistofdefaultCAstotrust,coveringmostsituations,soaslongasthewebsiteoperatorpurchasedacertificatefromoneoftheseCAs.

Thequickfix

Therearetwowaystosolvethisproblem.Firstly,wecansimplyconfigurecURLtoacceptanyserver(peer)certificate.Thisisn’toptimalfromasecuritypointofview,butifyou’renotpassingsensitiveinformationbackandforth,thisisprobablyalright.Simplyaddthefollowinglinebeforecallingcurl_exec():

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

ThisbasicallycausescURLtoblindlyacceptanyservercertificate,withoutdoinganyverificationastowhichCAsignedit,andwhetherornotthatCAistrusted.Ifyou’reatallconcernedaboutthedatayou’repassingtoorreceivingfromtheserver,you’llwanttoenablethispeerverificationproperly.Doingsoisabitmorecomplicated.

Theproperfix

TheproperfixinvolvessettingtheCURLOPT_CAINFOparameter.ThisisusedtopointtowardsaCAcertificatethatcURLshouldtrust.Thus,anyserver/peercertificatesissuedbythisCAwillalsobetrusted.Inordertodothis,wefirstneedtogettheCAcertificate.Inthisexample,I’llbeusingthehttps://api.del.icio.us/serverasareference.

First,you’llneedtovisittheURLwithyourwebbrowserinordertograbtheCAcertificate.Then,(inFirefox)openupthesecuritydetailsforthesitebydouble-clickingonthepadlockiconinthelowerrightcorner:

Thenclickon“ViewCertificate”:

Bringupthe“Details”tabofthecerficatespage,andselectthecertificateatthetopofthehierarchy.ThisistheCAcertificate.

Thenclick“Export”,andsavetheCAcertificatetoyourselectedlocation,makingsuretoselecttheX.509Certificate(PEM)asthesavetype/format.

NowweneedtomodifythecURLsetuptousethisCAcertificate,withCURLOPT_CAINFOsettopointtowherewesavedtheCAcertificatefileto.

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);

curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);

curl_setopt($ch,CURLOPT_CAINFO,getXXXd().

"/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");

TheotheroptionI’veincluded,CURLOPT_SSL_VERIFYHOSTcanbesettothefollowingintegervalues:

0:Don’tcheckthecommonname(CN)attribute

1:Checkthatthecommonnameattributeatleastexists

2:CheckthatthecommonnameexistsandthatitmatchesthehostnameoftheserverIfyouhaveCURLOPT_SSL_VERIFYPEERsettofalse,thenfromasecurityperspective,itdoesn’treallymatterwhatyou’vesetCURLOPT_SSL_VERIFYHOSTto,sincewithoutpeer

certificateverification,theservercoulduseanycertificate,includingaself-signedonethatwasguaranteedtohaveaCNthatmatchedtheserver’shostname.Sothissettingisreallyonlyrelevantifyou’veenabledcertificateverification.

ThisensuresthatnotjustanyservercertificatewillbetrustedbyyourcURLsession.Forexample,ifanattackerweretosomehowredirecttrafficfromtotheirownserver,thecURLsessionherewouldnotproperlyinitialize,sincetheattackerwouldnothaveaccesstoaservercertificate(i.e.wouldnothavetheprivatekey)trustedbytheCAweadded.ThesestepseffectivelyexportthetrustedCAfromthewebbrowsertothecURLconfiguration.

Moreinformation

IfyouhavetheCAce

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論