-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathuse-structured-metadata.php
More file actions
97 lines (77 loc) · 2.83 KB
/
Copy pathuse-structured-metadata.php
File metadata and controls
97 lines (77 loc) · 2.83 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Declare a structured metadata field, set it on an upload, update it, and search by it.
*
* Prerequisites:
* composer require cloudinary/cloudinary_php
* export CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
*
* Run:
* php examples/use-structured-metadata.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\Api\Exception\BadRequest;
use Cloudinary\Api\Metadata\StringMetadataField;
use Cloudinary\Cloudinary;
const SOURCE_IMAGE = 'https://res.cloudinary.com/demo/image/upload/sample.jpg';
const PUBLIC_ID = 'examples/metadata-demo';
const FIELD_ID = 'photographer';
function main(): void
{
$cloudinary = new Cloudinary();
declareField($cloudinary);
// Set the value at upload time. Keys must already exist, or the upload is rejected.
$result = $cloudinary->uploadApi()->upload(SOURCE_IMAGE, [
'public_id' => PUBLIC_ID,
'metadata' => [FIELD_ID => 'Ada Lovelace'],
]);
echo 'Uploaded: ', $result['public_id'], PHP_EOL;
echo 'Metadata: ', json_encode($result['metadata']), PHP_EOL;
// Change the value on the existing asset.
$updated = $cloudinary->adminApi()->update(PUBLIC_ID, [
'metadata' => [FIELD_ID => 'Grace Hopper'],
]);
echo 'Updated: ', json_encode($updated['metadata']), PHP_EOL;
// Declared fields are indexed and searchable.
sleep(5);
$found = $cloudinary->searchApi()
->expression('metadata.' . FIELD_ID . ':"Grace Hopper"')
->execute();
echo 'Search matches: ', $found['total_count'], PHP_EOL;
// What the account has declared.
$fields = $cloudinary->adminApi()->listMetadataFields();
foreach ($fields['metadata_fields'] as $field) {
echo ' ', $field['external_id'], ' (', $field['type'], ')', PHP_EOL;
}
}
/**
* Declares the field, tolerating the case where a previous run already created it.
*/
function declareField(Cloudinary $cloudinary): void
{
$field = new StringMetadataField('Photographer');
$field->setExternalId(FIELD_ID);
try {
$cloudinary->adminApi()->addMetadataField($field);
echo 'Declared field: ', FIELD_ID, PHP_EOL;
} catch (BadRequest $e) {
// Declaring is not idempotent; an existing external_id is a BadRequest.
if (!str_contains($e->getMessage(), 'already exists')) {
throw $e;
}
echo 'Field already declared: ', FIELD_ID, PHP_EOL;
}
}
try {
main();
} catch (Throwable $e) {
fwrite(STDERR, 'Structured metadata flow failed: ' . $e->getMessage() . PHP_EOL);
fwrite(STDERR, 'Check that CLOUDINARY_URL is set (Console > Settings > API Keys).' . PHP_EOL);
exit(1);
}