php请求远程url内容有两个方法fopen/file_get_contents和curl。
1,fopen/file_get_contents与curl的差异
(1)fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen /file_get_contents 好很多。
(2)fopen /file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。
(3)curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen / file_get_contents只能使用get方式获取数据。
2,如果远程服务器关闭,file_get_contents处理方法,可以参考这篇文章,http://www.cnblogs.com/scofi/articles/3607529.html
公司里有经常有这样的业务,需要调用第三方公司提供的HTTP接口,在把接口提供的信息显示到网页上,代码是这样写的: file_get_contents("http://example.com/") 。
1
2
3
4
5
6
7
8
9
|
$opts
=
array
(
'http'
=>
array
(
'method'
=>
"GET"
,
'timeout'
=>10,
)
);
$context
= stream_context_create(
$opts
);
$html
=
file_get_contents
(
'http://www.example.com'
, false,
$context
);
echo
$html
;
|
代码中的timeout就是file_get_contents读取url的超时时间。
上篇说到我们说到设置file_get_contents超时时间用到了 stream_context_create方法,那么这个方法到底是什么呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?php
$data
=
array
(
"name"
=>
'test_name'
,
"content"
=>
'test_con'
);
$data
= http_build_query(
$data
);
$opts
=
array
(
'http'
=>
array
(
'method'
=>
"POST"
,
'header'
=>
"Content-type: application/x-www-form-urlencoded\r\n"
.
"Content-length:"
.
strlen
(
$data
).
"\r\n"
.
"Cookie: foo=bar\r\n"
.
"\r\n"
,
'content'
=>
$data
,
)
);
$cxContext
= stream_context_create(
$opts
);
$sFile
=
file_get_contents
(
"http://127.0.0.1/reponse.php"
, false,
$cxContext
);
echo
$sFile
;
?>
|
reponse.php被请求的页面:
1
2
3
4
|
<?php
var_dump(
$_POST
);
var_dump(
$_COOKIE
);
?>
|
运行之后的结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
$url
=
'http://www.example.com'
;
//初始化一个 cURL 对象
$ch
= curl_init();
//设置你需要抓取的URL
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
//是否获得跳转后的页面
curl_setopt(
$ch
, CURLOPT_FOLLOWLOCATION, 1);
$data
= curl_exec(
$ch
);
curl_close(
$ch
);
echo
$data
;
?>
|
(2)使用curl。post获取数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php
function
curl_post(
$url
,
$arr_data
){
$post_data
= http_build_query(
$url_data
);
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ch
, CURLOPT_POST, 1);
curl_setopt(
$ch
, CURLOPT_POSTFLELDS,
$post_data
);
$data
= curl_exec(
$ch
);
curl_close(
$ch
);
echo
$data
;
}
$arr_post
=
array
(
'name'
=>
'test_name'
,
'age'
=> 1
);
curl_post(
"http://www.explame.com/"
,
$arr_post
);
?>
|
(3)使用代理抓取页面,什么要使用代理进行抓取呢?以google为例吧,如果去抓google的数据,短时间内抓的很频繁的话,你就抓取不到了。google对你的ip地址做限制这个时候,你可以换代理重新抓。
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
"http://google.com"
);
curl_setopt(
$ch
, CURLOPT_HEADER, false);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
//是否通过http代理来传输
curl_setopt(
$ch
, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt(
$ch
, CURLOPT_PROXY, 125.21.23.6:8080);
//url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');如果要密码的话,加上这个
$result
=curl_exec(
$ch
);
curl_close(
$ch
);
?>
|
(4)继续保持本站session调用,在实现用户同步登录的情况下需要共享session,如果要继续保持本站的session,那么要把sessionid放到http请求中。
1
2
3
4
5
6
7
8
9
10
11
|
<?php
$session_str
= session_name().
'='
.session_id().
'; path=/; domain=.explame.com'
;
session_write_close();
//将数据写入文件并且结束session
$ch
= curl_init();
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
curl_setopt(
$ch
, CURLOPT_HEADER, false);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ch
, CURLOPT_COOKIE,
$session_str
);
$ret
= curl_exec(
$ch
);
curl_close(
$ch
);
?>
|