php远程请求CURL实例教程(爬虫、保存登录状态)
网络编程 2021-07-04 23:06www.168986.cn编程入门
这篇文章主要给大家介绍了关于php远程请求CURL(爬虫、保存登录状态)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
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(爬虫、保存登录状态)的相关内容,狼蚁网站SEO优化话不多说了,来一起看看详细的介绍吧
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' => 'suess', 'body' => '' ); if(is_array($param)){ $param = http_build_query($param); } $url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param; $ch = curl_init(); curl_set($ch,CURLOPT_URL, $url);// 设置url !empty($options['header']) && curl_set($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){ curl_set($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); curl_set($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); }else if(!empty($options['cookie'])){ curl_set($ch, CURLOPT_COOKIE, $options['cookie']); } curl_set($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 curl_set($ch, CURLOPT_HEADER, 0);// 不获取请求头 curl_set($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面 !$options['ssl'] && curl_set($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl !empty($options['referer']) && curl_set($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗 curl_set($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; }
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' => 'suess', 'body' => '' ); if(is_array($param)){ $param = http_build_query($param); } $ch = curl_init(); curl_set($ch, CURLOPT_URL, $url);// 设置url !empty($options['header']) && curl_set($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 if(!empty($options['cookie_file']) && file_exists($options['cookie_file'])){ curl_set($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); curl_set($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); }else if(!empty($options['cookie'])){ curl_set($ch, CURLOPT_COOKIE, $options['cookie']); } curl_set($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 curl_set($ch, CURLOPT_POST, 1); curl_set($ch, CURLOPT_POSTFIELDS, $param); curl_set($ch, CURLOPT_HEADER, 0);// 不获取请求头 curl_set($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面 !$options['ssl'] && curl_set($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl !empty($options['referer']) && curl_set($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗 curl_set($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; }
其他请求类型请自己参考封装处理
到此这篇关于php远程请求CURL(爬虫、保存登录状态)的文章就介绍到这了,更多相关php远程请求CURL(爬虫、保存登录状态)内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程