-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_curl.php
More file actions
67 lines (60 loc) · 2.05 KB
/
test_curl.php
File metadata and controls
67 lines (60 loc) · 2.05 KB
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
<?php
function curl_v1($url, $is_post = 1, $post_data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($is_post == 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($post_data))
]
);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}else{
curl_setopt($ch, CURLOPT_HEADER, 0);
}
$data = curl_exec($ch);
curl_close($ch);
if (empty($data))
return array();
return $data;
}
function curl_post($url, $is_post = 1, $post_data = [], $async = 1)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($async == 1) {
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
} else {
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100); //超时毫秒
}
if ($is_post == 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($post_data))
]
);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}else{
curl_setopt($ch, CURLOPT_HEADER, 0);
}
$data = curl_exec($ch);
curl_close($ch);
if (empty($data))
return array();
return json_decode($data, true);
}
$url = 'http://dev.api.agc.moviebook.cn/api/materialcategory/?format=json';
$res = curl_post($url,0);
var_dump($res);