How to use a PHP array?

I have a PHP function from an SDK that returns an array. How can I use that in Wappler?

Thanks.

Hi,
Can you please post some code on where you wish to use this? Server or client side? How does the function work?

It’s the Cloudinary API SDK. There’s a 500/hr rate limit on the Cloudinary Admin API so I’m trying to do as few calls as possible so if I could do one call, grab all the info I need for that session, and store it locally somehow it would be great. I have the functionality I need working (somewhat) using ajax, but it sure would be nice to just be able to grab the info in Wappler!

This call -

$tid = $api->resources_by_context("tripbit_id", $tripbit_id);

Returns (just one result for simplicity, but mostly it’s multiple)…

                Cloudinary\Api\Response Object
(
    [rate_limit_reset_at] => 1608685200
    [rate_limit_allowed] => 500
    [rate_limit_remaining] => 491
    [storage:ArrayObject:private] => Array
        (
            [resources] => Array
                (
                    [0] => Array
                        (
                            [asset_id] => c4eff44fd8e88ceb42a1188b3796833d
                            [public_id] => 11
                            [format] => jpg
                            [version] => 1605385100
                            [resource_type] => image
                            [type] => upload
                            [created_at] => 2020-11-14T20:18:20Z
                            [bytes] => 10702
                            [width] => 225
                            [height] => 225
                            [access_mode] => public
                            [url] => http://res.cloudinary.com/tripmakers/image/upload/v1605385100/11.jpg
                            [secure_url] => https://res.cloudinary.com/tripmakers/image/upload/v1605385100/11.jpg
                            [context] => Array
                                (
                                    [custom] => Array
                                        (
                                            [tripbit_id] => abc
                                        )

                                )

                        )

                )

You could create a custom formatter to set a value/session value (session might be better if you plan to re-use the data)

Create the formatter (you will need to include the Cloudinary PHP script)

<?php namespace lib\core; 
function formatter_cloudinary($val){ 
$retval = $api->resources_by_context("tripbit_id", $tripbit_id);
// you could even just return the resources or storage part
return $retval; 
} 
?>

and pass the id(s) as an array to the formatter when setting the session value

tripbitids.cloudinary()

You should then be able access it.
The Wappler UI won’t find the schema for this, but you can just access as normal:
cloudinary_session.resources (if you just returned the ‘storage’ object)

Are you unable to get the data via client API action? You might not need a custom Ajax call.
If API action works for you, you can store the response in local storage and use it from there. Most of it should work fine with Wappler formatters.
In case you run into some complex nested structure or stuff, you can get into custom formatters to help you parse the response data.

I double checked with them this morning…“Currently, we do not provide such a feature by context. I can consult our Product team if this feature is on our roadmap.”

Awesome!! Thanks, I’ll give that a go.

I created the customformatter.php file, included the necessary Cloudinary files and the code (I changed $tripbit_id to $val though - I think that’s right). I uploaded that to the server and confirmed that it’s there.

Then I tried to use that to set a session value but I get a 500 error.

I’m confused where the tripbitids.cloudinary() comes from. I understand that cloudinary is the name of the formatter so also tried cloudinary(‘abc’) but that didn’t work either.

Thanks so much for your help!

Which feature are you talking about?
When I say API action, I mean Wappler’s client side component.

I think I follow. You need to use the SDK. Can’t do direct API call.
In that case, you can use JS in PHP to use the response as a variable (when PHP renders the page) and then use dmx.parse to set a Wappler component’s data when page runs on browser.

Set a value (before calling the formatter) to the value of the id you are looking for - this could be from a database query, a manual string or result of an API call (pretend I called mine, for this example, tripbitids). If you can pass an array for multiple results, just set the value to an array instead.

When setting the Session value use

tripbitids.cloudinary()

When using a formatter, the first parameter passed to the formatter is always the value being formatted, in this case tripbitids. That’s why the brackets can remain empty.

So, after a lot of investigation and paring the formatter down to as simple as possible because the formatter was causing all my server connects to Error 500- it seems you can’t include files in formatters??

This works…

namespace lib\core;
function formatter_cloudinary($val){
$retval=“OK”;
return $retval;
}

This makes all my server connects stop working with a 500 error.

require “/autoload.php”;
namespace lib\core;
function formatter_cloudinary($val){
$retval=“OK”;
return $retval;
}

I tried including a bunch of different files but it doesn’t matter what file, it causes the error.

@teodor - any ideas on this?

Thanks. Heather

Just to check…
The autoload.php is in the root folder of your site?
If you turn on debug mode, do you get any error message in the console?

Yes. And I tried putting a file in the same folder. Same result.

If formatters can’t include your libraries…
I’d try to create my own new custom action step.
Or just write a regular php page that will output results as JSON, then call it with the API action from within server connect.

Hi Heather,
I just noticed that the requires line is before the namesapce declaration. I think the Namespace has to be the very first line. You also may need to make it:

require $_SERVER['DOCUMENT_ROOT']."/autoload.php";

As the path will be relative to the formatters folder otherwise

Finally, make sure that the src folder (from the Cloudinary Github resources) is added to the root folder along with the autoload.php file

Here you go:

namespace lib\core;
require $_SERVER['DOCUMENT_ROOT'].“/autoload.php”;
function formatter_cloudinary($val){
$retval=“OK”;
return $retval;
}
1 Like

Thanks Ben. Sorry - have been busy with the holidays. I’ll give this a try soon.