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

  时间:2018-11-03
本文章向大家介绍php curl请求 (get请求,post请求,代理ip设置),需要的朋友可以参考一下
 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 }

 

原文地址:http://www.manongjc.com/article/17159.html