_php tutorial for getting Remote Web page content from PHP with multiple IP domains

Source: Internet
Author: User
Tags net domain
Fire Station College (bkjia.com) PHP tutorialPHP gets Remote Web page content in several ways, such as using its own file_get_contents, fopen and other functions.

Echo file_get_contents ("http://www.bkjia.com/abc.php");
?>

However, in a load balancer such as DNS polling, the same domain name may correspond to multiple servers, more than one IP. Suppose blog.kcoffee.net 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.kcoffee.net, 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 blog.kcoffee.net/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.kcoffee.net domain name to the header. So, I wrote the following PHP function.

The following is the referenced content:
/************************
* Function use: When the same domain name corresponds to multiple IPs, get the Remote Web page content of the specified server
* Date Created: 2008-12-09
* Created by: Zhang Yi (blog.s135.com)
* Parameter Description:
* $ip The IP address of the server
* The host name of the $host server
* The URL address of the $url server (excluding domain name)
* Return value:
* Access to the Remote Web page content
* False access to Remote Web page 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 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.s135.com", "/abc.php");
$server _info2 = httpvisit ("72.249.146.214", "blog.s135.com", "/abc.php");
$server _info3 = httpvisit ("72.249.146.215", "blog.s135.com", "/abc.php");
?>

http://www.bkjia.com/PHPjc/364233.html www.bkjia.com true http://www.bkjia.com/PHPjc/364233.html techarticle Fire Station College (liehuo.net) PHP tutorial PHP get Remote Web page content in a variety of ways, such as with its own file_get_contents, fopen and other functions. php echo file_get_contents (http:/ ...

  • Related Article

    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.