Function _ PHP Tutorial for PHP to obtain remote webpage content under multiple IP addresses of the same domain name

Source: Internet
Author: User
Tags net domain
Function for PHP to obtain remote webpage content under multiple IP addresses of the same domain name. LieHuo. Net PHP Tutorial PHP provides multiple methods to obtain remote webpage content, such as using built-in functions such as file_get_contents and fopen .? Phpechofile_get_contents (http: Tutorial on PHP of Kangli website building Institute (Bkjia. Com)PHP provides multiple methods to obtain remote webpage content, such as using built-in functions such as file_get_contents and fopen.

Echo file_get_contents ("http://www.bkjia.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.kcoffee.net 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.kcoffee.net, the system accesses a server based on the algorithm.

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 use file_get_contents to access blog.kcoffee.net/abc.php. in this case, a server can be accessed directly by the operator.

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.kcoffee.net domain name in the header. So I wrote the following PHP function.

Reference content is as follows:
/************************
* 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 ");
?>

Using PHP to obtain remote webpage content in a variety of ways, such as using built-in functions such as file_get_contents and fopen. ? Php echo file_get_contents (http :/...

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.