File_get_contents blocked Solution

Source: Internet
Author: User

In php, The file_get_contents function can directly collect remote server content and save it to a variable. Generally, the file_get_contents, fsockopen, and other IO operations are disabled, because they are afraid of DDOS attacks

In general, we can't change the server's inc. php. We can only write a set of IO to replace the above PHP function.

The Code is as follows: Copy code


$ Url = file_get_contents ('HTTP: // www. bKjia. c0m /');

We can use the following code instead.

The Code is as follows: Copy code

// How to disable file_get_contents
$ Ch = curl_init ();
$ Timeout = 10; // set to zero for no timeout
Curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // www.hzhuti.com /');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Url = curl_exec ($ ch );


Curl is a tool that uses URL syntax to transmit files and data. It supports many protocols, such as HTTP, FTP, and TELNET, and is not disabled by the server, therefore, we can open a URL like file_get_contents.

Use the function_exists function to determine whether php supports a function. You can easily write the following function.

 

The Code is as follows: Copy code
<? Php
Function vita_get_url_content ($ url ){
If (function_exists ('file _ get_contents ')){
$ File_contents = file_get_contents ($ url );
} Else {
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
}
Return $ file_contents;
}
?>

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.