PHP API Quickstart tutorial example 3, as-written doesn't work due to json_encode being run on an already encoded JSON object

The last example described in the PHP API Quickstart tutorial here: https://support.monday.com/hc/en-us/articles/360013465659-API-Quickstart-Tutorial-PHP does not work out of the box.

This example indicates that variables should be passed in as part of the request in a separate vars object passed alongside the query object; modified ever so slightly to work with text columns

<?php
$token = 'tokenstring';
    $apiUrl = 'https://api.monday.com/v2';
    $headers = ['Content-Type: application/json', 'Authorization: ' . $token];

    $query = 'mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:XXXXXXXXX, item_name:$myItemName, column_values:$columnVals) { id } }';
    $vars = ['myItemName' => 'Hello world!',
      'columnVals' => json_encode([
        'long_text5' => ['text' => '0400000000'],
        'phone3' => ['text' => 'asd']
      ])];
    $test_encoded = json_encode(['query' => $query, 'variables' => $vars]);
    $data = @file_get_contents($apiUrl, false, stream_context_create([
      'http' => [
        'method' => 'POST',
        'header' => $headers,
        'content' => $test_encoded,
      ]
    ]));
    $responseContent = json_decode($data, false);

    echo json_encode($responseContent);
?>

It also seems to indicate the columnVals should be json_encoded at the time of assignment. Encoding columnVals here, however, AND again when constructing the content array as part of the $data request seems to lead to an error response from the API. The request sent to the API is valid JSON:

$test_encoded just after assignment (Valid JSON):
{“query”:“mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:XXXXXXXXX, item_name:$myItemName, column_values:$columnVals) { id } }”,“variables”:{“myItemName”:“Hello world!”,“columnVals”:“{"long_text5":{"text":"0400000000"},"phone3":{"text":"asd"}}”}}

Yet the response seems to indicate php is sending a PHP array instead of JSON as part of the request?

{"error_code":"ColumnValueException","status_code":200,"error_message":"invalid value, please check our API documentation for the correct data structure for this column. https://api.developer.monday.com/docs/change-column-values","error_data":{"column_value":"{\"text\"=\u003e\"asd\"}","column_type":"TextColumn"}}

I’m under no illusion there is something simple I’m missing, or a flag that needs to be set somewhere - would be good to understand how this works, and maybe updating the example with a more complex column type requiring a deeper associative array like email.

Any help is greatly appreciated!

Cheers,

Hey, great feedback. I see that the dummy example is a bit simplistic and I can certainly see how it can be improved.

Let’s try and get your example working. To update columns on your board, you need to ensure two things:

  1. The column IDs that are passed must correspond to the IDs on your board. The example contains some dummy IDs that you need to update. Here’s how to get the column IDs of your board: Board, item, column, and automation or integration IDs
  2. The structure of each column value needs to correspond to the type of each column. For example, a phone column accepts different data than a text column. Check out our API reference here: Column Values

As an example, here’s how you would structure the $vars variable to update an email column:

$vars = ['myItemName' => 'Hello world!',
      'columnVals' => json_encode([
        'long_text5' => ['text' => '0400000000'],
        'email' => ['email' => 'alice@aliceandbob.com', 'text' => 'alice@aliceandbob.com']
      ])];

Did you ever get this figured out? How should the example look if you don’t mind me asking? just getting ‘null’ when I try it. For one the example they have posted not doesn’t have the first three lines declaring $token, $apiUrl and $headers. but I just copied it from the other examples which I did get to work.

Been talking to support about problems with the tutorial but still cant figure it out.

added first 3 lines
added my column_values and ‘text’

Here is what I’m have:

<?php

$token = 'MY_TOKEN_IS_HERE';
$apiUrl = 'https://api.monday.com/v2';
$headers = ['Content-Type: application/json', 'Authorization: ' . $token];

$query = 'mutation ($myItemName: String!, $columnVals: JSON!) { create_item (board_id:MY_BOARD_ID_IS_HERE, item_name:$myItemName, column_values:$columnVals) { id } }';
$vars = ['myItemName' => 'Hello world!', 
  'columnVals' => json_encode([
    'text9' => ['text' => 'TEST VENDOR'], 
    'long_text' => ['text' => '12345678901234567']
])];

$data = @file_get_contents($apiUrl, false, stream_context_create([
 'http' => [
 'method' => 'POST',
 'header' => $headers,
 'content' => json_encode(['query' => $query, 'variables' => $vars]),
 ]
]));
$responseContent = json_decode($data, true);

echo json_encode($responseContent);
?>

Hello @hderic,

Just in case, I wanted to let you know that I replied in the ticket you opened :slightly_smiling_face:

Cheers,
Matias