ile_get_contents是一个强大的函数,它不仅可以得到本地文件的内容,还可以抓取远程网址的内容。一般采集可以用Socket或Curl库,file_get_contents也同样可以实现,下面是代码:
01.
//设置头部信息
02.
$header
=
"Accept-Language: zh-cn\r\n"
;
03.
$header
.=
"http://www.myzmh.com\r\n"
; //设置来源网址
04.
05.
$opts
=
array
(
06.
'http'
=>
array
(
07.
'method'
=>
"GET"
,
//GET提交方式
08.
'timeout'
=> 5,
//超时时间
09.
'header'
=>
$header
,
//如没有提交的网址没有特殊限制,可以不用模拟头部信息
10.
)
11.
);
12.
13.
$url
=
'http://localhost/test/server.php'
;
14.
$context
= stream_context_create(
$opts
);
//创建文本流
15.
$content
= @
file_get_contents
(
$url
, false,
$context
);
16.
if
(
$content
)
17.
{
18.
echo
$content
;
19.
}
20.
else
21.
{
22.
echo
'请求出错'
;
23.
}