-
- $url='http://t.qq.com';
- $lines_array=file($url);
- $lines_string=implode('',$lines_array);
- echo htmlspecialchars($lines_string);
Copy code 2. file_get_contents() function
Using file_get_contents and fopen must enable allow_url_fopen.
Method: Edit php.ini and set allow_url_fopen = On. When allow_url_fopen is turned off, neither fopen nor file_get_contents can open remote files.
- $url='http://t.qq.com';
- $lines_string=file_get_contents($url);
- echo htmlspecialchars($lines_string);
Copy code 3. fopen()->fread()->fclose() mode
- $url='http://t.qq.com';
- $handle=fopen($url,"rb");
- $lines_string="";
- do{
- $data=fread($handle,1024);
- if(strlen($data)==0) {
- break;
- }
- $lines_string.=$data;
- }while(true);
- fclose($handle );
- echo htmlspecialchars($lines_string);
Copy code4. curl method
Using curl requires space to enable curl. Method: Modify php.ini under Windows, remove the semicolon in front of extension=php_curl.dll, and copy ssleay32.dll and libeay32.dll to C:WINDOWSsystem32; install the curl extension under Linux.
- $url='http://t.qq.com';
- $ch=curl_init();
- $timeout=5;
- curl_setopt($ch, CURLOPT_URL, $url );
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $lines_string=curl_exec($ch);
- curl_close($ch);
- echo htmlspecialchars($lines_string);
Copy code5. fsockopen() function socket mode
Whether the socket mode can be executed correctly is also related to the server settings. Specifically, you can use phpinfo to check which communication protocols are enabled on the server. For example, my local php socket does not enable http, so I can only use udp to test it.
- $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
- if (!$fp) {
- echo "ERROR: $errno - $errstr
n"
- } else {
- fwrite($fp, "n")
- echo fread($fp, 26)
- fclose($fp)
- }
Copy code
|