This article introduces three methods of obtaining web content in PHP. Friends in need can refer to it.
Use php code to implement the principle of obtaining web pages: The content will be crawled and filtered through regular expressions to get the desired content. Programmer Home has introduced a lot about regular expressions, you can refer to them for reference. php gets content, method 1, file_get_contents <?php $url = "http://bbs.it-home.org"; $contents = file_get_contents($url); //如果出现中文乱码使用下面代码 //$getcontent = iconv("gb2312", "utf-8",$contents); echo $contents; ?> Copy after login Get web content with php, method 2, curl <?php $url = "http://bbs.it-home.org"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页里需要增加下面两行 //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD); $contents = curl_exec($ch); curl_close($ch); echo $contents; ?> Copy after login php gets web page content, method 3, fopen->fread->fclose <?php $handle = fopen ("http://bbs.it-home.org", "rb"); $contents = ""; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); echo $contents; ?> Copy after login Note: 1. Allow_url_fopen must be turned on when using file_get_contents() and fopen(). method: Edit php.ini and set allow_url_fopen = On. When allow_url_fopen is turned off, neither the fopen() nor file_get_contents() functions can open remote files. 2. When using curl(), curl must be enabled in space. method: Under Windows, modify php.ini, remove the semicolon in front of extension=php_curl.dll, and then copy ssleay32.dll and libeay32.dll to C:WINDOWSsystem32; Under Linux, just install the curl extension. |