Use file_get_contents proxy in PHP

Source: Internet
Author: User
In PHP, use the file_get_contents proxy method. in PHP, use the file_get_contents proxy method to obtain the code of the remote webpage.

The code is as follows:


$ Url = "http://www.jb51.net /";
$ Ctx = stream_context_create (array (
'Http' => array ('timeout' => 5,
'Proxy' => 'tcp: // 60.175.203.243: 8080 ',
'Request _ fulluri '=> True ,)
)
);
$ Result = file_get_contents ($ url, False, $ ctx );
Echo $ result;
?>


Another curl method uses proxy:

The code is as follows:


Function postPage ($ url)
{
$ Response = "";
$ Rd = rand (1, 4 );
$ Proxy = 'http: // 221.214.27.253: 8080 ';
If ($ rd = 2) $ proxy = 'http: // 222.77.14.56: 8088 ';
If ($ rd = 3) $ proxy = 'http: // 202.98.123.126: 8080 ';
If ($ rd = 4) $ proxy = 'http: // 60.14.97.38: 8080 ';
If ($ url! = ""){
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_PROXY, $ proxy );
$ Response = curl_exec ($ ch );
If (curl_errno ($ ch) $ response = "";
Curl_close ($ ch );
}
Return $ response;
}


Use file_get_contents to solve ajax domain cracking problems

In ajax applications, files are called in different domains, and browsers warn or even block such operations for security purposes by default. 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 your server supports PHP, you can use the file_get_contents function to obtain other file content. For details about its usage, see the file_get_contents usage page on the official PHP website. Below is a simple example of this page.

The code is as follows:


$ ServerAddress = 'http: // s.jb51.net ';
// Obtain the external domain file content
$ RandomNumber = file_get_contents ($ serverAddress );
// Output Content
Echo $ randomNumber;
?>

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.