当前位置:首页 > PHP教程 > php应用 > 列表

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案例:

  1. /** 
  2.  * curl_get 
  3.  * @param $url 
  4.  * @param null $param 
  5.  * @param null $options 
  6.  * @return array 
  7.  */ 
  8. function curl_get($url,$param = null,$options = null){ 
  9.  if(emptyempty($options)){ 
  10.   $options = array
  11.    'timeout'        => 30,// 请求超时 
  12.    'header'         => array(), 
  13.    'cookie'         => '',// cookie字符串,浏览器直接复制即可 
  14.    'cookie_file' => '',// 文件路径,并要有读写权限的 
  15.    'ssl'            => 0,// 是否检查https协议 
  16.    'referer'        => null 
  17.   ); 
  18.  }else
  19.   emptyempty($options['timeout']) && $options['timeout'] = 30; 
  20.   emptyempty($options['ssl']) && $options['ssl'] = 0; 
  21.  } 
  22.  $result = array
  23.   'code'  => 0, 
  24.   'msg'  => 'success'
  25.   'body'  => '' 
  26.  ); 
  27.  if(is_array($param)){ 
  28.   $param = http_build_query($param); 
  29.  } 
  30.  $url = strstr($url,'?')?trim($url,'&').'&'.$param:$url.'?'.$param
  31.  $ch = curl_init(); 
  32.  
  33.  curl_setopt($ch,CURLOPT_URL, $url);// 设置url 
  34.  !emptyempty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 
  35.  if(!emptyempty($options['cookie_file']) && file_exists($options['cookie_file'])){ 
  36.   curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); 
  37.   curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); 
  38.  }else if(!emptyempty($options['cookie'])){ 
  39.   curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); 
  40.  } 
  41.  curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 
  42.  curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头 
  43.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面 
  44.  !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl 
  45.  !emptyempty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗 
  46.  curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); 
  47.  //执行并获取内容 
  48.  $output = curl_exec($ch); 
  49.  //对获取到的内容进行操作 
  50.  if($output === FALSE ){ 
  51.   $result['code'] = 1; // 错误 
  52.   $result['msg'] = "CURL Error:".curl_error($ch); 
  53.  } 
  54.  $result['body'] = $output
  55.  //释放curl句柄 
  56.  curl_close($ch); 
  57.  return $result

POST案例

  1. /** 
  2.  * curl_post 
  3.  * @param $url    请求地址 
  4.  * @param null $param  get参数 
  5.  * @param array $options 配置参数 
  6.  * @return array 
  7.  */ 
  8. function curl_post($url,$param = null,$options = array()){ 
  9.  if(emptyempty($options)){ 
  10.   $options = array
  11.    'timeout'        => 30, 
  12.    'header'         => array(), 
  13.    'cookie'         => ''
  14.    'cookie_file' => ''
  15.    'ssl'            => 0, 
  16.    'referer'        => null 
  17.   ); 
  18.  }else
  19.   emptyempty($options['timeout']) && $options['timeout'] = 30; 
  20.   emptyempty($options['ssl']) && $options['ssl'] = 0; 
  21.  } 
  22.  
  23.  $result = array
  24.   'code'  => 0, 
  25.   'msg'  => 'success'
  26.   'body'  => '' 
  27.  ); 
  28.  if(is_array($param)){ 
  29.   $param = http_build_query($param); 
  30.  } 
  31.  $ch = curl_init(); 
  32.  curl_setopt($ch, CURLOPT_URL, $url);// 设置url 
  33.  !emptyempty($options['header']) && curl_setopt($ch, CURLOPT_HTTPHEADER, $options['header']); // 设置请求头 
  34.  if(!emptyempty($options['cookie_file']) && file_exists($options['cookie_file'])){ 
  35.   curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']); 
  36.   curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']); 
  37.  }else if(!emptyempty($options['cookie'])){ 
  38.   curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']); 
  39.  } 
  40.  
  41.  
  42.  curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //curl解压gzip页面内容 
  43.  curl_setopt($ch, CURLOPT_POST, 1); 
  44.  curl_setopt($ch, CURLOPT_POSTFIELDS, $param); 
  45.  curl_setopt($ch, CURLOPT_HEADER, 0);// 不获取请求头 
  46.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 输出转移,不输出页面 
  47.  !$options['ssl'] && curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['ssl']); // 禁止服务器端的验证ssl 
  48.  !emptyempty($options['referer']) && curl_setopt($ch, CURLOPT_REFERER, $options['referer']);//伪装请求来源,绕过防盗 
  49.  curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); 
  50.  //执行并获取内容 
  51.  $output = curl_exec($ch); 
  52.  //对获取到的内容进行操作 
  53.  if($output === FALSE ){ 
  54.   $result['code'] = 1; // 错误 
  55.   $result['msg'] = "CURL Error:".curl_error($ch); 
  56.  } 
  57.  $result['body'] = $output
  58.  //释放curl句柄 
  59.  curl_close($ch); 
  60.  return $result

其他请求类型请自己参考封装处理。

Tags: php远程请求 CURL

分享到: