-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
03-download-jpg.php
52 lines (43 loc) · 1.43 KB
/
03-download-jpg.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
<?php
require(implode(DIRECTORY_SEPARATOR, ["..", "vendor", "autoload.php"]));
use Gt\Fetch\Http;
use Gt\Http\Blob;
use Gt\Http\Response;
/*
* This example uses Cat-as-a-Service API to request a random photo of a cat.
* See https://cataas.com/ for more information.
*/
// Example: Download an image from the API and store in the temp directory.
$http = new Http();
$http->fetch("https://cataas.com/cat")
->then(function(Response $response) {
if(!$response->ok) {
throw new RuntimeException("Error getting a cat. (ERROR $response->status)");
}
echo "Cat as a Service responded with a binary file with the following attributes:" . PHP_EOL;
echo "Response size: " . $response->getHeaderLine("Content-Length") . PHP_EOL;
echo "File type: " . $response->getHeaderLine("Content-Type") . PHP_EOL;
return $response->blob();
})
->then(function(Blob $blob) {
switch($blob->type) {
case "image/jpeg":
$extension = "jpg";
break;
case "image/png":
$extension = "png";
break;
default:
echo $blob->type . " type is not supported." . PHP_EOL;
exit(1);
}
$file = new SplFileObject("/tmp/cat.$extension", "w");
$bytesWritten = $file->fwrite($blob);
echo "Written $bytesWritten bytes." . PHP_EOL;
echo "Photo written to " . $file->getPathname() . PHP_EOL;
})
->catch(function(Throwable $reason) {
echo "There was an error caught: ", $reason->getMessage(), PHP_EOL;
});
// To execute the above Promise(s), call wait() or all().
$http->wait();