无为清净楼资源网 Design By www.qnjia.com
cURL
cURL可以使用URL的语法模拟浏览器来传输数据,因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP等协议都可以很好的支持,包括一些:HTTPS认证,HTTP POST方法,HTTP PUT方法,FTP上传,keyberos认证,HTTP上传,代理服务器,cookies,用户名/密码认证,下载文件断点续传,上传文件断点续传,http代理服务器管道,甚至它还支持IPv6,scoket5代理服务器,通过http代理服务器上传文件到FTP服务器等等。
本文主要介绍的是php远程请求CURL(爬虫、保存登录状态)的相关内容,下面话不多说了,来一起看看详细的介绍吧
GET案例
/** * curl_get * @param $url * @param null $param * @param null $options * @return array */ function curl_get($url,$param = null,$options = null){ if(empty($options)){ $options = array( 'timeout' => 30,// 请求超时 'header' => array(), 'cookie' => '',// cookie字符串,浏览器直接复制即可 'cookie_file' => '',// 文件路径,并要有读写权限的 'ssl' => 0,// 是否检查https协议 'referer' => null ); }else{ empty($options['timeout']) && $options['timeout'] = 30; empty($options['ssl']) && $options['ssl'] = 0; } $result = array( 'code' => 0, 'msg' => 'success', 'body' => '' ); if(is_array($param)){ $param = http_build_query($param); } $url = strstr($url,'"CURL Error:".curl_error($ch); } $result['body'] = $output; //释放curl句柄 curl_close($ch); return $result; }
POST案例
/** * curl_post * @param $url 请求地址 * @param null $param get参数 * @param array $options 配置参数 * @return array */ function curl_post($url,$param = null,$options = array()){ if(empty($options)){ $options = array( 'timeout' => 30, 'header' => array(), 'cookie' => '', 'cookie_file' => '', 'ssl' => 0, 'referer' => null ); }else{ empty($options['timeout']) && $options['timeout'] = 30; empty($options['ssl']) && $options['ssl'] = 0; } $result = array( 'code' => 0, 'msg' => 'success', 'body' => '' ); if(is_array($param)){ $param = http_build_query($param); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);// 设置url !empty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){ curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); }else if(!empty($options['cookie'])){ curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); } curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面 !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl !empty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗 curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); //执行并获取内容 $output = curl_exec($ch); //对获取到的内容进行操作 if($output === FALSE ){ $result['code'] = 1; // 错误 $result['msg'] = "CURL Error:".curl_error($ch); } $result['body'] = $output; //释放curl句柄 curl_close($ch); return $result; }
其他请求类型请自己参考封装处理
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 黄心懋1991-轻忧郁【滚石】【WAV+CUE】
- 群星.2000-大地金曲世纪回顾2CD【大地】【WAV+CUE】
- 王菲.1996-浮躁(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】