In the process of using PHP to collect data, we usually encounter the case that the IP is blocked or the verification code appears; In order to continue the acquisition, we need to switch different IP, each visit, randomly switch an IP. Of course, you can collect a large number of agents, through the way of switching agents to collect, the principle is probably similar.
Because I encountered this situation in the actual work, just happen the scene in the United States Station group server, above has already bound more than 200 IP (this server 1300 Yuan January), so can easily use the socket_bind () function to export IP binding, you only need to randomly extract an IP to bind it.
In C #, you can also use the Socket.bind () function for IP binding to switch between different IPs in the server to collect!
<?PHP//Output Content EchoGetdata ("Http://www.baidu.com/s?wd=ip"); //Getdata () acquisition function functionGetdata ($url){ //Random 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"; //WriteSocket_write ($sockHttp,$out,strlen($out)); $httpCode=substr(Socket_read ($sockHttp, 13), 9,3); $data= ' '; while($sRead= Socket_read ($sockHttp, 4096)){ $data.=$sRead; } //CloseSocket_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;} ?>
PHP uses socket_bind function to switch IP address to collect data