php正则函数抓取网页连接(如何利用cookie去访问网页,去get页面代码的函数 )
优采云 发布时间: 2021-11-22 05:12php正则函数抓取网页连接(如何利用cookie去访问网页,去get页面代码的函数
)
最近帮朋友开发了一个可以查询正方教务系统的微信公众平台号。得到一些东西。以上是个人经验总结。
开讲之前先说一下新浪云服务器。同一个程序中的功能在PC测试中可以正常运行,但是会挂在那里。
像往常一样,我会在代码中清楚地注释。使用下面的函数,你会得到两种形式的cookies,一种存储在文件中,另一种直接作为变量返回。
经验提示:有时候,在不同的代码运行环境下,用cookies访问文件会成功,但变量会失败,但有时是想法。但,
目前,这两种方法中的一种总会成功。
1 function get_cookie($url_,$params_,$referer_){
2
3 if($url_==null){echo "get_cookie_url_null";exit;}
4 if($params_==null){echo "get_params_null";exit;}
5 if($referer_==null){echo "get_referer-null";exit;}
6 $this_header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");//访问链接时要发送的头信息
7
8 $ch = curl_init($url_);//这里是初始化一个访问对话,并且传入url,这要个必须有
9
10 //curl_setopt就是设置一些选项为以后发起请求服务的
11
12
13 curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);//一个用来设置HTTP头字段的数组。使用如下的形式的数组进行设置: array('Content-type: text/plain', 'Content-length: 100')
14 curl_setopt($ch, CURLOPT_HEADER,1);//如果你想把一个头包含在输出中,设置这个选项为一个非零值,我这里是要输出,所以为 1
15
16 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将 curl_exec()获取的信息以文件流的形式返回,而不是直接输出。设置为0是直接输出
17
18 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//设置跟踪页面的跳转,有时候你打开一个链接,在它内部又会跳到另外一个,就是这样理解
19
20 curl_setopt($ch,CURLOPT_POST,1);//开启post数据的功能,这个是为了在访问链接的同时向网页发送数据,一般数urlencode码
21
22 curl_setopt($ch,CURLOPT_POSTFIELDS,$params_); //把你要提交的数据放这
23
24 curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');//获取的cookie 保存到指定的 文件路径,我这里是相对路径,可以是$变量
25
26 //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');//要发送的cookie文件,注意这里是文件,还一个是变量形式发送
27
28 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);//例如这句就是设置以变量的形式发送cookie,注意,这里的cookie变量是要先获取的,见下面获取方式
29
30 curl_setopt ($ch, CURLOPT_REFERER,$referer_); //在HTTP请求中包含一个'referer'头的字符串。告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理。
31
32 $content=curl_exec($ch); //重点来了,上面的众多设置都是为了这个,进行url访问,带着上面的所有设置
33
34 if(curl_errno($ch)){
35 echo 'Curl error: '.curl_error($ch);exit(); //这里是设置个错误信息的反馈
36 }
37
38 if($content==false){
39 echo "get_content_null";exit();
40 }
41 preg_match('/Set-Cookie:(.*);/iU',$content,$str); //这里采用正则匹配来获取cookie并且保存它到变量$str里,这就是为什么上面可以发送cookie变量的原因
42
43 $cookie = $str[1]; //获得COOKIE(SESSIONID)
44
45 curl_close($ch);//关闭会话
46
47 return $cookie;//返回cookie
48 }
下面是如何使用上面的cookie来访问网页,发布数据,获取页面代码的功能。
1 function post($url,$post_data,$location = 0,$reffer = null,$origin = null,$host = null){
2
3 $post_data = is_array($post_data)?http_build_query($post_data):$post_data;
4 //产生一个urlencode之后的请求字符串,因为我们post,传送给网页的数据都是经过处理,一般是urlencode编码后才发送的
5
6 $header = array( //头部信息,上面的函数已说明
7 'Accept:*/*',
8 'Accept-Charset:text/html,application/xhtml+xml,application/xml;q=0.7,*;q=0.3',
9 'Accept-Encoding:gzip,deflate,sdch',
10 'Accept-Language:zh-CN,zh;q=0.8',
11 'Connection:keep-alive',
12 'Content-Type:application/x-www-form-urlencoded',
13 //'CLIENT-IP:'.$ip,
14 //'X-FORWARDED-FOR:'.$ip,
15 );
16
17 //下面的都是头部信息的设置,请根据他们的变量名字,对应上面函数所说明
18 if($host){
19 $header = array_merge_recursive($header,array("Host:".$host));
20 }
21 else if($this->option["host"]){
22 $header = array_merge_recursive($header,array("Host:".$this->option["host"]));
23 }
24 if($origin){
25 $header = array_merge_recursive($header,array("Origin:".$origin));
26 }
27 else{
28 $header = array_merge_recursive($header,array("Origin:".$url));
29 }
30 if($reffer){
31 $header = array_merge_recursive($header,array("Referer:".$reffer));
32 }
33 else{
34 $header = array_merge_recursive($header,array("Referer:".$url));
35 }
36
37 $curl = curl_init(); //这里并没有带参数初始化
38
39 curl_setopt($curl, CURLOPT_URL, $url);//这里传入url
40
41 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
42
43 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//对*敏*感*词*来源的检查,不开启次功能
44
45 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//从证书中检测 SSL 加密算法
46
47 curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
48 //模拟用户使用的浏览器,自己设置,我的是"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"
49 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location);
50
51 curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动设置referer
52
53 curl_setopt($curl, CURLOPT_POST, 1);//开启post
54
55 curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
56 //HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。
57 //我上面设置的是*/*
58
59 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);//要传送的数据
60
61 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);//以变量形式发送cookie,我这里没用它,文件保险点
62
63 curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); //存cookie的文件名,
64
65 curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); //发送
66
67 curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制,防止死循环
68
69 curl_setopt($curl, CURLOPT_HEADER, 1);
70
71 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
72
73 $tmpInfo = curl_exec($curl);
74 if (curl_errno($curl)) {
75 echo 'Curl error: ' . curl_error ( $curl );exit();
76 }
77
78 curl_close($curl);
79 list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2);//分割出网页源代码的头和bode
80 $tmpInfo = $this->auto_charest($tmpInfo);//转码,防止乱码,自定义函数
81 return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo);
82 }
上面是post,下面是get。两者很相似。不同的是get没有发帖,将数据传输到发布前访问过的网页,只获取源码。
1 function get($url,$location = 1,$origin = null,$reffer = null,$host = null){
2 //$ip = $this->randip();
3 if($url==null){
4 echo "get-url-null";exit();
5 }
6 $header = array(
7 'Accept:*/*',
8 'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3',
9 'Accept-Encoding:gzip,deflate,sdch',
10 'Accept-Language:zh-CN,zh;q=0.8',
11 'Connection:keep-alive',
12
13 //'CLIENT-IP:'.$ip,
14 //'X-FORWARDED-FOR:'.$ip,
15 );
16 if($host){
17 $header = array_merge_recursive($header,array("Host:".$host));
18 }
19 else if($this->option["host"]){
20 $header = array_merge_recursive($header,array("Host:".$this->option["host"]));
21 }
22 if($origin){
23 $header = array_merge_recursive($header,array("Origin:".$origin));
24 }
25 else{
26 $header = array_merge_recursive($header,array("Origin:".$url));
27 }
28 if($reffer){
29 $header = array_merge_recursive($header,array("Referer:".$reffer));
30 }
31 else{
32 $header = array_merge_recursive($header,array("Referer:".$url));
33 }
34
35 $curl = curl_init();
36 curl_setopt($curl, CURLOPT_URL, $url);
37 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
38 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
39 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
40 curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
41 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $location);
42 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
43 curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
44 curl_setopt($curl, CURLOPT_ENCODING, "gzip" );
45 curl_setopt($curl, CURLOPT_HTTPGET, 1);
46 //curl_setopt($curl, CURLOPT_COOKIE, $this->cookies);
47 curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
48 curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
49 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
50 curl_setopt($curl, CURLOPT_HEADER, 1);
51 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
52 $tmpInfo = curl_exec($curl);
53
54 if (curl_errno($curl)) {
55 echo 'Curl error: '.curl_error ($curl);exit();
56 }
57 curl_close($curl);
58 list($header, $body) = explode("\r\n\r\n", $tmpInfo, 2);
59 $tmpInfo = $this->auto_charest($tmpInfo);
60 return array("header"=>$header,"body"=>$body,"content"=>$tmpInfo);
61 }
好的,如果觉得对你有用,请默默点开顶。(右下角)
如果你觉得这篇文章文章还不错或者有所收获,可以扫描下方支付宝二维码【素材支持】,或者点击右下角【推荐】按钮奖励我一杯咖啡角[精神]支持],因为这两种支持是我继续写作和分享的最大动力,