-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path01-basic-fetch.php
46 lines (39 loc) · 1.19 KB
/
01-basic-fetch.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
<?php
require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"]));
use Gt\Fetch\Http;
use Gt\Http\Response;
use Gt\Json\JsonKvpObject;
use Gt\Json\JsonPrimitive\JsonArrayPrimitive;
$http = new Http();
$http->fetch("https://api.github.com/orgs/phpgt/repos")
//$http->fetch("https://raw.githubusercontent.com/PhpGt/Fetch/master/broken.json")
->then(function(Response $response) {
echo "Got a response. Processing JSON... ", PHP_EOL;
if(!$response->ok) {
throw new RuntimeException("Can't retrieve Github's API on $response->uri");
}
return $response->json();
})
->then(function(JsonArrayPrimitive $json) {
echo "SUCCESS: Json promise resolved!", PHP_EOL, PHP_EOL;
echo "PHP.Gt has the following repositories: ";
$repoList = [];
/** @var JsonKvpObject $item */
foreach($json->getPrimitiveValue() as $item) {
array_push($repoList, $item->getString("name"));
}
echo wordwrap(implode(", ", $repoList)) . ".";
echo PHP_EOL, PHP_EOL;
})
->catch(function(Throwable $reason) {
echo "There was an error!",
PHP_EOL,
get_class($reason),
" - ",
$reason->getMessage(),
" ",
$reason->getFile(),
":", $reason->getLine(),
PHP_EOL, PHP_EOL;
});
$http->wait();