PATCH request through PHP

Any advice on getting a PATCH request through PHP to work? I’ve tried editing just about every line of the following code (API key, base key, and row ID omitted). PHP doesn’t give any errors, but the ID won’t update.

$ch = curl_init();

$api_key = '';
$base = '';
$table = $_GET['game_id'];
$url = 'https://api.airtable.com/v0/' . $base . '/' . $table;
$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $api_key,
);

// json body
$json_array = [
      [
        'id' => '',
        'fields' => [
          'coordinate' => 'test'
        ]
      ]
  ]; 
$body = json_encode($json_array);


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$r = curl_exec($ch);
curl_close($ch);

@kuovonne any suggestions?

I don’t see anything obvious, but it’s been a while since I’ve written in PHP.
Here are some general troubleshooting/debugging suggestions.

Are you able to get and create records with the same API key, base ID, and table ID? If so, then they are not the issue.

For the record Id, are you using Airtable’s internal record id that begins with rec followed by 14 characters? Your code does not show how you are getting the record ID.

Try echoing the response you get from the request. It may have some clues.

Try using echo to see the progress of the script and the values used (since PHP doesn’t have the equivalent of console.log). If nothing echos, maybe you have a syntax error.

This was very helpful, thanks so much for the debugging tips! I’m pretty new to PHP. Through an echo, I found the problem: the response string is returning error “INVALID_REQUEST_MISSING_FIELDS, could not find field “fields” in the request body”. Odd, but a good clue to go off of.

Any advice on dealing with this error? Patch request: "Could not find field \"fields\" in the request body" error

It sounds like Airtable does not like how you have formed the request body.
Double check with the REST API documentation on how to form your request body. Note that the request body (and the endpoint) varies slightly depending on if you are updating a single record versus a list of records. For example, it looks like with a list, you need to include a “records” level in the body.

This topic was solved and automatically closed 15 days after the last reply. New replies are no longer allowed.