PHP抓取页面的内容在实际的开发当中是非常有用的,如作一个简单的内容采集器,提取网页中的部分内容等等,抓取到的内容在通过正则表达式做一下过滤就得到了你想要的内容,至于如何用正则表达式过滤,在这里就不做介绍了,以下就是几种常用的用php抓取网页中的内容的方法.
1.file_get_contents,PHP代码如下:
<?php $url = "http://www.phprm.com"; $contents = file_get_contents($url); //如果出现中文乱码使用下面代码 //$getcontent = iconv("gb2312", "utf-8",$contents); echo $contents; ?>
2.curl,PHP代码如下:
<?php $url = "http://www.phprm.com"; $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; ?>
3.fopen->fread->fclose,PHP代码如下:
<?php $handle = fopen ("http://www.phprm.com", "rb"); $contents = ""; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); echo $contents; ?>
注:
1.使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。
2.使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:WINDOWSsystem32下;Linux下要安装curl扩展。
方法一,安装cURL
wget http://curl.haxx.se/download/curl-7.17.1.tar.gz tar -zxf curl-7.17.1.tar.gz ./configure --prefix=/usr/local/curl make & make install
安装php,只要打开开关 --with-curl=/usr/local/curl,就可以了,这个扩展库还是非常棒,是fsockopen等等相关的有效的替代品.
方法二,进入安装原php的源码目录.
cd ext cd curl phpize ./configure --with-curl=DIR make & make install
就会在PHPDIR/ext/curl/moudles/下生成curl.so的文件,复制curl.so文件到extensions的配置目录,修改php.ini就好了.
extension=curl.so
第一种方法试了N遍一直在失败中,于是放弃,使用第二种方法安装,phpize提示找不到,其实命令在/usr/local/php/bin/目标下:
# /usr/local/php/bin/phpize
./configure --with-curl=DIR需要指定php的配置路径,应该如下:
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-curl=DIR
注:上面的资料中错把--with-php-config写成了--with-php-php-config,然后就是编译安装:
# make
# make install
到这里会提示生成文件curl.so的路径: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
进入到这个路径下,复制curl到extension_dir目录下(本目录路径可以看phpinfo可是直接看php.int).
修改php.ini
extension=curl.so # /usr/local/php/bin/php -m
如果看到有curl项表示成功。
重启apache
# /usr/local/apache2/bin/apachectl stop # /usr/local/apache2/bin/apachectl start
到此成功了,在phpinfo中可以看到CURL的项.
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
