PHP利用socket_bind函数切换IP地址采集数据

时间:2023-03-09 20:53:33
PHP利用socket_bind函数切换IP地址采集数据

在利用PHP进行数据采集的过程中,通常会遇到IP被屏蔽或出现验证码的情况;为了能够继续采集,我们需要切换不同的ip,每访问一次,随机切换一个IP。当然也可以通过收集大量代理,通过切换代理的方式进行采集,原理大抵相似。
       因为本人在实际工作中遇到这种情况,刚好发生的场景在美国站群的服务器,上面有已经绑定了200多个ip(这种服务器1300元一月),因此可以轻松的利用socket_bind()函数进行出口ip的绑定,只需要随机抽取一个IP进行绑定就可以。
           在C#中同样可以通过Socket.Bind()函数进行ip的绑定,以此切换服务器中不同ip进行采集!

<?php
//输出内容
echo Getdata("http://www.baidu.com/s?wd=ip"); //Getdata()采集函数
function Getdata($url){
//随机ip
require_once('D:\fang360_100dir\datas\Iplist.php');
$ip = $ip_arr[rand(0,count($ip_arr)-1)]; //host post path
$arr = parse_url($url);
$path=$arr['path']?$arr['path']:"/";
$host=$arr['host'];
$port=isset($arr['port'])?$arr['port']:80;
if ( $arr['query'] ){
$path .= "?".$arr['query'];
} // Create a new socket
$sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$sockHttp){
echo "socket_create() failed: reason: " .socket_strerror(socket_last_error()) . "\n";
} // Bind the source address
if (socket_bind($sockHttp, $ip) === false) {
echo "socket_bind() failed: reason: " .socket_strerror(socket_last_error($sockHttp)) . "\n";
} // Connect to destination address
$resSockHttp = socket_connect($sockHttp, $host, $port);
if (!$resSockHttp){
echo 'socket_connect() failed!';
} $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 xttest/24.0";
$cookie = '';
$timeout = 25;
$out = "GET {$path} HTTP/1.0\r\n";
$out .= "Host: {$host}\r\n";
$out .= "User-Agent: {$user_agent}\r\n";
$out .= "Accept: */*\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "Accept-Encoding: identity\r\n";
$out .= "Referer: {$url}\r\n";
$out .= "Cookie: {$cookie}\r\n";
$out .= "Connection: Close\r\n\r\n"; // Write
socket_write($sockHttp, $out,strlen($out)); $httpCode = substr(socket_read($sockHttp, 13),9,3); $data ='';
while ($sRead = socket_read($sockHttp, 4096)){
$data .= $sRead;
}
// Close
socket_close($sockHttp); if (preg_match("#Content-Type:([^\r\n]*)#i", $data, $matches) && trim($matches[1]) != '')
{
$content_type_array = explode(';', $matches[1]);
$ContentType = strtolower(trim($content_type_array[0]));
}
else
{
$ContentType = 'text/html';
} header("Content-type: $ContentType");
$data=preg_replace("/^[^<]*?\r\n\r\n/","",$data); if($httpCode>=400){
$data = "Request Error";
} return $data;
} ?>