Search code examples
phpquickbase

how to properly insert into quickbase via Curl in php


I created table with the following 3 fields with quickbase

name,email,post

Now am trying to insert into the table via table id with quickbase.

here is the documentation API Link API lINK

here is the API Curl documentation

curl -X POST 'https://api.quickbase.com/v1/records' \
-H 'QB-Realm-Hostname: {QB-Realm-Hostname}' \
-H 'User-Agent: {User-Agent}' \
-H 'Authorization: {Authorization}' \
-d {}

here is the sample request

{
  "to": "bck7gp3q2",
  "data": [
    {
      "6": {
        "value": "This is my text"
      },
      "7": {
        "value": 10
      },
      "8": {
        "value": "2019-12-18T08:00:00.000Z"
      },
      "9": {
        "value": [
          "a",
          "b"
        ]
      },
      "10": {
        "value": true
      },
      "11": {
        "value": "[email protected]"
      },
      "12": {
        "value": "www.quickbase.com"
      },
      "13": {
        "value": [
          {
            "id": "123456.ab1s"
          },
          {
            "id": "254789.mkgp"
          },
          {
            "id": "789654.vc2s"
          }
        ]
      }
    }
  ],
  "fieldsToReturn": [
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13
  ]
}

Here is my effort so far When I run the code below, I have the following error

{"data":[],"metadata":{"createdRecordIds":[],"lineErrors":{"1":["Can not find record by ID \"My First post\"."]},"totalNumberOfRecordsProcessed":1,"unchangedRecordIds":[],"updatedRecordIds":[]}}{"data":[],"metadata":{"createdRecordIds":[],"lineErrors":{"1":["Can not find record by ID \"My First post\"."]},"totalNumberOfRecordsProcessed":1,"unchangedRecordIds":[],"updatedRecordIds":[]}}

here is my code

<?php

$access_t ="my_access_token-goes here";
$url="https://api.quickbase.com/v1/records";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
$useragent ='my-user-agent goes here';

$quicbase_domain= 'my-quickbase-domain-goes-here';

$data='

{
  "to": "my-table-id-goes-here",
  "data": [
    {


      "1": {
        "value": "nancy more"
      },
      "2": {
        "value": "[email protected]"
      },

"3": {
        "value": "My First post"
      }

    }
  ]
  
}

';

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"QB-Realm-Hostname: quicbase_domain",
"User-Agent: $useragent",
"Authorization: QB-USER-TOKEN $access_t",
'Content-Type:application/json'
));  

curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
 echo $response = curl_exec($ch);

curl_close($ch);

print_r($response);

Solution

  • The same endpoint is used for both insert and update and Quick Base uses the inclusion of the key field to distinguish between the two. In my experience field ID #3 has always been the "Record ID#" field; one of the fields Quick Base automatically creates for every new table and the one it uses as the default key field. Since your data array contains a value for field ID #3, Quick Base is interpreting your request as an update to which it can find no record with the ID "My First Post". There's also the issue that field ID #3 expects a number, but Quick Base's error response is not sophisticated enough to give that feedback.

    I think the issue with your request is that you have the wrong field IDs. 1, 2, and 3 correspond to the automatically generated fields of "Date Created", "Date Modified", and "Record ID#". The fields that you created for your table probably start at field ID #6. If you go into Quick Base and look at the properties of your table you should be able to see a list of fields and their Field Ids (if you don't see the Field Id column, you can choose to display it in Advanced Options). Use the correct field IDs in your request, and it should work as expected.