This article introduces several methods of obtaining remote web content implemented by PHP, including fopen and curl methods. Friends in need may refer to them.
Share a few pieces of php code to get the content of remote web pages. 1. fopen method <?php $handle = fopen ("http://bbs.it-home.org/", "rb"); $contents = ""; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); echo $contents; //输出获取到得内容。 //以下适用于php5以上版本 $handle = fopen("http://bbs.it-home.org", "rb"); $contents = stream_get_contents($handle); fclose($handle); echo $contents; ?> Copy after login If an error occurs: failed to open stream: HTTP request failed!. Solution: In php.ini, there are two options: allow_url_fopen =on (indicates that remote files can be opened through url), user_agent="PHP" (indicates which script is used to access the network, by default there is a ";" in front of it, just remove it. ) Restart the server. As shown below: Perfect solution: Set the user_agent in php.ini. The default user_agent of php is PHP. We change it to Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser. user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 2. curl method <?php $url = "http://bbs.it-home.org"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10); $dxycontent = curl_exec($ch); echo $dxycontent; ?> Copy after login Note: You can use the following code to download under Linux exec("wget {$url}"); The differences between PHP’s external resource grabbing functions fopen, file_get_contents, and curl: 1. fopen/file_get_contents will redo the DNS query for each request and does not cache the DNS information. 2. CURL will automatically cache DNS information. Requests for web pages or images under the same domain name only require one DNS query. This greatly reduces the number of DNS queries. Therefore, the performance of CURL is better than fopen and file_get_contents, and it is recommended to use it. |