服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php远程请求CURL实例教程(爬虫、保存登录状态)

php远程请求CURL实例教程(爬虫、保存登录状态)

2021-11-01 15:51slongzhang_ PHP教程

这篇文章主要给大家介绍了关于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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * 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,'?')?trim($url,'&').'&'.$param:$url.'?'.$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_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;
}

POST案例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * 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;
}

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

到此这篇关于php远程请求CURL(爬虫、保存登录状态)的文章就介绍到这了,更多相关php远程请求CURL(爬虫、保存登录状态)内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_35606400/article/details/108023234

延伸 · 阅读

精彩推荐
  • PHP教程PHP实现二叉树的深度优先与广度优先遍历方法

    PHP实现二叉树的深度优先与广度优先遍历方法

    这篇文章主要介绍了PHP实现二叉树的深度优先与广度优先遍历方法,涉及php针对二叉树进行遍历的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    风雨征途20122152020-11-23
  • PHP教程Yii2框架整合Xunsearch搜索引擎的方法

    Yii2框架整合Xunsearch搜索引擎的方法

    这篇文章主要介绍了Yii2框架整合Xunsearch搜索引擎的方法,结合实例形式分析了Yii2框架整合Xunsearch的具体步骤与相关注意事项,需要的朋友可以参考下...

    dengwz778810432021-03-11
  • PHP教程.htaccess文件保护实例讲解

    .htaccess文件保护实例讲解

    .htaccess太强大了,但它本身会不会被破解掉呢,请问如何保护它? ...

    PHP教程网4442019-11-17
  • PHP教程PHP实现随机发放扑克牌

    PHP实现随机发放扑克牌

    这篇文章主要为大家详细介绍了PHP实现随机发放扑克牌,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    野马菲比8772021-10-08
  • PHP教程SAE实时日志接口SDK用法示例

    SAE实时日志接口SDK用法示例

    这篇文章主要介绍了SAE实时日志接口SDK用法,结合实例形式分析了PHP版本的接口调用SDK及相应的使用方法,需要的朋友可以参考下...

    夏天的风7962021-03-09
  • PHP教程PHP积分兑换接口实例

    PHP积分兑换接口实例

    这篇文章主要介绍了PHP积分兑换接口,实例分析了积分兑换接口的逻辑处理与数据库操作技巧,需要的朋友可以参考下...

    yhb2414152020-09-03
  • PHP教程PHP微框架Dispatch简介

    PHP微框架Dispatch简介

    Dispatch是一个PHP小框架,可以合并Dispatch和其他框架,开发出相当强大并且轻量级的程序,需要的朋友可以参考下 ...

    shichen20145012020-07-04
  • PHP教程解析crontab php自动运行的方法

    解析crontab php自动运行的方法

    本篇文章是对crontab php自动运行的方法进行了详细的分析介绍,需要的朋友参考下 ...

    PHP教程网1612020-04-26