-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.php
79 lines (67 loc) · 2.03 KB
/
Curl.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
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
<?php
/**
* Created by PhpStorm.
* User: kunal
* Date: 8/28/2019
* Time: 3:20 PM
*/
class Curl
{
private static $instance;
public function __construct()
{
set_time_limit(0);
self::$instance = new self();
}
/**
* @param $url
* @param $type
* @param $parameters
* @return array|mixed|object
* HOW TO MAKE THE CALL::
*
* require_once get_template_directory() . "/framework/Curl.php";
* $output = Curl::httpPost($url, "POST", $params);
* var_dump($output);
*/
public static function httpPost($url, $type, $parameters)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'App-Key: ' . 'YOUR AUTH KEY GOES HERE')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300) {
$result = ($output);
}else {
$result=[
"code" =>1,
"response" => 'Error',
"errors" => ['Unable to load data!'],
"warnings" => [],
"data" => [$httpcode],
];
}
return $result;
}
public static function httpGet($url,$type,$params){
$url = rtrim($url,"?") . "?" . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Key: ' . 'YOUR AUTH KEY GOES HERE')
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}