PHP CURL 内存泄露问题解决方法
phpcurl使用privoxy代理访问https://www.google.com/search?q=xxx
curl配置平淡无奇,长时间运行发现一个严重问题,内存泄露!不论用单线程和多线程都无法避免!是curl访问https站点的时候有bug!
内存泄露可以通过linux的top命令发现,使用php函数memory_get_usage()不会发现。
经过反复调试找到解决办法,curl配置添加如下几项解决问题:
[CURLOPT_HTTPPROXYTUNNEL]=true; [CURLOPT_SSL_VERIFYPEER]=false; [CURLOPT_SSL_VERIFYHOST]=false;
CURLOPT_HTTPPROXYTUNNEL具体说明stackoverflow上有,直接贴原文:
WithoutCURLOPT_HTTPPROXYTUNNEL
WithoutCURLOPT_HTTPPROXYTUNNEL:Youjustusetheproxyaddress/portasadestinationofyourHTTPrequest.TheproxywillreadtheHTTPheadersofyourquery,forwardyourrequesttothedestination(withyourHTTPheaders)andthenwritetheresponsetoyou.
Examplesteps:
1)HTTPGET/index.htmlsentto1.1.1.1(proxy)
2)1.1.1.1receiverequestandparseheaderforgettingthefinaldestinationofyourHTTPrequest.
3)1.1.1.1forwardyourqueryandheaderstowww.site.com(destinationinrequestheaders).
4)1.1.1.1writebacktoyoutheresponsereceivefromwww.site.com
WithCURLOPT_HTTPPROXYTUNNEL
WithCURLOPT_HTTPPROXYTUNNEL:Youasktheproxytoopenadirectbinaryconnection(likeHTTPS,calledaTCPTunnel)directlytoyourdestinationbydoingaCONNECTHTTPrequest.Whenthetunnelisok,theproxywriteyoubackaHTTP/1.1200Connectionestablished.Whenitreceivedyourbrowserstarttoquerythedestinationdirectly:TheproxydoesnotparseHTTPheadersandtheoreticallydoesnotreadtunneldatas,itjustforwardit,thatswhyitiscalledatunnel!
Examplesteps:
1)HTTPCONNECTsentto1.1.1.1
2)1.1.1.1receiveHTTPCONNECTandgettheip/portofyourfinaldestination(headerfieldofHTTPCONNECT).
3)1.1.1.1openaTCPSocketbydoingaTCPhandshaketoyourdestination2.22.63.73:80(ip/portofwww.site.com).
4)1.1.1.1MakeatunnelbypipingyourTCPSockettotheTCPSocketopenedto2.22.63.73:80andthenwriteyoubackHTTP/1.1200ConnectionestablishedwitchmeansthatyourclientcannowmakeyourquerythrowtheTCPTunnel(TCPdatasreceivedwillbetransmiteddirectlytoserverandviceversa).
http://stackoverflow.com/questions/12288956/what-is-the-curl-option-curlopt-httpproxytunnel-means