PHP uses socket_bind function to switch IP address to collect data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.