Repository navigation
Expand file tree
/
Copy pathuse-custom-http-client.php
More file actions
64 lines (53 loc) · 2.02 KB
/
Copy pathuse-custom-http-client.php
File metadata and controls
64 lines (53 loc) · 2.02 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Send all SDK requests through your own PSR-18 HTTP client, then upload an image.
*
* This example uses Symfony HttpClient with retries; any PSR-18 client works.
*
* Prerequisites:
* composer require cloudinary/cloudinary_php symfony/http-client
* export CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
*
* Run:
* php examples/use-custom-http-client.php
*
* In your own project, the import and instantiation are:
* require 'vendor/autoload.php';
* use Cloudinary\Cloudinary;
* $cloudinary = new Cloudinary();
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Cloudinary\Cloudinary;
use GuzzleHttp\Psr7\HttpFactory;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\RetryableHttpClient;
const SOURCE_IMAGE = 'https://res.cloudinary.com/demo/image/upload/sample.jpg';
const PUBLIC_ID = 'examples/use-custom-http-client';
function main(): void
{
// Timeouts, proxy and retries are configured on your client, not in Cloudinary config.
$httpClient = new RetryableHttpClient(HttpClient::create(['timeout' => 60]), null, 3);
// API objects returned by this instance send their requests through it.
$cloudinary = new Cloudinary();
$cloudinary->setHttpClient(new Psr18Client($httpClient, new HttpFactory(), new HttpFactory()));
$result = $cloudinary->uploadApi()->upload(
SOURCE_IMAGE,
[
'public_id' => PUBLIC_ID,
'tags' => ['example'],
]
);
echo 'Uploaded via PSR-18 client: ', $result['public_id'], PHP_EOL;
echo 'URL: ', $result['secure_url'], PHP_EOL;
$asset = $cloudinary->adminApi()->asset(PUBLIC_ID);
echo 'Fetched via PSR-18 client: ', $asset['bytes'], ' bytes', PHP_EOL;
}
try {
main();
} catch (Throwable $e) {
fwrite(STDERR, 'Request failed: ' . $e->getMessage() . PHP_EOL);
fwrite(STDERR, 'Check that CLOUDINARY_URL is set (Console > Settings > API Keys).' . PHP_EOL);
exit(1);
}