欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-28 20:07:52
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... ......
 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 }