Phpfile_get_contents function proxy to obtain code for remote pages

Source: Internet
Author: User
Phpfile_get_contents function proxy to obtain code for remote pages

  1. $ Url = "http://bbs.it-home.org /";
  2. $ Ctx = stream_context_create (array (
  3. 'Http' => array ('timeout' => 5,
  4. 'Proxy' => 'tcp: // 60.175.203.243: 8080 ',
  5. 'Request _ fulluri '=> True ,)
  6. )
  7. );
  8. $ Result = file_get_contents ($ url, False, $ ctx );
  9. Echo $ result;
  10. ?>

2. curl proxy method:

  1. Function postPage ($ url)
  2. {
  3. $ Response = "";
  4. $ Rd = rand (1, 4 );
  5. $ Proxy = 'http: // 212.33.27.253: 8080 ';
  6. If ($ rd = 2) $ proxy = 'http: // 212.88.16.56: 8088 ';
  7. If ($ rd = 3) $ proxy = 'http: // 202.98.123.126: 8080 ';
  8. If ($ rd = 4) $ proxy = 'http: // 59.14.97.38: 8080 ';
  9. If ($ url! = ""){
  10. $ Ch = curl_init ($ url );
  11. Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
  12. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  13. Curl_setopt ($ ch, CURLOPT_PROXY, $ proxy );
  14. $ Response = curl_exec ($ ch );
  15. If (curl_errno ($ ch) $ response = "";
  16. Curl_close ($ ch );
  17. }
  18. Return $ response;
  19. }

Appendix: use php file_get_contents to solve the problem of ajax cracking domain

In ajax application, sometimes the domain call file is broken, and the browser sends a warning or even directly blocks this operation for security. If it is IE, a warning window will pop up asking if you want to continue the operation. only when you agree with IE will you call the file that breaks down the domain. Other browsers, such as Firefox and Opera, receive an error by default to prevent external domain files from being called. This will give users a poor operation experience. if you want to solve this problem by modifying the security settings of the browser, it is unrealistic, preferably on the server side.

On the server side, you can use a file in the same domain as the proxy file. This Proxy file will get the content of the external domain file and then pass it to ajax. In this way, ajax does not call the external domain file, but calls the Proxy file of the same domain, and the security problem is solved.

If the server supports PHP, you can use the file_get_contents function. for detailed usage, see http://www.w3school.com.cn/php/func_filesystem_file_get_contents.aspexample:

  1. $ ServerAddress = 'http: // s.jbxue.com ';
  2. // Obtain the external domain file content
  3. $ RandomNumber = file_get_contents ($ serverAddress );
  4. // Output Content
  5. Echo $ randomNumber;
  6. ?>

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.