-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.php
37 lines (33 loc) · 1.25 KB
/
http.php
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
<?php
function get_query($url, $args) {
$query = http_build_query($args);
$full_url = "$url?$query";
$ch = curl_init($full_url);
// make curl_exec return HTTP result body as string, instead of sending to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ensure that 404 and other errors cause curl_exec to return false
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// automatically follow any redirects (e.g. HTTP status 301)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function post_query($url, $args) {
$query = http_build_query($args);
$ch = curl_init($url);
// make curl_exec return HTTP result body as string, instead of sending to browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// ensure that 404 and other errors cause curl_exec to return false
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// make a post request
curl_setopt($ch, CURLOPT_POST, true);
// sets the post fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
// automatically follow any redirects (e.g. HTTP status 301)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>