php curl请求 (get请求,post请求,代理ip设置)

2018-11-03 08:51:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

 1 /**
 2  * get  curlOpen('www.baidu.com?act=2')
 3  *  post curlOpen('www.baidu.com',array('post'=>['name'=>'aa','age'=>1],'ssl'=>true))
 4  *  $config['proxy']='192.168.1.1' 代理ip
 5  *  $config['header']['ip'] = '255.255.255.1' 
 6  *  $config['cookie']
 7  *  $config['referer'] 
 8  */
 9 function http_curl($url, $config = array())
10 {
11     $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'connectout'=>3, 'timeout' => 10, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false,'returnheader'=>false);
12     $arr = array_merge($arr, $config);
13     $ch = curl_init();
14     
15     curl_setopt($ch, CURLOPT_URL, $url);
16     curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
17     curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);  
18     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
19     curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
20     curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
21     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $arr['connectout']);
22     curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
23     curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);
24     if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
25     if($arr['ssl'])
26     {
27         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
28         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
29     }
30     if(!empty($arr['cookie']))
31     {
32         if(substr($arr['cookie'], -4) == '.txt'){
33             curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
34             curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
35         }else{
36             curl_setopt($ch, CURLOPT_COOKIE, $arr['cookie']);
37         }
38 
39     }
40     if(!empty($arr['proxy']))
41     {
42         curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
43         if(!empty($arr['userpwd']))
44         {            
45             curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
46         }        
47     }    
48 
49     if(!empty($arr['header']['ip']))
50     {
51         array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
52         unset($arr['header']['ip']);
53     }   
54     $arr['header'] = array_filter($arr['header']);
55     
56     if(!empty($arr['header']))
57     {
58         curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']); 
59     }
60 
61     if ($arr['post'] != false)
62     {
63         curl_setopt($ch, CURLOPT_POST, true);
64         if(is_array($arr['post']) && $arr['isupfile'] === false)
65         {
66             $post = http_build_query($arr['post']);            
67         } 
68         else
69         {
70             $post = $arr['post'];
71         }
72         curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
73     }    
74     $result = curl_exec($ch);
75     curl_close($ch);
76 
77     return $result;
78 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:常用命令大全总结中

下一篇:php使用curl模拟多线程发送请求