forked from ICT-BLJ/phpdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomname.php
More file actions
47 lines (39 loc) · 1.3 KB
/
Copy pathrandomname.php
File metadata and controls
47 lines (39 loc) · 1.3 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
<?php
$url = "https://randomname.de/";
$params = array(
"count" => "3",
"gender" => "b",
"minAge" => "18",
"format" => "json"
);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$url .= '?' . http_build_query($params);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
# header('Content-Type: application/json; charset=utf-8');
if ($code == 200) {
$response = json_decode($response, true);
echo "Got " . count($response) . " entries ..." . PHP_EOL;
// Show the json response array to find the keys or RTFM ;)
// print_r($response);
// Loop over array items
for ($i = 0; $i < count($response); $i++) {
print PHP_EOL;
// Output array item using the appropriate key
printf("%s %s" . PHP_EOL, $response[$i]['firstname'], $response[$i]['lastname']);
printf("%d %s" . PHP_EOL, $response[$i]['location']['zip'], $response[$i]['location']['city']);
print PHP_EOL;
}
} else {
echo 'error ' . $code;
}