php-curl

主要用于 Api 请求,开发必备利器!

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
# post
function ajaxPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);

if($res === false) {
var_dump(curl_errno($ch));
var_dump(curl_error($ch));
} else {
var_dump($res);
$arr = json_decode($res, TRUE);
var_dump($arr);
}
curl_close($ch);
}

# get
function ajaxGet($url, $data) {


$url .= '?';
foreach($data as $key=>$row) {
$url .= $key . '=' . $row . '&';
}

$url = substr($url, 0, -1);

$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$res = curl_exec($ch);

if($res === false) {
var_dump(curl_errno($ch));
var_dump(curl_error($ch));
} else {
var_dump($res);
$arr = json_decode($res, TRUE);
var_dump($arr);
}
curl_close($ch);
}

现在有许多第三方的库,我们可以使用第三方库方便实现 Curl 功能.

©版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 & 作者信息

End

坚持原创技术分享,您的支持将鼓励我继续创作!
Flyertutor WeChat Pay

WeChat Pay

Flyertutor Alipay

Alipay