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; } ?> |