file_get_contents
file_get_contents() 函数把整个文件读入一个字符串中,此函数可以用来打开一个网络地址 可以实现简单的网页抓取。
也可以读取本地的文件,以及模拟post请求。
GET请求,查询北京天气如下:1
2url = 'https://www.sojson.com/open/api/weather/json.shtml?city=北京';
url);
模拟POST请求如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18$data = array(
'name' => 'wang',
'age' => 22,
);
// name=wang&age=22
$query = http_build_query($data);
$options['http'] = array(
'timeout' => 60,
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $query,
);
$url = 'http://127.0.0.1:8010/welcome.php';
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
注意:
- http_build_query() 函数是用来把数组生成经过URL-encode 的请求字符串。
- stream_context_create() 作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、
代理服务器、请求方式、头信息设置的特殊过程。
curl请求
curl是http请求库
模拟GET请求
1 | $url = 'https://www.sojson.com/open/api/weather/json.shtml?city=北京'; |
注意:如果启动了CURLOPT_HEADER
即会返回头信息,需要分离header和body1
2
3
4
5
6
7//分离 header 与 body
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //头信息大小
$header = substr($data, 0, $headerSize);
$body = substr($data, $headerSize);
// 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$body_data = json_decode($body);
HTTPS请求时要注意SSL验证
注意:因为请求时https ,所以走SSL ,如果出现错误: Curl error: 60 应该是证书出现了问题
我们在做测试,所以使用1
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
POST请求
post请求只需添加1
curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
具体事例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44$url = 'http://larabbs_api1.test/api/authorizations';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
$this_header = array(
'content-type:application/x-www-form-urlencoded;
charset=UTF-8',
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this_header);
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
//设置post数据
$post_data = array(
'username' => 'xiao@123.com',
'password' => 'password',
'grant_type' => 'password',
'client_id' => '1',
'client_secret' => '42YYyaogGjaKLYEsg973ymTucZszTzTN98d8esd2',
);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
$data = curl_exec($curl);
//分离 header 与 body
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //头信息大小
$header = substr($data, 0, $headerSize);
$body = substr($data, $headerSize);
// 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
$body_data = json_decode($body);
echo $data;
//关闭URL请求
curl_close($curl);
封装一下请求函数,代码示例:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93/**
* @author
*
* @param string $url
* @param array $params
* @param string $curlType 请求服务端类型 'json'-json请求; 为空时form-data请求
* @param array $header 请求的头信息
*
* @return mixed
*/
function curlRequest($url, $params = [], $curlType = '', $header = [])
{
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //超时
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
if ($header) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}
if ($params) {
curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
switch ($curlType) {
case 'json':
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
break;
default:
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
break;
}
}
$apiResult = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); // 获取http状态码。
if ($header) {
//分离 header 与 body
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //头信息大小
$header = substr($apiResult, 0, $headerSize);
$body = substr($apiResult, $headerSize);
$apiResult = $body;
}
curl_close($curl);
return [
'httpCode' => $httpCode,
'data' => $apiResult,
];
}
/**
* 登录 POST.
*/
$url = 'http://larabbs_api1.test/api/authorizations';
//设置post数据
$params = array(
'username' => 'xiao@123.com',
'password' => 'password',
'grant_type' => 'password',
'client_id' => '1',
'client_secret' => '42YYyaogGjaKLYEsg973ymTucZszTzTN98d8esd2',
);
$header = array(
'content-type:application/x-www-form-urlencoded;
charset=UTF-8',
);
$data = curlRequest($url, http_build_query($params), '', $header);
$body_data = json_decode($data['data']);
$access_token = $body_data->access_token;
var_dump($data);
/*
* 获取用户信息 ,GET.
*/
$header1 = array(
'content-type:application/json;
charset=UTF-8;
Authorization:'.'Bearer '.$access_token,
);
$url1 = 'http://larabbs_api1.test/api/user';
$headers[] = 'Accept:application/json';
$headers[] = 'Authorization: Bearer '.$access_token;
$data1 = curlRequest($url1, null, '', $headers);
var_dump($data1);
总结
curl的原理是模拟浏览器的操作,效率要高于file_get_contents