When the same domain name corresponds to multiple IP addresses, PHP obtains the remote webpage content function

Source: Internet
Author: User
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

  1. /************************

  2. * Function purpose: obtain the remote webpage content of the specified server when the same domain name corresponds to multiple IP addresses.

  3. * Creation time:

  4. * Created By: Zhang Yan (blog.s135.com)

  5. * Parameter description:

  6. * $ Ip address of the ip server

  7. * $ Host server host name

  8. * $ Url server URL (excluding domain names)

  9. * Return value:

  10. * Remote webpage content obtained

  11. * False: An error occurred while accessing the remote webpage.

  12. ************************/

  13. Function HttpVisit ($ ip, $ host, $ url)

  14. {

  15. $ Errstr = '';

  16. $ Errno = '';

  17. $ Fp = fsockopen ($ ip, 80, $ errno, $ errstr, 90 );

  18. If (! $ Fp)

  19. {

  20. Return false;

  21. }

  22. Else

  23. {

  24. $ Out = "GET {$ url} HTTP/1.1 \ r \ n ";

  25. $ Out. = "Host: {$ host} \ r \ n ";

  26. $ Out. = "Connection: close \ r \ n ";

  27. Fputs ($ fp, $ out );

  28. While ($ line = fread ($ fp, 4096 )){

  29. $ Response. = $ line;

  30. }

  31. Fclose ($ fp );

  32. // Remove Header information

  33. $ Pos = strpos ($ response, "\ r \ n ");

  34. $ Response = substr ($ response, $ pos + 4 );

  35. Return $ response;

  36. }

  37. }

  38. // Call method:

  39. $ Server_info1 = HttpVisit ("72.249.146.213", "blog.s135.com", "/abc. php ");

  40. $ Server_info2 = HttpVisit ("72.249.146.214", "blog.s135.com", "/abc. php ");

  41. $ Server_info3 = HttpVisit ("72.249.146.215", "blog.s135.com", "/abc. php ");

  42. ?>

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.