-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxamp.php
84 lines (73 loc) · 1.99 KB
/
xamp.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
80
81
82
83
84
<?php
//phpinfo();
//url declaration
$url = "https://reqres.in/api/users";
//data array to be POSTED
$information_array = array(
'Name' => 'Parul Parul',
'Job' => 'Software Developer',
'Following Data is fetched from 2 different APIs' => 'https://jsonplaceholder.typicode.com/users
and https://jsonplaceholder.typicode.com/todos ');
$data = http_build_query($information_array);
//Commands for POST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
//errors check
if($e = curl_error($ch))
{
echo $e;
}
else
{
$decoded = json_decode($resp);
foreach ($decoded as $key => $val)
{
echo $key . ': ' . $val . '<br>';
}
}
curl_close($ch);
class cURL {
//Initialize value
public $curl = null;
//Function to call curl_init to store the resource internally
public function __construct($url = null){
return $this->init($url);
}
//Calling for the class
//Called function will return value
public function __call($n,$p){
if($n=='init' || $n=='multi_init'){
//Connection closed
if($this->curl) curl_close($this->curl);
//Resource saved internally
return $this->curl = call_user_func_array('curl_'.$n,$p);
} else {
//Current resource to the function call
array_unshift($p,$this->curl);
return call_user_func_array('curl_'.$n,$p);
}
}
}
//Executing class for API 1
$http = new cURL("https://jsonplaceholder.typicode.com/users");
$http->setopt(CURLOPT_HEADER, 0);
$http->setopt(CURLOPT_RETURNTRANSFER, 1);
echo '<pre>';
echo '<hr /><h1> List of Users and their information </h1>';
echo $http->exec();
echo '</pre>';
$http->close();
//Executing class for API 2
$http = new cURL("https://jsonplaceholder.typicode.com/todos");
$http->setopt(CURLOPT_HEADER, 0);
$http->setopt(CURLOPT_RETURNTRANSFER, 1);
echo '<pre>';
echo '<hr /><h1> List of TODOS </h1>';
echo $http->exec();
echo '</pre>';
$http->close();
?>