PHP Gets the Remote Web page content function when the same domain name corresponds to multiple IPs

Source: Internet
Author: User
Tags cc domain

 When the same domain name corresponds to multiple IPs, PHP gets the function of the Remote Web page content [Article Zhang feast this article version: v1.0 last modified: 2008.12.15 reproduced Please specify the original link: http://blog.zyan.cc/post/389/] PHP gets Remote Web page content in several ways, such as using its own file_get_contents, fopen and other functions.   Reference <?php Echo file_get_contents ("http://blog.zyan.cc/abc.php"); ?> However, in load balancing such as DNS polling, the same domain name may correspond to multiple servers, more than one IP. Suppose blog.zyan.cc is parsed by DNS to 72.249.146.213, 72.249.146.214, 72.249.146.215 three IPs,  Each time a user accesses blog.zyan.cc, one of the servers is accessed based on the appropriate algorithm for load balancing.  When I was doing a video project last week, I met a requirement that we should go to a PHP interface program on each server (assuming abc.php) and query the transport status of this server.  It is not possible to access the http://blog.zyan.cc/abc.php directly with file_get_contents because it may have been repeatedly accessing a single server. Instead, access http://72.249.146.213/abc.php, http://72.249.146.214/abc.php, http://72.249.146.215/  Abc.php method, it is not possible to have multiple virtual hosts on a Web server in these three servers.  Not by setting up the local hosts, because hosts cannot set multiple IPs for the same domain name. That's only possible with PHP and http: When you access abc.php, add the blog.zyan.cc domain name to the header. So, I wrote the following PHP function: View plainprint?<?php/************************ * Function Purpose: When the same domain name corresponds to multiple IPs, get the Remote Web page content of the specified server * Created on: 2008-12-09 * created by: Zhang Yi (blog.zyan.cc) * Parameter Description: * $IP server's IP address * $host server's host name * $url server URL address (without domain name) * return Return Value: * ReceivedFetch Remote Web page content * False access to remote webpage failed ************************/function httpvisit ($ip, $host, $url) {$errstr = '         ;      $errno = ";      $fp = Fsockopen ($ip, $errno, $errstr, 90);         if (! $fp) {return false;          } else {$out = "GET {$url} http/1.1\r\n";             $out. = "host:{$host}\r\n";          $out. = "connection:close\r\n\r\n";               Fputs ($fp, $out);          while ($line = Fread ($fp, 4096)) {$response. = $line;            } fclose ($FP);          Remove Header header Information $pos = Strpos ($response, "\r\n\r\n");                $response = substr ($response, $pos + 4);         return $response;  }}//Call method: $server _info1 = httpvisit ("72.249.146.213", "blog.zyan.cc", "/abc.php");  $server _info2 = httpvisit ("72.249.146.214", "blog.zyan.cc", "/abc.php");  $server _info3 = httpvisit ("72.249.146.215", "blog.zyan.cc", "/abc.php");   ?>

  

PHP Gets the Remote Web page content function when the same domain name corresponds to multiple IPs

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.