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_100dirdatasIplist.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()) . "
";
        } 
 
        // Bind the source address 
        if (socket_bind($sockHttp, $ip) === false) {
            echo "socket_bind() failed: reason: " .socket_strerror(socket_last_error($sockHttp)) . "
";
        }
 
        // 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
";
        $out .= "Host: {$host}
";
        $out .= "User-Agent: {$user_agent}
";
        $out .= "Accept: */*
";
        $out .= "Accept-Language: zh-cn
";
        $out .= "Accept-Encoding: identity
";
        $out .= "Referer: {$url}
";
        $out .= "Cookie: {$cookie}
";
        $out .= "Connection: Close

";
 
        // 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:([^
]*)#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("/^[^<]*?

/","",$data);
 
    if($httpCode>=400){
        $data = "Request Error";
    } 
 
    return $data;
}
 
?>                        
牧羊童Gamir——随遇而安,保持一颗愉快之心!
原文地址:https://www.cnblogs.com/gamir/p/4308095.html