php curl请求 (get请求,post请求,代理ip设置)
2018-11-03 08:51:15来源:博客园 阅读 ()
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写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash