Home > php教程 > php手册 > php curl采集远程页面内容演示代码

php curl采集远程页面内容演示代码

WBOY
Release: 2016-05-24 12:59:10
Original
1249 people have browsed it

在php中可以采集远程页面的函数有很多,如有file_get_contents(),fopen,file()这些函数都可采集远程服务器数据,但是要算性能curl最可,他支持支持多线程,代码如下:

$curlPost = 'a=1&b=2';//模拟POST数据 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:0.0.0.0', 'CLIENT-IP:0.0.0.0'));  //构造IP 
curl_setopt($ch, CURLOPT_REFERER, "http://www.phprm.com/");   //构造来路  
curl_setopt($ch,CURLOPT_URL, 'http://www.phprm.com');//需要抓取的页面路径 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt ($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);//post值 
//开源代码phprm.com 
$file_contents = curl_exec($ch);//抓取的内容放在变量中 
curl_close($ch)
Copy after login

还有一种办法就是使用file_get_contents()远程文件获取函数,用来获取远程页面内容.

注意事项:curl()效率挺高的,支持多线程,不过需要开启下curl扩展,下面是curl扩展开启的步骤:

1、将PHP文件夹下的三个文件php_curl.dll,libeay32.dll,ssleay32.dll复制到system32下;

2、将php.ini(c:WINDOWS目录下)中的;extension=php_curl.dll中的分号去掉; 

3、重启apache或者IIS.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template