When the same domain name corresponds to multiple IP addresses, PHP functions for obtaining remote webpage content PHP has multiple methods for obtaining remote webpage content, such as using built-in functions such as file_get_contents and fopen.
Echo file_get_contents ("http://blog.s135.com/abc.php ");
?>
However, in server load balancer such as DNS round-robin, the same domain name may correspond to multiple servers and multiple IP addresses. Assume that blog.s135.com is resolved to three IP addresses, namely 72.249.146.213, 72.249.146.214, and 72.249.146.215 by DNS. each time you access blog.s135.com, the system accesses one of the servers based on the corresponding.
When I was working on a video project last week, I had to access a PHP interface program (suppose abc. php) on each server in sequence to query the transmission status of this server.
In this case, you cannot directly access the http://blog.s135.com/abc.php with file_get_contents, because it may have been repeatedly accessing a server.
Access http: // 72.249.146.213/abc in sequence. php, http: // 72.249.146.214/abc. php, http: // 72.249.146.215/abc. the php method does not work when the Web Server on the three servers has multiple virtual hosts.
You cannot set the local hosts because hosts cannot set multiple IP addresses to correspond to the same domain name.
It can only be implemented through PHP and HTTP: when accessing abc. php, add the blog.s135.com domain name in the header. So I wrote the following PHP function:
Java code
/************************
* Function purpose: obtain the remote webpage content of the specified server when the same domain name corresponds to multiple IP addresses.
* Creation time:
* Created By: Zhang Yan (blog.s135.com)
* Parameter description:
* $ Ip address of the ip server
* $ Host server host name
* $ Url server URL (excluding domain names)
* Return value:
* Remote webpage content obtained
* False: An error occurred while accessing the remote webpage.
************************/
Function HttpVisit ($ ip, $ host, $ url)
{
$ Errstr = '';
$ Errno = '';
$ Fp = fsockopen ($ ip, 80, $ 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 ";
Fputs ($ fp, $ out );
While ($ line = fread ($ fp, 4096 )){
$ Response. = $ line;
}
Fclose ($ fp );
// Remove Header information
$ Pos = strpos ($ response, "\ 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 ");
?>