Adalo API - PHP Implementation

Hi everyone,

Would someone be able to review my code below? I’ve been stuck with it for the past few hours and just cannot get it to work. I’m trying to use the Adalo Collections API to add a record. I’m using PHP to make a cURL request and send it.

My code is below:

// function
function httpPost($data) {
    $url = "MyCollectionsURL";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $headers = array(
        'Authorization: Bearer MySecurityToken',
        'Content-Type: application/json'
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    curl_exec($curl);
    curl_close($curl);
}

// calling function
$adalo_array = http_build_query(array('Name'=>$ex1,'Age'=>$ex2,'Address'=>$ex3));
httpPost($adalo_array);

This is more of a PHP Question, but I have tried my code with another API and it works. Could it be something to do with Adalo’s authentication process?

Thank you

1 Like

Hi, wondering if you’ve solved this? I’m having the very same issue trying to update single record.

Using your code I get the following response from the server:

Error
Bad Request

I’ve determined this is an issue with using PHP to make the request. After continuing to play I came upon an error from the CDN Adalo are using to process api requests. The response from the CDN is “Application Error”.

The only workaround I’ve found is to use Python rather than PHP. Example of working code:

START

import requests
from requests.structures import CaseInsensitiveDict

url = “[YOUR SPECIFIC URL]”

headers = CaseInsensitiveDict()
headers[“Authorization”] = “Bearer [YOUR AUTH KEY]”
headers[“Content-Type”] = “application/json”

data = ‘{“Username”:“Test”}’
resp = requests.put(url, headers=headers, data=data)
print(resp.status_code)

END

Hope this helps someone else.

Josh

Hello there,

Here’s a working PHP example for you.
I think the only thing you’d need to tweak on your original PHP code json_encode the payload instead of using http_build_query given that the API requires JSON.

$url = "https://api.adalo.com/v0/apps/app-id/collections/collection-id";
$payload = json_encode([
    "Email" => "email@example.com",
    "Full Name" => "Joe Bloggs",
    "Username" => "jbloggs"
]);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: Bearer my-token"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

echo "<pre>$result</pre>";

It works also in PHP you just have to encode your array as json.

Replace in line 28: $adalo_array = http_build_query(json_encode($data));

1 Like

That’s great. It’s always the little things! Thanks.

Thanks for the help everyone - I’ve figured it out :grinning: