John Davidson

How to read cloudinary API Response Object in php to get the secure url after upload

0 comments
Message:


I am trying to upload to cloudinary through php, I tried the following code from my localhost and upload is successful and I get the response as Cloudinary APIResponse object


$config = Configuration::instance();
$config->cloud->cloudName = 'cloudname';
$config->cloud->apiKey = 'apikey';
$config->cloud->apiSecret = 'secrethere';
$config->url->secure = true;

$cloudinary = new Cloudinary($config);

$resp = $cloudinary->uploadApi()->upload( './Broccoli.gif', ["use_filename" => TRUE, "unique_filename" => FALSE, "folder" => "mythumbs"] );

and the response is


Cloudinary\Api\ApiResponse Object
(
[rateLimitResetAt] =>
[rateLimitAllowed] => 0
[rateLimitRemaining] => 0
[headers] => Array
(
[Date] => Array
(
[0] => Sat, 28 May 2022 18:58:47 GMT
)

[Content-Type] => Array
(
[0] => application/json; charset=utf-8
)

[Transfer-Encoding] => Array
(
[0] => chunked
)

[Connection] => Array
(
[0] => keep-alive
)

[Vary] => Array
(
[0] => Accept-Encoding
)

[Status] => Array
(
[0] => 200 OK
)

[Cache-Control] => Array
(
[0] => max-age=0, private, must-revalidate
)

[X-XSS-Protection] => Array
(
[0] => 1; mode=block
)

[X-Request-Id] => Array
(
[0] => a57f3df3ad7ba508ac21009183adb527
)

[X-UA-Compatible] => Array
(
[0] => IE=Edge,chrome=1
)

[ETag] => Array
(
[0] => "38ba68cdf700d347e507b1f5e15176fe"
)

[Server] => Array
(
[0] => cloudinary
)

)

[storage:ArrayObject:private] => Array
(
[asset_id] => 68b431d7e07ffeca56af43062eb6da22
[public_id] => mythumbs/Broccoli
[version] => 1653764327
[version_id] => ba3190f6dc801e856e8056ac0e2a8be8
[signature] => 50974060aa15a82b8fa0e5ee6c49809d82c46189
[width] => 600
[height] => 600
[format] => gif
[resource_type] => image
[created_at] => 2022-05-28T18:58:47Z
[tags] => Array
(
)

[pages] => 1
[bytes] => 13349
[type] => upload
[etag] => fc64f57bcf6066b19f1b7d803298bc25
[placeholder] =>
[url] => http://res.cloudinary.com/hooshiyaa/cloudname/upload/v1653764327/mythumbs/Broccoli.gif
[secure_url] => https://res.cloudinary.com/cloudname/image/upload/v1653764327/mythumbs/Broccoli.gif
[folder] => mythumbs
[original_filename] => Broccoli
[api_key] =>
)

)

I couldn't find any example of how to read this response object on cloudinary documentation. I want to retrieve the secure url and store it in db.



My Answer:
> Here is an example of how you can read the Cloudinary API Response Object in PHP to get the secure URL after uploading an image:

php
// Include the Cloudinary library
require 'vendor/autoload.php';

// Set your Cloudinary credentials
\Cloudinary::config(array(
"cloud_name" => "YOUR_CLOUD_NAME",
"api_key" => "YOUR_API_KEY",
"api_secret" => "YOUR_API_SECRET"
));

// Upload the image to Cloudinary
$result = \Cloudinary\Uploader::upload("path/to/your/image.jpg");

// Get the secure URL of the uploaded image from the API Response Object
$secureUrl = $result['secure_url'];

// Print the secure URL
echo $secureUrl;


In this code snippet, we first include the Cloudinary library and set our Cloudinary credentials. We then upload an image to Cloudinary using the `Uploader::upload` method, which returns an API Response Object. We can access the secure URL of the uploaded image by accessing the `secure_url` key in the result array.

Finally, we print the secure URL to the screen. You can use this URL to display the uploaded image on your website or application.

Rate this post

3 of 5 based on 1476 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us