php远程请求CURL实例教程(爬虫、保存登录状态)
发布:smiling 来源: PHP粉丝网 添加日期:2022-04-04 10:26:24 浏览: 评论:0
这篇文章主要给大家介绍了关于php远程请求CURL(爬虫、保存登录状态)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
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(emptyempty($options)){
- $options = array(
- 'timeout' => 30,// 请求超时
- 'header' => array(),
- 'cookie' => '',// cookie字符串,浏览器直接复制即可
- 'cookie_file' => '',// 文件路径,并要有读写权限的
- 'ssl' => 0,// 是否检查https协议
- 'referer' => null
- );
- }else{
- emptyempty($options['timeout']) && $options['timeout'] = 30;
- emptyempty($options['ssl']) && $options['ssl'] = 0;
- }
- $result = array(
- 'code' => 0,
- 'msg' => 'success',
- 'body' => ''
- );
- if(is_array($param)){
- $param = http_build_query($param);
- }
- $url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param;
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL, $url);// 设置url
- !emptyempty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
- if(!emptyempty($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(!emptyempty($options['cookie'])){
- curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
- }
- curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容
- curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面
- !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl
- !emptyempty($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;
- }
POST案例
- /**
- * curl_post
- * @param $url 请求地址
- * @param null $param get参数
- * @param array $options 配置参数
- * @return array
- */
- function curl_post($url,$param = null,$options = array()){
- if(emptyempty($options)){
- $options = array(
- 'timeout' => 30,
- 'header' => array(),
- 'cookie' => '',
- 'cookie_file' => '',
- 'ssl' => 0,
- 'referer' => null
- );
- }else{
- emptyempty($options['timeout']) && $options['timeout'] = 30;
- emptyempty($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
- !emptyempty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头
- if(!emptyempty($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(!emptyempty($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
- !emptyempty($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;
- }
其他请求类型请自己参考封装处理。
Tags: php远程请求 CURL
- 上一篇:php解析非标准json、非规范json的方式实例
- 下一篇:最后一页
相关文章
- ·PHP启动提示Unable to load dynamic library php_curl.dll怎么办(2013-12-02)
- ·php中的curl使用心得详解(2014-02-22)
- ·php curl 伪造IP来源实例(2014-07-30)
- ·利用php curl实现程序登录新浪微博代码(2014-08-01)
- ·php curl post数据的问题(2014-08-04)
- ·php curl、fopen、file_get_contents实例代码(2014-08-17)
- ·php通过curl函数取得数据、模拟登陆、POST数据(2014-09-12)
- ·XAMPP使用CURL提示“Call to undefined function curl_init”问题(2014-09-12)
- ·php CURLOPT错误Warning: curl_setopt() [function.curl-setopt]:...(2014-09-20)
- ·php curl函数采集网页出现乱码问题解决方法(2014-09-21)
- ·解决php curl获取网页内容提示405 method not allowed状态码(2014-09-21)
- ·win7(X64)系统环境PHP Curl函数不能使用(2014-09-21)
- ·php curl实现http与https请求实例(2015-04-04)
- ·CURL模拟POST提交的二种方法实例(2015-04-09)
- ·php curl模仿登录人人网发布动态实例(2015-04-11)
- ·PHP用CURL伪造来源IP与来源URL地址程序代码(2015-04-11)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)