PHP 对代理池的使用

tsvico Lv5

没啥好说的,都在代码里

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
62
63
64
65
66
67
68
69
70
71
72
73
<?php
//获取新IP,并写入文件
function ip_file(){
if(($fp=fopen("ip.txt","r"))==false){
echo "打开ip.txt失败!";
}else{
/*
$counter=fgets($fp,1024); //读取文件中数据
fclose($fp); //关闭文本文件
*/
$counter = get_ip();
$fp=fopen("ip.txt","w"); //以写的方式打开文本文件<!---->
fputs($fp,$counter); //将新的统计数据增加1
fclose($fp);
}
}
//读IP
function read_ip(){
$fp=fopen("ip.txt","r");
$ip=fgets($fp,1024); //读取文件中数据
fclose($fp);
return $ip;
}
function get_ip()
{
$url = 'https://proxy.357.im/api/proxies/stable?protocol=http&anonymity=high_anonymous';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 设置头信息(当用IP直接访问时,加这个如:https://baibu.com -> 220.15.23.5)
// curl_setopt($ci, CURLOPT_HTTPHEADER, array('Host:baibu.com'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //这个是重点,规避ssl的证书检查。
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 跳过host验证
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);
if($httpCode==200){
$json = json_decode($response);
//$str = $json[]['protocol']."://".$json['ip'].":".$json['port'];
$json2 = $json->data;
$json3 = json_to_array($json2);
$str = $json3['protocol']."://".$json3['ip'].":".$json3['port'];
}
return $str;
}
function json_to_array($web){
$arr = array();
foreach($web as $k=>$w){
if(is_object($w)) $arr[$k]=$this->json_to_array($w); //判断类型是不是object
else $arr[$k]=$w;
}
return $arr;
}
//判断目标服务器是否挂掉
function isType(){
$ch = curl_init("http://www.xulirun.com");
$proxy = read_ip();
curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
$result = curl_exec($ch);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($ch);
curl_close($curl);
if($httpCode==200){
return true; //没挂
}else{
return false; //挂了
}

}
ip_file();
  • 标题: PHP 对代理池的使用
  • 作者: tsvico
  • 创建于 : 2019-02-17 12:24:49
  • 更新于 : 2021-03-06 20:21:07
  • 链接: https://blog.tbox.fun/2019/fb19b135.html
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
PHP 对代理池的使用