资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

phpcurl数据丢失 php curl useragent

PHP CURL内存泄露的解决方法

PHP CURL内存泄露的解决方法

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了承留免费建站欢迎大家使用!

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上有,直接贴原文:

Without CURLOPT_HTTPPROXYTUNNEL

Without CURLOPT_HTTPPROXYTUNNEL : You just use the proxy address/port as a destination of your HTTP request. The proxy will read the HTTP headers of your query, forward your request to the destination (with your HTTP headers) and then write the response to you.

Example steps :

1)HTTP GET / sent to 1.1.1.1 (proxy)

2)1.1.1.1 receive request and parse header for getting the final destination of your HTTP request.

3)1.1.1.1 forward your query and headers to (destination in request headers).

4)1.1.1.1 write back to you the response receive from

With CURLOPT_HTTPPROXYTUNNEL

With CURLOPT_HTTPPROXYTUNNEL : You ask the proxy to open a direct binary connection (like HTTPS, called a TCP Tunnel) directly to your destination by doing a CONNECT HTTP request. When the tunnel is ok, the proxy write you back a HTTP/1.1 200 Connection established. When it received your browser start to query the destination directly : The proxy does not parse HTTP headers and theoretically does not read tunnel datas, it just forward it, thats why it is called a tunnel !

Example steps :

1)HTTP CONNECT sent to 1.1.1.1

2)1.1.1.1 receive HTTP CONNECT and get the ip/port of your final destination (header field of HTTP CONNECT).

3)1.1.1.1 open a TCP Socket by doing a TCP handshake to your destination 2.22.63.73:80 (ip/port of ).

4)1.1.1.1 Make a tunnel by piping your TCP Socket to the TCP Socket opened to 2.22.63.73:80and then write you back HTTP/1.1 200 Connection established witch means that your client can now make your query throw the TCP Tunnel (TCP datas received will be transmited directly to server and vice versa). ;

php获取数据为什么curl获取不完整

因为,PHP CURL库默认1024字节的长度不等待数据的返回,所以你那段代码需增加一项配置:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

给你一个更全面的封装方法:

function req_curl($url, $status = null, $options = array())

{

$res = '';

$options = array_merge(array(

'follow_local' = true,

'timeout' = 30,

'max_redirects' = 4,

'binary_transfer' = false,

'include_header' = false,

'no_body' = false,

'cookie_location' = dirname(__FILE__) . '/cookie',

'useragent' = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',

'post' = array() ,

'referer' = null,

'ssl_verifypeer' = 0,

'ssl_verifyhost' = 0,

'headers' = array(

'Expect:'

) ,

'auth_name' = '',

'auth_pass' = '',

'session' = false

) , $options);

$options['url'] = $url;

$s = curl_init();

if (!$s) return false;

curl_setopt($s, CURLOPT_URL, $options['url']);

curl_setopt($s, CURLOPT_HTTPHEADER, $options['headers']);

curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options['ssl_verifypeer']);

curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options['ssl_verifyhost']);

curl_setopt($s, CURLOPT_TIMEOUT, $options['timeout']);

curl_setopt($s, CURLOPT_MAXREDIRS, $options['max_redirects']);

curl_setopt($s, CURLOPT_RETURNTRANSFER, true);

curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options['follow_local']);

curl_setopt($s, CURLOPT_COOKIEJAR, $options['cookie_location']);

curl_setopt($s, CURLOPT_COOKIEFILE, $options['cookie_location']);

if (!empty($options['auth_name']) is_string($options['auth_name']))

{

curl_setopt($s, CURLOPT_USERPWD, $options['auth_name'] . ':' . $options['auth_pass']);

}

if (!empty($options['post']))

{

curl_setopt($s, CURLOPT_POST, true);

curl_setopt($s, CURLOPT_POSTFIELDS, $options['post']);

//curl_setopt($s, CURLOPT_POSTFIELDS, array('username' = 'aeon', 'password' = '111111'));

}

if ($options['include_header'])

{

curl_setopt($s, CURLOPT_HEADER, true);

}

if ($options['no_body'])

{

curl_setopt($s, CURLOPT_NOBODY, true);

}

if ($options['session'])

{

curl_setopt($s, CURLOPT_COOKIESESSION, true);

curl_setopt($s, CURLOPT_COOKIE, $options['session']);

}

curl_setopt($s, CURLOPT_USERAGENT, $options['useragent']);

curl_setopt($s, CURLOPT_REFERER, $options['referer']);

$res = curl_exec($s);

$status = curl_getinfo($s, CURLINFO_HTTP_CODE);

curl_close($s);

return $res;

}

php的curl模拟post发送数据,部分丢失

用urlencode编码试试,这样可以传递特殊字符并且防止一些奇怪的错误信息

为什么我用php的curl获取到的数据不完整,无法获取列表全部数据

你好,一般有俩原因:

①接口本身数据不完整;

②接口中数据量过大,可以调整一下服务器配置,PHP配置文件:memory_limit 每个PHP页面所吃掉的最大内存


分享文章:phpcurl数据丢失 php curl useragent
文章网址:http://cdkjz.cn/article/doihdoe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220